read

Redis - Redis is an open source, BSD licensed, advanced key-value store. Simple. I wouldn’t bother you too much. Think about it as Memcached on steroids. Redis is ‘insanely fast.’ - RAM is cheap these days.

I've heard so many good things about this awesome systems, untill last two weeks I haven't done anything with it. I was recently assigned the task of adding a caching layer to one of our internal tools and my boss specifically asked I use Redis. I smiled at myself - alas, I'll play with Redis in production.

Like I said earlier, Redis is a Key-Value store. Let's get our hands dirty.

Setting and retrieving a value is straight forward.

SET foo bar
GET foo 
bar

It doesn’t get any simpler

If you need to delete this data, simply pass the key as an argument to DEL

DEL foo.

Redis has tons of commands. You can find all of them here - http://redis.com/commands

Since the bulk of the data I was going to be storing were timestamp bound it was important I work with a command that support ranges and a sorted set was the best option. There are tons of sorted set commands but ZRANGEBYSCORE was a perfect fit.

Task 1. Store users based on the date the registered on the site.

ZADD   
Retrieving is simple:

ZRANGEBYSCORE   
 - A key that uniquely identified this operation. Think of the key as a table in a RDBMS
 - Start date
 - End date
</pre>

Live example:

ZADD users 20131001 Celestine:Omin:smartone@example.com
ZADD users 20131002 Mark:Cuban:smarttwo@example.com
ZRANGEBYSCORE users 20131001 20131002
[Celestine:Omin:smartone@example.com, Mark:Cuban:smarttwo@example.com]

If I needed every single item in this without considering the date range, I could use
ZRANGEBYSCORE users -inf +inf
Where -inf and +inf are min and max respectively.

P/S ZRANGEBYSCORE has a time complexity of O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)). Thank you.

I'll love to hear from you

Do you want to say hello? Email me - celestineomin@gmail.com

I tweet at @cyberomin

If you enjoyed this post, please consider sharing it.
comments powered by Disqus
Blog Logo

Celestine Omin


Published

Image

Celestine Omin

On Software, life and everything in-between

Back to Overview