The short answer is: because databases are optimized for correctness and durability, while Redis is optimized for speed.
Even if your database can answer a query quickly, repeatedly asking it for the same data wastes CPU, memory, disk I/O, and network bandwidth. A cache avoids that work.
Here's an example.
Suppose you have a product page:
GET /products/123
Without a cache:
User
↓
App
↓
Database
If 10,000 people request that product in one minute:
- Database executes the same query 10,000 times.
- It repeatedly parses the query, looks up indexes, loads rows, and sends results.
- Your database becomes the bottleneck.
With Redis:
User
↓
App
↓
Redis
↓ (only if missing)
Database
The flow is:
- App checks Redis.
- If the product exists in Redis (a cache hit), return it immediately.
- If not (a cache miss):
- Query the database.
- Store the result in Redis for, say, 5 minutes.
- Return the result.
Now those 10,000 requests might become:
- 1 database query
- 9,999 Redis lookups
Why is Redis so much faster?
Redis stores everything in memory (RAM).
Typical latency:
| System | Approximate latency |
|---|
| Redis | 0.1–1 ms |
| SQL database | 5–50+ ms |
The database often has to:
- Read indexes
- Check locks
- Execute the query planner
- Maintain transactions
- Ensure consistency
- Possibly read from disk
Redis simply does:
dictionary["product:123"]
which is essentially a hash table lookup.
Why not just keep everything in the database?
Imagine a homepage that shows:
- Trending posts
- User profile
- Notifications
- Recommendations
If 100,000 users refresh every minute, that's potentially millions of identical queries.
Caching lets the database focus on:
- writes
- complex queries
- transactions
instead of repeatedly serving the same data.
Doesn't the database cache things too?
Yes.
Most databases already cache:
- frequently accessed pages
- indexes
- query execution plans
But that doesn't eliminate the cost of:
- executing the query
- transaction handling
- concurrency control
- generating the result
Redis skips almost all of that by storing the final value ready to return.
Why not cache inside the application?
You can, and many applications do.
App
├── Memory cache
└── Database
The problem appears when you have multiple application servers:
App 1
/
Users --- App 2
\
App 3
Each server has its own memory.
If App 1 caches a value, App 2 doesn't know about it.
Redis acts as a shared cache:
App 1
/
Users --- Redis
\
App 2
\
App 3
Now every server sees the same cached data.
Are there downsides?
Yes.
- Cached data can become stale.
- You have to decide when to expire or invalidate cached values.
- Redis uses RAM, which is more expensive than disk.
- Cache invalidation is one of the trickier parts of system design.
Rule of thumb
Use the database as the source of truth.
Use Redis to store:
- frequently read data
- expensive query results
- sessions
- rate-limiting counters
- leaderboards
- temporary data
A common pattern looks like this:
Read request
│
▼
Check Redis
│
┌────┴────┐
│ Hit │ Miss
▼ ▼
Return Query DB
data │
▼
Store in Redis
│
▼
Return data
This architecture dramatically reduces load on the database and improves response times, especially for applications with many more reads than writes.