Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
What Can We With Redis ?
You can run atomic operations on these types –
- like appending to a string;
- incrementing the value in a hash;
- pushing an element to a list;
- computing set intersection, union and difference
- getting the member with highest ranking in a sorted set.
Use Cases for Redis
- Session Cache
One of the most apparent use cases for Redis is using it as a session cache. The advantages of using Redis over other session stores, such as Memcached, is that Redis offers persistence. While maintaining a cache isn’t typically mission critical with regards to consistency, most users wouldn’t exactly enjoy if all their cart sessions went away, now would they? Luckily, with the steam Redis has picked up over the years, it’s pretty easy to find documentation on how to use Redis appropriately for session caching. Even the well-known ecommerce platform Magento has a plug in for Redis!
- Full Page Cache (FPC)
Outside of your basic session tokens, Redis provides a very easy FPC platform to operate in. Going back to consistency, even across restarts of Redis instances, with disk persistence your users won’t see a decrease in speed for their page loads—a drastic change from something like PHP native FPC. To use Magento as an example again, Magento offers a plugin to utilize Redis as a full page cache backend.
Taking advantage of Redis’ in memory storage engine to do list and set operations makes it an amazing platform to use for a message queue. Interacting with Redis as a queue should feel native to anyone used to using push/pop operations with lists in programming languages such as Python. If you do a quick Google search on “Redis queues,” you’ll soon see that there are tons of open-source projects out there aimed at making Redis an awesome backend utility for all your queuing needs.
Redis does an amazing job at increments and decrements since it’s in-memory. Sets and sorted sets also make our lives easier when trying to do these kinds of operations, and Redis just so happens to offer both of these data structures.
Last is Redis’s Pub/Sub feature. The use cases for Pub/Sub are truly boundless. I’ve seen people use it for social network connections, for triggering scripts based on Pub/Sub events, and even a chat system built using Redis Pub/Sub!