The Wayback Machine - https://web.archive.org/web/20260406215053/https://www.geeksforgeeks.org/system-design/introduction-to-redis-server/

Redis Introduction

Last Updated : 14 Mar, 2026

Redis (Remote Dictionary Server) is an in-memory database that stores data in RAM instead of disk, making it extremely fast. It is mainly used to cache frequently used data and reduce the load on the main database, which improves system performance and response time.

• Stores frequently accessed data so applications can retrieve it quickly without querying the main database.
• Used to store user sessions in web applications for fast login and authentication handling.
• Helps manage queues, leaderboards, and analytics in applications that require quick updates.

Example: An e-commerce website can use Redis to cache product details so users can load product pages much faster.

Working

Redis works as a caching layer between the client and the main database to provide faster data access and reduce database load.

• Request Handling: When a client requests data, the API gateway first checks Redis for the requested data.
• Fast Data Retrieval: If the data is available in Redis (cache hit), it is returned instantly without querying the database.
• Database Fallback: If the data is not found in Redis (cache miss), the system fetches it from the main database and stores it in Redis for future requests.

Example: When a user opens a frequently visited product page on an e-commerce site, Redis quickly returns the cached product details instead of querying the database every time

working_of_redis_server
Working of Redis

If Redis has the data (cache hit), it returns it quickly through the API Gateway to the client. If the data is missing (cache miss), Redis retrieves it from the database, stores it in the cache for future requests, and then passes it back through the API Gateway to the client. This flow speeds up response times and reduces the database load.

Before starting to learn Redis we need to install redis on our system.

Working Example:

Python
import redis
r = redis.Redis(host='localhost', port=6379, db=0)

r.set('name', 'Alia')
print(r.get('name').decode('utf-8'))  

r.set('name', 'Riya')
print(r.get('name').decode('utf-8')) 

r.delete('name')
print(r.get('name'))  

Output

code-1


Understand the above example:

import redis

This line imports the redis Python library, which allows you to talk to a Redis server from your Python code.

r = redis.Redis(host='localhost', port=6379, db=0)

Here, you are creating a Redis connection:

  • host='localhost': connects to Redis running on your own computer
  • port=6379: the default port Redis listens on
  • db=0: Redis supports multiple logical databases numbered from 0, and you’re using database 0

So this "r" variable is now your handle to talk to Redis.

r.set('name', 'Alia')

This line stores the key name with the value Alia in Redis.

print(r.get('name').decode('utf-8'))

This retrieves the value stored under the key name (which is Alia) from Redis.

  • r.get('name') returns the value in bytes format, e.g. b'Alia'
  • .decode('utf-8') converts those bytes to a normal string, so it prints Alia.

Note: In this example, Redis is integrated with Python but redis can also be implemented in other programming languages like Java, JavaScript (Node.js), Go, Ruby, C# (.NET), PHP, and many more, using their respective Redis client libraries.

Use Cases of Redis

Redis is useful when an application needs very fast data access and frequent database queries can slow down the system. It is commonly used to store temporary or frequently accessed data in memory so that the main database does not need to be queried every time.

• Caching Frequently Used Data: If an application repeatedly queries a database like MySQL for the same data, the results can be stored in Redis. Instead of waiting 100–1000 ms for a database response, the application can fetch the cached result from Redis in a few milliseconds.

• Session Management: Redis can store user sessions for web applications, allowing quick access to login or session data.

• Real-time Applications: It is useful for leaderboards, queues, notifications, and chat systems that require very fast updates.

Example: In a messaging application, Redis can store the last few messages of a conversation using its list data structure so that users can quickly see recent messages without repeatedly querying the main database.

Factors That Make Redis Fast

Redis is extremely fast because it stores data in memory (RAM) instead of reading from disk storage. Accessing data from memory is much quicker, which allows Redis to process requests with very low latency.

• In-Memory Storage: All data is stored in RAM, so read and write operations are much faster than disk-based databases.
• Single-Threaded Event Loop: Redis processes commands using a single-threaded architecture, which avoids the overhead of managing multiple threads and context switching.
• Efficient Data Structures: Redis uses optimized data structures like lists, sets, hashes, and sorted sets for quick data operations.
• Lightweight Communication Protocol: It uses a simple protocol called RESP (Redis Serialization Protocol) that enables fast communication between the client and the server.

Example: When an application requests cached product data from Redis, the response can be returned in microseconds, whereas querying a disk-based database may take milliseconds.

Read more here: Why Redis is so fast and popular?

Difference Between Redis Vs MongoDB

Both Redis and MongoDB are widely used databases, but there are some key differences between them that you should know:

MongoDB

Redis

Document-based NoSQL database

In-memory key-value store, NoSQL

Stores data as BSON documents (JSON-like)

Stores data as key-value pairs, strings, sets, lists, hashes, etc.

Disk-based, persistent storage

Primarily in-memory, but can persist data to disk (RDB, AOF)

Slower compared to in-memory stores like Redis

Extremely fast due to in-memory storage

Built-in persistence with automatic backups

Optional persistence with RDB snapshots or AOF logs

Supports complex querying with rich operators like $gt, $lt, $regex, etc.

Limited querying capabilities (basic key-value operations)

Ideal for large datasets, complex queries, and rich document structures

Ideal for caching, real-time analytics, messaging, and high-speed applications

More complex to manage and scale due to its rich features

Simple to use, mainly for high-speed, low-latency use cases

Note: Redis is much faster than MongoDB, especially for tasks that rely on simple, high-throughput operations. The exact margin can vary, but for basic operations, Redis is often 10x to 100x faster than MongoDB.

Comment
Article Tags:

Explore