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-cli
to enter redis commands - if you need redis instance be outside, do
nano /etc/redis/redis.conf
and replacebind 127.0.0.1
tobind 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 getPONG
in return
Python
Python library:
sudo pip install redis
orsudo easy_install install redis
or
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 key
returns the lenth of the value associated with keykey
getrange key 0 10
returns the substring from 0 to 10 of the value stored bykey
append key value2
appends ‘value2’ to the current value stored bykey
Counters
- You can store counters in redis
incr key
increment the integer value stored inkey
, or create a new key with value 1decr key
decrement the valueincrby
anddecrby
are used to increase/decrease the values by some number
Bitmaps
- you can store byte arrays in redis and set/get their specific bits
- commands:
setbit
andgetbit
- 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
hset
andhget
hset hashname field 30
is approximately equal tohashname[field] = 30
- if hash
hashname
doesn’t exist, it will be created. If exists, butfield
doesn’t,field
will be created hgetall hashname
returns all fields of the hashhashname
hdel hashname field
removesfield
fromhashname
Lists
- lists keep an array of values associated with a specific key
lpush listname new_value
adds the new value to the front oflistname
ltrim listname 0 49
removes first 50 values from the listlrange listname 0 9
returns first 10 values
Sets
Unordered sets
sadd set1 val1 val2 val3
adds values “val1” “val2” “val3” to setsetkey
smember set1 val1
sinter set1 set2
intersection of two setsset1
andset2
sinterstore set1 set2 new_set
store 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 30
where 30 is the time to live in secondsexpireat key unix_timestamp
ttl key
to see how much time thekey
has left to live
Publish/Subscribe
- it’s easy to implement the Publish-Subscribe Model in redis - which makes it a lightweight message queue
blpop
andbrpop
- 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_id
andpublish 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