Redis
Redis
●
● SADD: 186567.16 requests per second
● SPOP: 185873.61 requests per second
Scaling it up
● Default configuration:
○ Save after 900 sec (15 min) if at least 1 key changed
○ Save after 300 sec (5 min) if at least 10 keys changed
○ Save after 60 sec if at least 10000 keys changed
Virtual Memory
● Get/Set/Incr - strings/numbers
● Lists
● Sets
● Sorted Sets
● Hash Tables
● PubSub
● SORT
● Transactions
Note that If the locking client crashes that might cause some problems, but it can be solved
easily.
List operations
● Lists are your ordinary linked lists.
● You can push and pop at both sides, extract range, resize,
etc.
● Random access and ranges at O(N)! :-(
redis> LPUSH foo bar
(integer) 1
● BLPOP: Blocking POP - wait until a list has elements and pop them. Useful for realtime stuff.
redis> BLPOP baz 10 [seconds]
..... We wait!
Set operations
● Sets are... well, sets of unique values w/ push, pop, etc.
● Sets can be intersected/diffed /union'ed server side.
● Can be useful as keys when building complex schemata.
redis> SADD foo bar
(integer) 1
redis> SADD foo baz
(integer) 1
redis> SMEMBERS foo
["baz", "bar"]
//now we wait
.... redis> publish feed:joe "all your base are
<<<<<---------- belong to me"
1. "message" (integer) 1 //received by 1
2. "feed:joe"
3. "all your base are belong to me"
SORT FTW!
● Key redis awesomeness
● Sort SETs or LISTS using external values, and join values
in one go:
SORT key
SORT key BY pattern (e.g. sort userIds BY user:*->age)
SORT key BY pattern GET othervalue
● WATCH allows you to lock keys while you are queuing your
transaction, and avoid race conditions.
Gotchas, Lessons Learned
● Memory fragmentation can be a problem with some usage
patterns. Alternative allocators (jemalloc, tcmalloc) ease
that.
● vm.overcommit_memory = 1
#add a message
>>> client.hmset('messages:%s' % msgId, {'text': 'hello world', 'user': 1})
#distribute to followers
>>> followers = client.lrange('user:1:followers', 0, numFollowers)
● Queue Server
● resque - a large portion of redis' user base
Melt - My little evil master-plan
● We wanted freakin' fast access to data on the front-end.
● Central Id generator.
Melt - an example:
#syncing objects:
with MySqlStore:
users = Users.get({Users.id: Int(1,2,3,4)})
with RedisStore:
for user in users:
Users.save(user)
Redis' website:
http://redis.io