The Good and the Bad of Redis In Memory Database

The Good and the Bad of Redis In-Memory Database

When Salvatore Sanfilippo created Redis in 2009 to scale up his startup, he couldn't have predicted that it would become one of the most widely used databases, powering everything from real-time analytics to gaming leaderboards.

Today, over 30,000 businesses, including British Airways, HackerRank, and MGM Resorts International, rely on Redis for data management.

But is it always the right choice? Let’s explore its pros and cons to see where Redis shines—and where it might not be the best fit.

What is Redis?

Redis, short for REmote DIctionary Server, is a NoSQL in-memory key-value data store. Unlike traditional database management systems that save data permanently on hard disk drives (HDDs) or solid-state drives (SSDs), Redis keeps everything in temporary or random access memory (RAM), which makes it much quicker to retrieve data. The following numbers put Redis’s approach into perspective:

  • RAM access speed is around 100 nanoseconds.
  • SSDs—the fastest available hardware for permanent storage—let you fetch data in 100 microseconds at best (1,000 times slower than RAM).
  • Older HDDs need 10 milliseconds for the same operation on average (100,000 times slower than RAM).
Key-value format
Key-value format

Redis saves data in a key-value pair format, meaning each piece of data is associated with a unique key that acts as an identifier. Keys in Redis are always strings, while values can be different data types. For example, a key could be "user:123:name", and the value could be "John Doe."

Four ways to use Redis: Redis Community Edition vs Redis Stack vs Redis Software vs Redis Cloud

When talking about Redis, it's important to understand its core offerings — Redis Community Edition, Redis Stack, Redis Software, and Redis Cloud.

Redis Community Edition is the foundational version of Redis. It is available for free download to developers and customers but not for commercial usage by cloud service providers. It is a good fit for teams that have a tight budget and are capable of managing Redis themselves.

Redis Stack builds on the free version by integrating additional modules that extend its capabilities. These include modules like

Redis Stack can also be used as a vector database, which stores and retrieves mathematical representations of complex data like text, images, and audio. This is especially useful in artificial intelligence and machine learning applications. For example, it can improve recommender systems by quickly finding items similar to a user's preferences, enhance search results by understanding the meaning behind words, and detect unusual patterns in data for security purposes.

Redis Software is Redis’s commercial offering that’s designed to meet the demands of large-scale applications. It includes all the features of Redis Stack and adds enterprise-grade capabilities like automated scaling, high availability, and advanced security. It’s self-managed and can be deployed on premises, or on private or public cloud.

Redis Cloud is a fully managed DBaaS offering from Redis. It is designed to provide Redis's speed and reliability in a cloud environment and integrates with major cloud providers.

What is Redis used for?

Redis is primarily used for caching frequently requested information to reduce the load on the main database and boost an app’s performance. In this scenario, the system first checks popular content like product pages or user profiles in the Redis cache. If it’s not there, queries the primary store, retrieves the data, and puts it in the cache for future use.

How Redis caching works
How Redis caching works

Other popular use cases include but are not limited to the following.

Message broker. Redis can act as a middleman, helping applications exchange data. Instead of talking directly to each other, systems send messages to Redis, which delivers them to the proper recipients. The platform supports asynchronous (Publish/Subscribe or Pub/Sub) communication, enabling real-time notifications and event-driven architectures.

Data Streaming, ExplainedPlayButton

Data Streaming, Explained

Session storage. Redis is a good choice for storing temporary session data — like their login status and shopping cart info. It prevents users from having to log in again every time they refresh a page or move between different sections of the app.

Streaming data processing and real-time analytics. High processing speed makes Redis ideal for use cases like monitoring website traffic, tracking user activity, analyzing financial transactions, and detecting fraud in real-time. However, since Redis is an in-memory store, it is best suited for short-term analytics and is often combined with long-term storage solutions.

How does Redis work? Key features and characteristics

Let’s learn how Redis works by exploring its features and characteristics.

Persistence

In the case of an in-memory store, all data would normally disappear when the server shuts down, crashes, or reboots. To prevent this, Redis uses persistence to periodically write data to a non-volatile, permanent storage medium.

Redis persistence

Redis persistence

There are 3 main persistence methods:

  • RDB (Redis database backup), which saves snapshots of the dataset to disk at different intervals;
  • AOF (append-only file), which logs every write operation to a file that can be replayed to restore the dataset; and
  • the hybrid approach, which combines RDB and AOF to give you the speed of snapshots and the safety of logging every change.

When Redis restarts after a failure, it loads the last saved data from the persistent storage back into memory. This ensures the data is not lost and the last known state is recovered in case of disruptions.

Replication

Replication is a process of creating multiple copies (replicas) of the same data to ensure its availability and safety. By default, Redis uses asynchronous replication: Data is first written to a master (primary) server or node and later copied to so-called replica nodes that handle read requests from database users.

