Redis Advanced Architecture
Redis Streams vs Pub/Sub (Advanced Architecture)
When designing scalable systems using Redis, one of the most common architectural decisions is:
Should I use Pub/Sub or Streams?
1️⃣ Redis Pub/Sub – Fire & Forget Messaging
How It Works
-
Publisher sends message to channel
-
Redis pushes message instantly to subscribers
-
No storage
-
No acknowledgment
SUBSCRIBE notificationsPUBLISH notifications "New Order Created"
Characteristics
-
Real-time delivery
-
No persistence
-
No retry
-
No message history
-
No consumer tracking
Best For
2️⃣ Redis Streams – Persistent Event Log
Redis Streams (introduced in Redis 5) behave like a lightweight event streaming platform.
XADD orders * user_id 101 product "Laptop"XREAD STREAMS orders 0
Key Features
-
Persistent message log
-
Consumer groups
-
Acknowledgment support
-
Message replay
-
Message ID tracking
Core Architectural Differences
| Feature | Pub/Sub | Streams |
|---|---|---|
| Persistence | No | Yes |
| Replay | No | Yes |
| Consumer Groups | No | Yes |
| Message Acknowledgment | No | Yes |
| Offline Consumers | Lose messages | Can catch up |
| Ordering | Per subscriber | Global stream order |
Q: How would you design a reliable background job system using Redis?
Use Redis Streams, not Pub/Sub.
Why?
-
You need message durability
-
Worker crash recovery
-
Acknowledgment support
-
Retry mechanism
When to Use What?
Use Pub/Sub If:
-
You don’t care about message loss
-
You need ultra-low latency
-
It's purely real-time
Use Streams If:
-
You need durability
-
You need retries
-
You need multiple consumers
Scenario 1: Rate Limiter (API Protection)
Problem: Limit users to 100 requests/minute.
Solution:
Use Redis atomic counters:
INCR user:101:countEXPIRE user:101:count 60
Why Redis?
-
Atomic operations
-
Fast lookups
-
TTL support
-
You are building a queue system
If reliability matters → Streams win.
If speed + simplicity matters → Pub/Sub is enough.
Scenario 2: Real-Time Chat System
Option A – Pub/Sub
-
Real-time message broadcasting
-
Good for active users
Option B – Streams
-
Store chat history
-
Allow offline replay
-
Better architecture for production
In real systems, combine both:
-
Pub/Sub for live
-
Streams for storage
Scenario 3: Background Job Queue
Instead of basic Pub/Sub:
Use:
-
Redis Streams
-
Or Celery (which often uses Redis as broker)
Why?
-
Acknowledgment
-
Retry
-
Worker coordination
Scenario 4: Leaderboard System
Use Sorted Sets:
ZADD leaderboard 500 user1ZADD leaderboard 800 user2ZRANGE leaderboard 0 -1 WITHSCORES
Why Redis?
-
O(log n) ranking
-
Instant score updates
-
Perfect for gaming systems
Scenario 5: Distributed Locking
Use:
SET resource_lock "1" NX EX 30
Prevents:
-
Duplicate payments
-
Concurrent cron execution
-
Double order processing
Scenario 6: Session Store for Django
With:Django
Why Redis?
-
Fast session lookup
-
Centralized session storage
-
Works well in multi-server deployments
In large-scale production systems:
| Layer | Tool |
|---|---|
| Cache | Redis |
| Queue | Redis Streams / Celery |
| Real-time | Pub/Sub |
| Long-term events | Apache Kafka |
| Primary DB | PostgreSQL |
Redis often acts as a performance accelerator, not primary storage.
“Is Redis Streams a replacement for Kafka?”
Answer:No.
Redis Streams:
-
Great for moderate-scale event systems
-
Simpler to operate
-
Less operational overhead
Kafka:
-
Designed for massive event pipelines
-
Long retention
-
Partition scaling
-
Strong durability guarantees
| If You Need | Use |
|---|---|
| Real-time only | Pub/Sub |
| Reliable queue | Streams |
| Background jobs | Streams / Celery |
| Huge event platform | Kafka |
| Simple caching | Redis Strings |
Comments
Post a Comment