Thursday, June 4, 2015

Redis hash - To be or not to be

The primary data structure used by Redis to optimize memory utilization is the hash. When storing your object in  a hash, you hash individual fields and store it with a hash. By using HMSET, you can set multiple fields using a hash in a single command. In implementation speak, the hash is actually a function that takes a parameter and hashes it - a good algorithm for the function would avoid hash collisions.

Now, why go through the extra effort of creating a hash and storing individual fields of the object with the hash, rather than storing the object as a single key value structure which makes it simpler to serialize/deserialize from JSON to your POCO objects? The answer is performance and yes, memory efficiency too.

With a hash, you are indexing the field and you can get to the value of a particular field of your object (stored with the hash) very quickly - not so if you are storing the whole object in just one KV pair.

So the bottom line is, if you need to read/write specific properties frequently within your object, consider storing it as a hash. If the object is retrieved most of the time as a whole, you can just store it as a simple string key value pair.

Note that the default C# driver as of today does not use hashes for storing objects by default.

For more reading on this topic, go to
http://redis.io/topics/memory-optimization

For an excellent blog on the five different data structures in Redis, go to
http://openmymind.net/2011/11/8/Redis-Zero-To-Master-In-30-Minutes-Part-1/

No comments:

Post a Comment