Redis is often described as an in-memory database, but its usefulness goes far beyond simply storing key-value pairs.
It is commonly used for:
- Caching
- Sessions
- Rate limiting
- Queues
- Pub/Sub
- Temporary data
- Distributed locks
Why Is Redis Fast?
Traditional databases usually need to access persistent storage.
Redis primarily operates in memory.
For example:
await redis.set("user:123", JSON.stringify(user));
Later:
const user = await redis.get("user:123");
Because the data is stored in memory, operations can be extremely fast.
Redis as a Cache
Suppose your application repeatedly requests a product:
GET /products/123
Without caching:
Application → Database → Application → User
With caching:
Application → Redis
↓
Cache Hit
The database doesn't need to process every request.
Cache Expiration
Cached data should often expire.
await redis.set(
"product:123",
JSON.stringify(product),
{ EX: 300 }
);
This stores the value for five minutes.
Expiration prevents stale data from remaining indefinitely.
Redis for Rate Limiting
Imagine an API allowing 100 requests per minute.
Redis can track request counts:
rate-limit:user123 = 87
Once the count exceeds the limit, the application can reject additional requests.
Don't Put Everything in Redis
Redis shouldn't automatically replace your primary database.
A relational or document database is usually responsible for durable application data.
Redis is often better suited for fast, temporary, or coordination-related data.
Final Thoughts
Redis is valuable because it solves several performance and distributed-system problems with a relatively simple interface.
Learning Redis is especially useful for backend developers working on production systems.
Top comments (0)