While this ensures high performance, there’s a risk: If the primary node crashes before replication is completed, the data could be lost. To reduce this risk, Redis enables the optional synchronous scenario (when the data is copied to the master and replicas simultaneously) with the WAIT command.

Single-threaded model

Redis uses a single-threaded model to handle client requests. When people hear this, their initial reaction is concern over potential performance limitations, as single-threaded architectures experience bottlenecks. However, Redis handles this in two ways.

The event-driven model involves listening to incoming requests and processing them asynchronously in a highly optimized loop. This allows Redis to handle millions of operations per second without being slowed down.

I/O multiplexing helps Redis manage multiple requests simultaneously without creating a separate thread for each connection, which would introduce CPU overhead. Instead of waiting for one request to be served before handling another, Redis’s loop oversees multiple input/output operations, picks the one ready for processing, sends it to the CPU, and immediately moves to the next operation ripe for action, before a response to the previous request come back.

Eviction policies

When Redis runs out of memory, it needs to decide which data to remove in order to free up space. This is where eviction policies come into play. Redis allows you to choose from different eviction policies, including the following.

  • LRU (Least recently used) removes the least-accessed keys first.
  • LFU (Least frequently used) removes items that are used the least.
  • TTL (time-to-live) automatically deletes data that has expired.
  • Random evicts random keys when memory is full.

While these are the most common ones, other eviction policies also exist, and they all have their use cases, pros, and cons.

Pros of Redis

Why do so many teams use Redis for their applications? Let’s find out the advantages  that have made this database highly loved.

Easy to learn and use

Redis is relatively easy to learn and set up, even for developers who are new to NoSQL databases. It has a simple command-line interface, intuitive syntax, and a straightforward configuration process.

High performance and speed

As we said, one of Redis’s biggest advantages is its speed. Unlike traditional databases that store data on disk, Redis keeps and caches everything in memory (RAM). This allows for reading and writing data in nanoseconds, making the platform much faster than databases that rely on SSDs or hard drives.

Versatile data structures

Unlike simple key-value stores, Redis supports over 20 data types, including

  • strings that store a series of bytes, including texts and binary data. It's ideal for caching web pages and counting operations;
  • lists, ordered collection of strings, used for implementing message queues and task scheduling;
  • sets, unordered collections of unique strings that are useful for tasks like tagging, recommendations, and real-time analytics;
  • sorted sets, which are sets with scores for ranking. They’re great for leaderboards, priority queues, and time series data.

With Redis, businesses are empowered to choose the data types that fit their specific needs. For instance, a social media platform can use lists to store recent notifications, sorted sets to rank trending posts, and strings to manage user sessions — all within the same system.

Read our dedicated article to learn about different fundamental data structures that exist.

Multi-language support

Redis provides client libraries for connecting with different pogramming languages, including Python, JavaScript, Go, and Java. Besides the official client libraries, there are also third-party libraries created by the community.

Scalability

Redis can scale horizontally with Redis Cluster, a distributed setup that enables the database to handle large workloads.  Instead of storing all data on a single server, Redis Cluster divides data into slots and distributes them across different nodes. It ensures Redis can maintain performance and reliability regardless of potential increase in workloads.

Lua scripting support

Redis has built-in support for Lua scripting, which allows developers to run custom code directly within the Redis server. Instead of sending multiple commands to Redis and waiting for each one to execute, you can group several commands together into a single custom Lua script and run them all at once. This makes operations more efficient and faster.

Active community

Redis is supported by a strong open-source community that continuously improves its features, fixes bugs, and boasts a large collection of tutorials, libraries, frameworks, and plugins that enhance its capabilities and simplify integration with other technologies.

In terms of the community size, the proof is in the pudding — as they say. The Redis subreddit has over 8,000 members, while its GitHub repo has over 67,000 stars. There are also 25,000+ Redis-related questions, the Discord server with 15,000+ members, and the YouTube channel with 28,000+ subscribers. The different channels and the interactions ongoing in them show how vibrant Redis’s community is.

Comprehensive documentation

Redis provides well-structured documentation that caters to developers of all skill levels. From installation steps for different environments to tutorials, examples, and explanations of key concepts and features, Redis’s official docs are extensive and make a great source for further learning.

Cons of Redis

Redis, like every tool, has its imperfections. Let’s explore some of these weaknesses.

Memory-intensive and not ideal for large datasets

While Redis’s approach to storing data in RAM contributes to its speed, this has its consequences. RAM is significantly more expensive than disk storage, which means that using Redis for large datasets can become expensive, especially when scaling up.

Companies that store and process terabytes of data must make significant investments when working with Redis. This is why it’srarely used as a standalone solution and is often paired with other databases to balance performance and cost.

