Learning about Redis through writing a Twitter clone
Redis is a fast key-value database by Salvatore Sanfilippo with some new ideas. Sometimes it’s called a ‘data-structures database’ because it supports sets and lists. I spent a few hours this morning writing a Twitter clone that uses Redis as its datastore, which is now online at http://retwisrb.danlucraft.com. The code is available on Github.
The lists feature of Redis meant that it was not necessary anywhere in the application to serialize a datastructure into a stored value. This is a tremendous win. I don’t have much experience of writing applications against key-value databases, but I worry that serialization would cause headaches.
I didn’t find a use for the Set structure, simply because I couldn’t see a set of data that I wouldn’t at some point want to page over. The list of a user’s followers was the closest I came. The downside of using a set is that in order to paginate the followers the entire set would have to be sorted first. The downside of using a list is that membership checking (is User A following User B?) requires list traversal by Redis.
There is a SORT command, so sorting sets can be done in the database layer, but I have no desire to sort this data (which could be massive) just to iterate over it in a consistent order. I went with a list for now, but you could use a set and a list to support both types of usage, at the cost of maybe twice the memory usage. I’d like to have an ordered Set structure available, if it could be done in a fast way.
I wonder if a Set and List is all we’ll get, or if Salvatore is planning more structures. Could you implement Trees in any kind of sensible way? Arrays? Would they be useful?
Anyway, I encourage you to try Redis out soon.blog comments powered by Disqus