When to Redis
paperplanes.de
paperplanes.de
If you are interested check http://code.google.com/p/redis/wiki/CommandReference (All the commands prefixed with "Z" are about the Sorted Set data type). Btw both insert / remove / and update-score operations are O(Log(N)). Top-N items is O(N) since the list of items is already sorted.
Redis 1.1 will be released as stable in one or two months at max.
For data structures and algorithms geeks: the sorted data type of Redis is implemented with a skip list, hacked to actually build a doubly-liked list, with the backward pointers being only at level 0 (we have reverse range operations, ZREVRANGE). Every element is also taken inside an hash table, so that it's possible to update the score of an element already inside the sorted set in O(Log(N)) time because we can get the old score in O(1) from the hash table, and then find the element in the skip list.
Even when the scores are not distributed but there are large clusters of elements with the same score, the update is still O(Log(N)) thanks to some interesting trick.
For example, when parsing incoming data into a database, push incoming data onto one end of a Redis list, and have a seperate worker process popping them off the end and into CouchDB, MySQL or whatever.
The fact that Redis list operations are atomic mean that multiple worker processes (possibly on seperate physical boxes) can process jobs simultaneously.
Result: the internet is entirely decoupled from both the hard drive and the database, and extremely high performance is possible.
Twitter Streaming API, anybody?..
After this post, I had an hourlong discussion with my lead dev on swapping out memcached for Redis so we could use it for our job scheduler. This post makes an excellent, compelling case for Redis.
Just an interesting example of how two different evangelistic approaches can play out. I don't know if I'm representative of "normal people", though. (Don't say it.)
Watch out for the Python library though! It doesn't even try to sanitize key names so malformed names cause all sorts of problems (including executing arbitrary commands). Plus unicode strings with non-ascii chars cause it to blow up.
All very easy to fix of course, as soon as I get a chance I'll submit a patch if it hasn't already been done by someone else by then.
Btw the Ruby client lib is solid, like it appears to be the Java one. Still there are client libs that absolutely need to be improved.
Currently the Ruby one is as far as I know the only one supporting consistent hashing but probably this problem will be fixed with the introduction of a new daemon 'redis-cluster' that will work as a proxy taking care to deal with the hash ring in a transparent way.
Check the changed doctests for examples of why it was a bad idea not to sanitize key names and other params. Ignore the changed doctests involving Decimals, I had to change those to get them to pass on my machine but they're not related to me fixes.
Is one of the goals of the project to replace the need to use a database? Or will it always be more suitable cache or worker queue replacement?