Redis Cache in System Design
Learn Redis Cache from a System Design perspective. This guide explains Redis architecture, data structures, caching strategies, Spring Boot integration, distributed caching, Redis Cluster, Redis Sentinel, Pub/Sub, distributed locks, rate limiting, and real-world implementations used by Amazon, Netflix, Uber, and Banking systems.
Introduction
Imagine your Spring Boot application receives:
- 5 Million API Requests/day
- 200,000 Product Searches/hour
- 50,000 Login Requests/hour
Without caching, every request hits the database.
Client
↓
Spring Boot
↓
PostgreSQL
↓
Response
Problems:
- High database CPU usage
- Increased response time
- Expensive database scaling
- Connection pool exhaustion
Modern companies solve this using Redis.
Redis is one of the fastest in-memory databases and caching platforms available today.
Companies using Redis include:
- Amazon
- Netflix
- Uber
- Airbnb
- GitHub
- Stack Overflow
Learning Objectives
After completing this article, you will understand:
- What is Redis?
- Why Redis?
- Redis Architecture
- Redis Data Structures
- Cache Strategies
- Redis Cluster
- Redis Sentinel
- Distributed Cache
- Pub/Sub
- Distributed Locking
- Rate Limiting
- Spring Boot Integration
- Real-World Examples
What is Redis?
Redis stands for:
Remote Dictionary Server
Redis is an in-memory key-value data store.
Unlike traditional databases,
Redis stores data in RAM, making it extremely fast.
Average latency
Database
↓
10-100 ms
Redis
↓
<1 ms
Why Redis?
Without Redis
flowchart LR
USERS["Users"]
APP["Spring Boot"]
DB["Database"]
USERS --> APP
APP --> DB
With Redis
flowchart LR
USERS["Users"]
APP["Spring Boot"]
REDIS["Redis Cache"]
DB["Database"]
USERS --> APP
APP --> REDIS
REDIS --> DB
Frequently accessed data is served directly from Redis.
Redis Request Flow
flowchart LR
CLIENT["Client"]
APP["Spring Boot API"]
REDIS{"Redis Cache"}
DB["Database"]
CLIENT --> APP
APP --> REDIS
REDIS -- "Cache Hit" --> CLIENT
REDIS -- "Cache Miss" --> DB
DB --> REDIS
REDIS --> CLIENT
Flow
- Check Redis.
- Cache Hit → Return data.
- Cache Miss → Query database.
- Store in Redis.
- Return response.
Redis Architecture
flowchart TD
USERS["Users"]
LB["Load Balancer"]
APP1["Spring Boot Instance 1"]
APP2["Spring Boot Instance 2"]
APP3["Spring Boot Instance 3"]
REDIS["Redis Cluster"]
DB["PostgreSQL"]
USERS --> LB
LB --> APP1
LB --> APP2
LB --> APP3
APP1 --> REDIS
APP2 --> REDIS
APP3 --> REDIS
REDIS --> DB
Redis becomes the shared cache for all application instances.
Cache Hit
flowchart LR
CLIENT["Client"]
APP["Spring Boot"]
CACHE["Redis Cache"]
RESP["Cached Response"]
CLIENT --> APP
APP --> CACHE
CACHE --> RESP
RESP --> CLIENT
Latency
<1 Millisecond
Cache Miss
flowchart LR
CLIENT["Client"]
APP["Spring Boot"]
CACHE{"Redis Cache"}
DB["Database"]
RESP["Response"]
CLIENT --> APP
APP --> CACHE
CACHE -- "Cache Miss" --> DB
DB --> CACHE
CACHE --> RESP
RESP --> CLIENT
Future requests are much faster.
Redis Data Structures
Redis supports many data structures.
| Data Structure | Use Case |
|---|---|
| String | Cache Objects |
| Hash | User Profiles |
| List | Recent Searches |
| Set | Unique Tags |
| Sorted Set | Leaderboards |
| Stream | Event Streaming |
| Bitmap | Online Users |
| HyperLogLog | Approximate Counting |
| Geo | Location Services |
String Example
Key
product:1001
↓
Value
iPhone 17
Most common caching type.
Hash Example
user:100
↓
name
John
↓
city
Dallas
Stores objects efficiently.
List Example
Recent Searches
Redis
↓
Spring Boot
↓
AWS
↓
Kafka
Set Example
Unique Skills
Java
Spring
AWS
Docker
Duplicates are automatically removed.
Sorted Set Example
Leaderboard
Alice
980
Bob
920
Charlie
880
Perfect for rankings.
Cache Aside Pattern
Most popular caching strategy.
flowchart TD
APP["Application"]
CACHE{"Redis Cache"}
DB["Database"]
RETURN["Return Data"]
APP --> CACHE
CACHE -- "Hit" --> RETURN
CACHE -- "Miss" --> DB
DB --> CACHE
CACHE --> RETURN
Flow
- Read from Redis.
- If missing, query database.
- Store result in Redis.
Read Through Cache
flowchart TD
APP["Application"]
CACHE{"Redis Cache"}
DB["Database"]
RESP["Return Data"]
APP --> CACHE
CACHE -- "Cache Hit" --> RESP
CACHE -- "Cache Miss" --> DB
DB --> CACHE
CACHE --> RESP
Application interacts only with Redis.
Write Through Cache
flowchart TD
APP["Application"]
CACHE["Redis Cache"]
DB["Database"]
RESPONSE["Return Success"]
APP --> CACHE
CACHE --> DB
DB --> RESPONSE
Both cache and database are updated together.
Advantages
- Strong consistency
Write Behind Cache
flowchart TD
APP["Application"]
CACHE["Redis Cache"]
RESPONSE["Return Success"]
WORKER["Background Worker"]
DB["Database"]
APP --> CACHE
CACHE --> RESPONSE
CACHE -. "Async Write" .-> WORKER
WORKER --> DB
Advantages
- Extremely fast writes
Disadvantages
- Possible data loss before persistence
Distributed Cache
flowchart TD
U["Users"]
LB["Load Balancer"]
APP1["Spring Boot 1"]
APP2["Spring Boot 2"]
APP3["Spring Boot 3"]
REDIS["Redis Cluster"]
DB["Database"]
U --> LB
LB --> APP1
LB --> APP2
LB --> APP3
APP1 --> REDIS
APP2 --> REDIS
APP3 --> REDIS
REDIS --> DB
Every application server shares the same cache.
Redis Cluster
Redis Cluster distributes data across multiple nodes.
flowchart TD
CLIENT["Client"]
CLUSTER["Redis Cluster"]
NODE1["Master Node 1"]
NODE2["Master Node 2"]
NODE3["Master Node 3"]
CLIENT --> CLUSTER
CLUSTER --> NODE1
CLUSTER --> NODE2
CLUSTER --> NODE3
Benefits
- Horizontal Scaling
- Fault Tolerance
- High Availability
Redis Sentinel
Redis Sentinel monitors Redis instances.
flowchart TD
S1["Sentinel 1"]
S2["Sentinel 2"]
S3["Sentinel 3"]
P["Primary Redis"]
R1["Replica 1"]
R2["Replica 2"]
S1 --> P
S2 --> P
S3 --> P
P --> R1
P --> R2
If the primary node fails,
Sentinel automatically promotes a replica.
Pub/Sub Messaging
Redis supports lightweight messaging.
flowchart LR
P["Publisher Service"]
R["Redis Channel"]
S1["Subscriber 1"]
S2["Subscriber 2"]
S3["Subscriber 3"]
P --> R
R --> S1
R --> S2
R --> S3
Use Cases
- Notifications
- Chat Applications
- Live Updates
Distributed Lock
Prevent multiple servers from updating the same record.
flowchart TD
S1["Application Instance 1"]
S2["Application Instance 2"]
LOCK["Redis Lock"]
DB["Database"]
S1 --> LOCK
S2 --> LOCK
LOCK --> DB
Common use cases
- Payment Processing
- Inventory Updates
- Batch Jobs
Rate Limiting
Redis is widely used for API rate limiting.
flowchart TD
CLIENT["Client"]
GATEWAY["API Gateway"]
REDIS["Redis Rate Limiter"]
LIMIT{"Limit Exceeded?"}
APP["Spring Boot API"]
REJECT["429 Too Many Requests"]
CLIENT --> GATEWAY
GATEWAY --> REDIS
REDIS --> LIMIT
LIMIT -- No --> APP
LIMIT -- Yes --> REJECT
Example
100 Requests
↓
Per Minute
↓
Per User
Session Management
Redis stores user sessions centrally.
flowchart TD
B["Browser"]
APP["Spring Boot"]
REDIS["Redis Session Store"]
B --> APP
APP --> REDIS
Useful for
- Stateless applications
- Kubernetes
- Auto Scaling
Banking Example
Cache
- Branch Information
- Exchange Rates
- Interest Rates
- Product Catalog
Never Cache
- Account Balance
- Payment Status
- OTP
- Transaction Ledger
Amazon Example
Redis caches
- Product Details
- Recommendations
- Shopping Preferences
- Product Rankings
Dynamic information is still fetched from databases.
Netflix Example
Redis stores
- User Preferences
- Watch History
- Movie Metadata
- Personalized Recommendations
Uber Example
Redis stores
- Driver Locations
- Estimated Arrival Time
- Surge Pricing
- Popular Destinations
Spring Boot Architecture
flowchart TD
CLIENT["Client"]
LB["Load Balancer"]
GATEWAY["API Gateway"]
APP1["Spring Boot 1"]
APP2["Spring Boot 2"]
REDIS["Redis Cluster"]
DB["PostgreSQL"]
CLIENT --> LB
LB --> GATEWAY
GATEWAY --> APP1
GATEWAY --> APP2
APP1 --> REDIS
APP2 --> REDIS
REDIS --> DB
Popular Spring Libraries
- Spring Cache
- Spring Data Redis
- Lettuce
- Redisson
Redis vs Database
| Redis | Database |
|---|---|
| RAM | Disk |
| <1 ms | 10-100 ms |
| Key-Value | Relational |
| Temporary/Frequently Accessed Data | Persistent Business Data |
| High Throughput | Transaction Processing |
Redis vs Memcached
| Redis | Memcached |
|---|---|
| Rich Data Structures | Simple Key-Value |
| Persistence | No Persistence |
| Replication | Limited |
| Pub/Sub | No |
| Streams | No |
Monitoring
Monitor
- Memory Usage
- Cache Hit Ratio
- Cache Miss Ratio
- Evictions
- Connected Clients
- Latency
- CPU Usage
- Replication Lag
Tools
- Redis Insight
- Prometheus
- Grafana
- Datadog
- CloudWatch
Common Mistakes
❌ Caching sensitive banking data
❌ No TTL configuration
❌ Very large cache objects
❌ Ignoring cache invalidation
❌ Running Redis without authentication
❌ Single Redis instance in production
Best Practices
- Cache frequently accessed data.
- Use TTL to expire stale data.
- Keep cached objects small.
- Use Redis Cluster for scalability.
- Use Redis Sentinel for high availability.
- Monitor cache hit ratio continuously.
- Secure Redis with authentication and network isolation.
- Avoid caching highly volatile transactional data.
- Use distributed locks for critical workflows.
- Use Spring Cache abstraction for maintainable code.
Common Interview Questions
What is Redis?
Redis is an in-memory key-value data store used for caching, session management, messaging, rate limiting, and distributed coordination.
Why is Redis faster than a traditional database?
Redis stores data in RAM, allowing operations to complete in microseconds or a few milliseconds, whereas traditional databases typically access data from disk.
What is Redis Cluster?
Redis Cluster distributes data across multiple Redis nodes, providing horizontal scalability, fault tolerance, and high availability.
What is Redis Sentinel?
Redis Sentinel monitors Redis instances, detects failures, and automatically performs failover by promoting a replica to the primary role.
When should Redis not be used?
Redis should not be used as the sole storage for critical transactional data unless persistence and appropriate durability mechanisms are configured. Highly sensitive or rapidly changing financial data often requires a transactional database as the source of truth.
Summary
Redis is one of the most important technologies in modern distributed systems. It provides extremely fast in-memory data access and supports far more than simple caching, including session storage, distributed locks, Pub/Sub messaging, and rate limiting.
In this article, we covered:
- Redis fundamentals
- Redis architecture
- Cache Hit & Cache Miss
- Redis data structures
- Cache strategies
- Redis Cluster
- Redis Sentinel
- Distributed caching
- Pub/Sub
- Distributed locking
- Spring Boot integration
- Banking, Amazon, Netflix, and Uber examples
- Monitoring
- Best practices
Redis enables applications to handle millions of requests with low latency, making it a foundational component of high-performance, cloud-native architectures.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...