Memory usage of Redis hashes vs single keys
I am really a great fan for Redis. It just scales, and is so fast !
Well if you came to read this article, you might suffer from a memory problem on your linux server. So did I . Until I came to the Redis memory optimization tips article.
Redis is a great platform for storing key/value pairs, but memory usage can grow quickly if you don’t pay attention to the type of data you store.
I ran a few benchmarks last week to profile the use of redis hash sets vs single keys. Basically, I wanted to know if :
hset key k1 val1
hset key k2 val2
hset key k3 val3
would use less memory than :
set k1 val1
set k2 val2
set k3 val3
Given my tests, it is clear that the 1st option uses much less memory than the second one, especially on a 64-bit machine.I reached a >10x memory usage difference between the two tests.
The reason ?
Simple : small hashes with a few keys are stored internally using optimized structures which are way more efficient than single key/value pairs. Of course, when the number of key/value pairs becomes large in a redis hset, you should rework your datamodel in order to break these large hashtables into smaller ones.
So in the future, you should use redis hashes when possible !