Redis Introduction (original) (raw)
Last Updated : 17 Apr, 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, improving performance and response time.
- Used to store user sessions for fast authentication and helps manage queues, leaderboards, and analytics in applications requiring quick updates.
**Example: An e-commerce website can use Redis to cache product details so users can load product pages much faster.
Real-World Applications
Redis is widely used by large-scale applications to handle high-speed data access, real-time processing, and efficient caching.
- **Amazon & Flipkart: Use Redis to cache product details, prices, and user sessions, ensuring fast page loads and smooth checkout during high traffic sales.
- **Netflix: Uses Redis for caching frequently accessed content data and managing real-time user sessions to deliver a seamless streaming experience.
- **Facebook & Instagram: Use Redis to handle real-time notifications, feeds, and user activity for fast and responsive interactions.
- **Uber: Uses Redis for real-time location tracking, ride matching, and surge pricing calculations.
Working
Redis acts as a caching layer between the database and the client to speed up data access and reduce the load on the main database. When a client asks for data, the API Gateway forwards the request to Redis.
1. Request Handling
When a client sends a request, it is first routed through the API Gateway. The API Gateway checks Redis (cache) to see if the requested data is already available.
2. Cache Hit
If the data is found in Redis, it is immediately returned to the client. This avoids querying the main database and significantly improves response time.
3. Cache Miss
If the data is not present in Redis, the request is forwarded to the main database. The database processes the request and returns the required data to the application.
4. Cache Update
After fetching data from the database, it is stored in Redis for future use. This ensures that subsequent requests for the same data can be served faster.
5. Response to Client
The final response is sent back to the client through the API Gateway. The data may come from either Redis (cache) or the main database depending on availability.
**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
_Before starting to learn Redis we need to install redis_on our system.
**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

**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 computerport=6379: the default port Redis listens ondb=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.
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,gt, 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 significantly faster than MongoDB for simple, high-throughput operations—often around 10x to 100x faster, depending on the use case.