Manual memory management

Redis does not automatically manage memory like relational databases do. Developers must manually configure eviction policies to decide what happens when memory is full. This disadvantage is addressed once you move to Redis Cloud since it’s a fully-managed Redis service.

Potential data loss

We’ve already explained earlier that unlike disk-based databases, where data is written to persistent storage immediately, Redis keeps everything in RAM, meaning that if the server unexpectedly crashes, all the data may be lost.

Though persistence options like RDB and AOF help reduce this risk, they are not foolproof. For example, while RDB persistence takes a snapshot of your dataset and writes it to disk at intervals, all changes made since the last backup will be lost if Redis is disabled in between.

Redis vs alternatives

We’ve seen Redis's strengths and weaknesses. It's not the best fit for every situation, and some cases call for alternatives. Let’s explore these other solutions.

Redis vs alternatives

Redis vs alternatives

Redis vs Memcached

Redis and Memcached are both in-memory key-value data stores. However, they serve different needs. Memcached lacks the rich data structures that Redis has and only supports strings, making Redis a better option for applications that need complex data manipulation. Memcached also doesn’t have built-in persistence, meaning data is lost when the server is restarted.

Unlike Redis, which is suitable for diverse use cases — real-time analytics, pub/sub messaging, session storage, etc. — Memcached is designed for basic key-value caching in web applications where persistence is not required. It is one of the most straightforward alternatives to Redis.

Lastly, while Redis relies on the single-threaded model, Memcached uses multi-threading, which makes Memcached better suited for simple read-heavy workloads.

Redis vs MongoDB

Redis and MongoDB are widely dissimilar, except for one common characteristic: they are both NoSQL databases. Except for that, there are many differences. For one, Redis is a key-value store, while MongoDB is a document-based database that keeps information in the BSON format — a flexible, JSON-like structure.

MongoDB shines when handling massive datasets. In contrast, Redis is optimized for speed and real-time operations but is limited by available RAM.

Although built for different purposes, Redis and MongoDB are sometimes paired together to maximize the speed and efficiency of a NoSQL database. Redis handles frequently accessed data, while MongoDB stores long-term records and supports complex queries, providing the best of both worlds.

Redis vs DynamoDB

DynamoDB is a managed key-value and document database offered by AWS, meaning  the cloud platform handles all operational aspects and automatically provides persistence and scaling without requiring manual configuration. Both tools offer high performance, but DynamoDB stores data in SSDs, making it more cost-efficient for large datasets.

DynamoDB provides limited querying capabilities and may not be ideal for complex data analysis, especially when dealing with large datasets.

Redis vs Kafka

While Redis includes messaging capabilities, Kafka is a message broker that’s designed specifically for event streaming and log processing. It is best suited for high-throughput streaming and large-scale data pipelines.

Note that you can use both tools together, with Redis acting as the caching store and Kafka as the message broker. For example, a chat application may employ the Redis cache to store active messages for quick access, while Kafka handles event streaming.

Redis vs. AWS ElastiCache

AWS ElastiCache is a fully managed caching service that supports Redis and Memcached. Redis and ElastiCache are both used for caching, real-time analytics, and message brokering.

With ElastiCache, AWS handles the majority of the management and maintenance. While this simplifies setup, it means less control and customization are available.

Redis products (Community Edition, Stack, and Software) are the right pick if you want to manage the infrastructure yourself and need total control and flexibility over configuration, customization, and deployment. However, note that integration and monitoring will require additional effort.

Getting started with Redis

If you're interested in mastering Redis, here are some resources to help you get started.

Documentation. Redis has excellent official documentation covering everything from basic commands to advanced concepts like replication, clustering, and persistence. The Getting Started section is a great place to begin studying the platform.

GitHub repositories. Redis has an official GitHub repository where you can explore its source code and contribute to its development. Also, explore the awesome-redis repo, a curated list of Redis-related tools, libraries, and tutorials from creators in its ecosystem.

Community. Redis has an active and supportive community where you can ask questions and learn from others. You can take advantage of Redis’ Discord server, Stack Overflow, and the Redis subreddit.

Books. If you enjoy learning through books, here are some excellent resources:

  • Redis in Action by Josiah L. Carlson
  • Redis Essentials by Maxwell Dayvson Da Silva
  • The Little Redis Book by Karl Seguin

Online courses. Several online resources can help you learn Redis, whether you're a beginner or an advanced user. Redis University offers free courses on Redis fundamentals that cover all you need to know about the technology. Also, YouTube has various tutorials, like Traversy Media's Redis Crash Course.

This post is a part of our “The Good and the Bad” series. For more information about the pros and cons of the most popular technologies, see the other articles from the series:

The Good and the Bad of Full Stack JavaScript Development

The Good and the Bad of Express.js Web Framework

Comments