Redis (Remote Dictionary Server) is an in-memory data store that provides low-latency data access. It is commonly used for caching, sessions, queues, leaderboards, messaging, counters, and other workloads that require fast data access.
- Fast Data Access: Stores frequently accessed data in memory for quick retrieval.
- Rich Data Structures: Supports strings, hashes, lists, sets, sorted sets, streams, and other structures for different application needs.
Example: An e-commerce website can use Redis to cache frequently accessed product details so product pages can be served with lower latency.
How Redis Works as a Cache
Redis can act as a caching layer between an application and its primary database. When a client requests data, the application checks Redis before querying the database.

The process follows these steps:
- The client sends a request to the application.
- The application checks Redis for the requested data.
- If the data is available in Redis, it is returned without querying the database.
- If the data is missing or expired, the application retrieves it from the primary database.
- The retrieved data is stored in Redis for future requests.
- The application returns the requested data to the client.
Redis Example Using Python
Redis can be accessed through different programming languages. The following example uses Python to perform basic operations such as storing, retrieving, updating, and deleting data.
Step 1: Install Redis and Python Client
Install and start a Redis server, then install the redis Python library:
pip install redis
The following example assumes Redis is running locally on port 6379.
Step 2: Write the Python Code
import redis
# Connect to Redis
r = redis.Redis(
host="localhost",
port=6379,
db=0,
decode_responses=True
)
# Store data
r.set("name", "Alia")
print("Stored:", r.get("name"))
# Update data
r.set("name", "Riya")
print("Updated:", r.get("name"))
# Set a key with expiration
r.set("session:101", "active", ex=300)
print("Session:", r.get("session:101"))
# Delete data
r.delete("name")
print("After deletion:", r.get("name"))
Output

Explanation:
- Connect to Redis: redis.Redis() configures a connection to the local Redis server.
- Store and Retrieve: set() stores a key-value pair, and get() retrieves its value.
- Update Data: Calling set() on an existing key replaces its previous value.
- Set Expiration: The ex=300 argument sets the key's expiration to 300 seconds.
- Delete Data: delete() removes the specified key, after which get() returns None.
The decode_responses=True option converts Redis responses from bytes to Python strings.
Common Redis Data Structures
Redis supports multiple data structures that help applications store and process different types of information efficiently.
Data Structure | Description | Common Use Case |
|---|---|---|
Strings | Store text, numeric values, and binary data | Caching, counters |
Hashes | Store field-value pairs | User profiles, objects |
Lists | Store ordered sequences of elements | Queues, recent activity |
Sets | Store unique, unordered elements | Tags, membership checks |
Sorted Sets | Store unique members with numeric scores | Leaderboards, rankings |
Streams | Store append-only event records | Messaging, event processing |
Redis also provides specialized data types for use cases such as JSON documents, geospatial data, and probabilistic counting.
Redis Expiration and TTL
Redis supports Time to Live (TTL), which allows keys to expire automatically after a specified duration. It is particularly useful for temporary information that should not remain stored indefinitely.
r.set("otp:123", "758421", ex=300)
Here, ex=300 sets the expiration to 300 seconds. After the key expires, Redis treats it as unavailable and removes it through its expiration mechanisms.
Common applications of TTL include
- Session Expiration: Limits how long temporary session keys remain valid.
- Temporary Caching: Automatically expires cached data after a defined interval.
- Rate Limiting: Supports time-based request counters and rate-limit windows.
- Verification Codes: Expires OTPs after their validity period.
Note: TTL expiration and memory eviction are different mechanisms. TTL removes keys based on their configured expiration, while eviction frees memory according to a selected memory policy.
Redis Eviction Policies
Redis eviction policies determine what happens when Redis reaches its configured memory limit and additional memory is required.
- LRU-Based Eviction: Removes less recently used keys. Redis provides policies that apply LRU eviction to all keys or only keys with expiration times.
- LFU-Based Eviction: Removes less frequently accessed keys based on usage frequency.
- TTL-Based Eviction: Removes keys with expiration times, generally favoring keys that are closer to expiration.
- Random Eviction: Removes keys randomly, either from all keys or only expiring keys depending on the selected policy.
- No Eviction: Rejects write operations that require additional memory instead of removing existing keys.
Choosing an eviction policy
- Use allkeys-lru when recently accessed data is more likely to be requested again.
- Use allkeys-lfu when frequently accessed data should remain cached.
- Use volatile-ttl when keys with shorter remaining lifetimes are preferred eviction candidates.
- Use noeviction when automatically discarding stored keys would be inappropriate.
The choice depends on access patterns, memory requirements, and whether data can be safely recreated after eviction.
Why Redis Is Fast
Redis provides low-latency data access through its memory-oriented architecture and efficient command-processing mechanisms.
- In-Memory Storage: Keeps active data in RAM, reducing the need for disk access during normal operations.
- Efficient Data Structures: Uses optimized structures for common operations such as lookup, insertion, and updates.
- Mostly Single-Threaded Command Execution: Processes commands primarily through an event-driven execution model, reducing synchronization overhead.
- Lightweight Protocol: Uses Redis Serialization Protocol (RESP) for client-server communication.
- Efficient Network Handling: Supports pipelining and optimized network I/O to reduce communication overhead.
Redis Persistence
Although Redis primarily stores data in memory, it provides persistence mechanisms to help recover data after restarts.
- RDB (Redis Database): Creates point-in-time snapshots of the dataset and saves them to disk.
- AOF (Append Only File): Records write operations so Redis can reconstruct the dataset during recovery.
Redis can use RDB, AOF, or both, depending on application requirements. Persistence improves recoverability, but the amount of potential data loss depends on the configured durability settings.
Real-World Applications
Redis is commonly used in applications that require fast data access, real-time processing, and low-latency operations.
- E-commerce: Caching product information, shopping carts, and session data.
- Social Media: Managing feeds, notifications, counters, and real-time activity.
- Gaming: Building leaderboards, storing player state, and managing matchmaking queues.
- Financial Applications: Supporting rate limiting, real-time counters, and fast-access application data.
- Web Applications: Managing sessions and caching frequently accessed database results.
Advantages of Redis
- Provides low-latency read and write operations for many workloads.
- Reduces database load by caching frequently accessed information.
- Supports multiple data structures for different application needs.
- Offers TTL and eviction policies for managing temporary data.
- Supports replication, persistence, and high-availability configurations.
Limitations of Redis
- Memory-based storage can be more expensive than disk-based storage for large datasets.
- Cached data can become stale if applications do not manage cache invalidation correctly.
- Data loss is possible during failures, depending on persistence and replication settings.
- Memory limits and eviction policies require careful configuration.
- Redis is not a direct replacement for a relational database when complex joins and relational queries are required.