Redis
Redis is an in-memory database, good to be used as
- a cache
- a message queue
Installation
Ubuntu
Installing Redis on ubuntu
sudo apt-get install redis-tools- default port is 6379
- Use
redis-clito enter redis commands - if you need redis instance be outside, do
nano /etc/redis/redis.confand replacebind 127.0.0.1tobind 0.0.0.0 - restart the server
service redis-server restart - to check if you can connect to redis from another server, do
redis-cli -h 192.168.x.x ping, should getPONGin return
Python
Python library:
sudo pip install redisorsudo easy_install install redisor
Building Blocks
Databases
- Each redis server can have several databases
- they are indexed by integers: 0, 1, 2, …
- 0 is the default db
- to choose the 1st db, use
select 1 - redis keeps all the data in memory, but by default, redis also snapshots DB to disk after each $X$ changes
- there’s also append-only mode
Key/Value Pairs
String key/values
- key can be anything, e.g.
users:ololo,:doesn’t have any specific meaning, but can be used to separate “table name” and “key name” - command
set users:ololo '{"name":"ololo", ...}'- the content can be any string, not necessarily JSON - to get the value, use
get users:ololo - you can query only by key, never by value
String functions:
strlen keyreturns the lenth of the value associated with keykeygetrange key 0 10returns the substring from 0 to 10 of the value stored bykeyappend key value2appends ‘value2’ to the current value stored bykey
Counters
- You can store counters in redis
incr keyincrement the integer value stored inkey, or create a new key with value 1decr keydecrement the valueincrbyanddecrbyare used to increase/decrease the values by some number
Bitmaps
- you can store byte arrays in redis and set/get their specific bits
- commands:
setbitandgetbit - this can be used for implementing Sketching Algorithms, e.g. Bloom Filters
Hashes
- hashes in redis are associative arrays
- to store a hash, not a string, use
hsetandhget hset hashname field 30is approximately equal tohashname[field] = 30- if hash
hashnamedoesn’t exist, it will be created. If exists, butfielddoesn’t,fieldwill be created hgetall hashnamereturns all fields of the hashhashnamehdel hashname fieldremovesfieldfromhashname
Lists
- lists keep an array of values associated with a specific key
lpush listname new_valueadds the new value to the front oflistnameltrim listname 0 49removes first 50 values from the listlrange listname 0 9returns first 10 values
Sets
Unordered sets
sadd set1 val1 val2 val3adds values “val1” “val2” “val3” to setsetkeysmember set1 val1sinter set1 set2intersection of two setsset1andset2sinterstore set1 set2 new_setstore instersection of two sets in a new setnew_set
Ordered sets
- the same commands, but they start with “z”:
zadd,zmember, …
Usage Patterns
- in redis it’s fine to issue several requests (“round trips” to db) - because it’s so fast
Avoiding Duplication
- suppose users can log in using their ids or emails - so you have two keys for the same user
- how can you avoid storing the same user twice?
- first, keep the user data at
user:id, e.g.users:9001 - use hash to lookup the id for email:
hget user:emails ololo@mlwiki.org
Expiration
- you can set time to live of a key
expire key 30where 30 is the time to live in secondsexpireat key unix_timestampttl keyto see how much time thekeyhas left to live
Publish/Subscribe
- it’s easy to implement the Publish-Subscribe Model in redis - which makes it a lightweight message queue
blpopandbrpop- return and remove first (last) element of a list - or blocks until something is avialable- can use it to implement a Queue
- also can use
subscribe q_idandpublish q_it message
A P/S model implemented in Scala: https://gist.github.com/debasishg/7056696
Lua Scripting
- finally, it’s possible to write stored procedures in Lua
Sources
- Little Redis Book: https://github.com/karlseguin/the-little-redis-book