In this age of modern technology, no one likes to wait for a long time for their search results or Twitter feeds to show up. Similarly, if you’re playing a game you would want to view the leaderboard updated in real-time. All these needs require a solution that is highly performant and rapid, which helps us in accessing data faster. Redis is a solution that makes all of this possible.
What is Redis?
Redis actually stands for Remote Dictionary Server. It is basically a data-structure store that stores the data in the primary memory of a computer system. It uses key-value pairs to store the data, which is just like a HashMap in Java, a dictionary in Python, or an object in JavaScript. This is also why it is sometimes referred to as a NoSQL database.
Redis is an open-source project with a flourishing community.
Features of Redis
- In-memory storage: Conventionally, all databases store and access data from hard disks and SSDs, which are secondary memory. Primary memory is faster than secondary memory, as it can be accessed directly by the processor. Since Redis stores its data on the primary memory, reading and writing are made faster than databases that store data on disks.
This is also why Redis is used as a cache in many applications, to provide results rapidly. But we could store our data directly on the primary memory or in the system cache, we won’t require Redis then, right?
Redis is so much more than ‘just a cache’, as you will see.
- Advanced Data Structures: Redis stores its data in a key-value pair and has the ability to store data using a variety of data structures like:
- Strings
- Lists
- Sets
- Sorted Sets
- Hashes
- Bit Arrays
- HyperLog Logs
- Streams
These data structures are not available in the standard system memory. Even Memcached, another popular in-memory key-value store, only supports Strings. As a Developer, you must have used some of these data structures, which also makes Redis easier to use and implement.
There is no serialization required here since the data is stored directly in the form of the data structures you are coding in.
- Persistence: Another disadvantage of directly using primary memory or system cache is that data is volatile. Data on primary memory gets cleared when the system is shut down.
Redis offers two mechanisms to prevent this – Snapshots and AOFs (Append Only Files), which are basically backups of the Redis data-store on the secondary memory, so that if there is a system failure, it can recall the current state of the database.
- Replication: Redis uses master-slave (primary-replica) architecture, where data can be replicated to multiple replica servers. This boosts the read performance since requests can be split among different servers. It also helps in faster recovery in case the master or primary server experiences an outage.
- Huge Support: Being an Open Source project with a diverse community, Redis has no technology constraint, as it is based on open standards and supports open data formats. It also supports a rich set of clients, with support in more than 40 Programming Languages.
Since Redis stores everything in-memory, in the form of a wide variety of data structures, and also provides persistence and the ability of replicating servers, it stands out when comparing to other databases/caches which cannot serve the same purpose as Redis.
Here is a quick comparison of Redis with other major databases and key-value stores.
Where is Redis being used?
Take a look at some prominent uses of Redis with real-world examples:
- Twitter: Caching with low latency
- Pinterest: Caching
- Dream11/My11Circle: Highly performant leaderboards with concurrent read and writes
- StackOverflow: Displaying top-voted answers on top
- Valorant: Load balancing across server instances
- Doordash: Machine Learning inference with low latency
- Home Depot: Real-time data analytics
- Message Broker: Pub/Sub model
- Session Store: Maintaining user sessions
When to avoid using Redis
- Sole database: Redis is an in-memory store and should not be used as the only database for large applications.
- Transactional server: Redis is not as secure as a real transactional server.
- Relational Database: Redis is a data structure server and does not provide a query language or support for relational algebra.
Alternatives to Redis
- Memcached: A caching alternative that provides multithreading.
- MongoDB: A document-oriented NoSQL database.
- RabbitMQ: A message broker that is more durable and preferred over Redis.
- Cassandra: A partitioned row store that is highly scalable and fault-tolerant.
Final thoughts
Redis is a versatile tool with multiple use cases in the real world. It provides high performance, low latency, and advanced data structures. Understanding how Redis is used in these applications can help you build efficient and scalable systems.