What is Redis and what do I use it for?

If you can map a use case to Redis and discover you aren’t at risk of running out of RAM by using Redis there is a good chance you should probably use Redis.

It’s a “NoSQL” key-value data store. More precisely, it is a data structure server.

Not like MongoDB (which is a disk-based document store), though MongoDB could be used for similar key/value use cases.
The closest analog is probably to think of Redis as Memcached, but with built-in persistence (snapshotting or journaling to disk) and more datatypes.

Those two additions may seem pretty minor, but they are what make Redis pretty incredible. Persistence to disk means you can use Redis as a real database instead of just a volatile cache. The data won’t disappear when you restart, like with memcached.

The additional data types are probably even more important. Key values can be simple strings, like you’ll find in memcached, but they can also be more complex types like Hashes, Lists (ordered collection, makes a great queue), Sets (unordered collection of non-repeating values), or Sorted Sets (ordered/ranked collection of non-repeating values).

This is only the tip of the Redis iceberg, as there are other powerful features like built-in pub/sub, transactions (with optimistic locking), and Lua scripting.

The entire data set, like memcached, is stored in-memory so it is extremely fast (like memcached)… often even faster than memcached. Redis had virtual memory, where rarely used values would be swapped out to disk, so only the keys had to fit into memory, but this has been deprecated. Going forward the use cases for Redis are those where it’s possible (and desirable) for the entire data set to fit into memory.

Redis is a fantastic choice if you want a highly scalable data store shared by multiple processes, multiple applications, or multiple servers. As just an inter-process communication mechanism it is tough to beat. The fact that you can communicate cross-platform, cross-server, or cross-application just as easily makes it a pretty great choice for many many use cases. Its speed also makes it great as a caching layer.

4.5/5 - (2 votes)