TTL Patterns & Expiration
Time-To-Live (TTL) is the heartbeat of Redis caching. When you set a TTL on a key, Redis automatically deletes it when the timer expires. This keeps your cache fresh and memory efficient without manual cleanup.
SET with EXPIRE
Store a product cache entry and set it to expire in 1 hour (3600 seconds):
(integer) 1
(integer) 3600
Breakdown:
SETstores the JSON product data.EXPIREsets the key to expire in 3600 seconds (1 hour).TTLreturns the seconds remaining. Returns -2 if the key doesn't exist, -1 if it has no expiration.
Session Management with Sliding Window TTL
In a real application, sessions often need renewal on each access (sliding window). Here's how:
(nil)
(integer) 1
(integer) 1800
By calling EXPIRE again on each user action, the session timer resets. This is called a sliding window โ the session lives as long as the user keeps accessing the app.
Cache-Aside Pattern (Lazy Loading)
The cache-aside pattern is the most popular. The logic is simple:
- Check the cache (Redis)
- If miss, query the database (PostgreSQL)
- Store the result in cache with TTL
- Return to client
This puts control in the application's hands. Let's walk through a real ShopStream example:
Demonstrating Cache-Aside in Redis CLI
OK
(integer) 1
{"id":50,"name":"Cotton T-Shirt","category":"apparel","price":899,"stock":120}
(integer) 3600
Real-world scenario: ShopStream shows product details for 10 million requests/day. Without cache-aside, each request hits the database. With cache, 95% of requests hit Redis (sub-millisecond response) and only 5% hit PostgreSQL (cache misses or TTL expiry).
Cache Invalidation Strategies
The hardest problem in caching is knowing when to invalidate the cache. Two main approaches:
1. TTL-Based (Automatic) Invalidation
Pros: Simple, no manual work. Cons: Stale data until expiry.
(integer) 1
2. Event-Based (Manual) Invalidation
When product stock changes in the database, invalidate the cache immediately:
OK
OK
(integer) 1
1) "product:100:details"
2) "product:101:details"
3) "product:102:details"
(integer) 3
Using UNLINK for Better Performance
UNLINK is like DEL but non-blocking. If you're deleting large keys or many keys, UNLINK is faster:
Best Practice: Combine both. Use TTL for automatic freshness (e.g., 1 hour for product details) and event-based invalidation for critical updates (e.g., stock changes, price changes).
Write-Through vs. Write-Behind Patterns
Write-Through Pattern
Flow: Client โ Cache โ Database
Write to cache first, then database. Guarantees cache and DB are always in sync. Slower (must write to both), but safe.
Write-Behind Pattern
Flow: Client โ Cache โ (async) Database
Write to cache immediately, DB is updated asynchronously. Fast for client, but data loss risk if cache fails before DB flush.
Use Write-Through for: Financial data, orders, critical updates where consistency is non-negotiable.
Use Write-Behind for: Counters, logs, product views where a small lag is acceptable and speed matters.
Pub/Sub: Real-Time Messaging
Redis Pub/Sub enables real-time messaging between services. One service publishes events, other services subscribe and react instantly.
Pub/Sub Basics
Use case: ShopStream order notifications. When an order is placed, notify warehouse and customer instantly.
Publisher: Broadcast Order Created Event
Subscriber: Listen for Order Events
In a real system, the warehouse service subscribes to orders:new and processes them:
Pattern Subscription (PSUBSCRIBE)
Subscribe to multiple channels using patterns:
Important: Pub/Sub is fire-and-forget. If no subscribers are listening when you PUBLISH, the message is lost. For persistent messaging, use Redis Streams (Lesson 31).
Python redis-py Integration
Now let's implement all these patterns in production Python code for ShopStream.
Cache-Aside Function
Cached product 100 for 3600 seconds
{"id": 100, "name": "Wireless Earbuds", "price": 2499}
Cache hit: {"id": 100, "name": "Wireless Earbuds", "price": 2499}
{"id": 100, "name": "Wireless Earbuds", "price": 2499}
Caching Decorator
Wrap any function with caching using a decorator:
Rate Limiting with Redis INCR
Limit API requests per user. Increment counter, check if over limit:
Request 2: Request 2/10 allowed
...
Request 10: Request 10/10 allowed
Request 11: Rate limit exceeded: 11/10
Request 12: Rate limit exceeded: 12/10
Batch Pipeline for High-Throughput Caching
Cache multiple products at once using pipeline:
Challenge
Build a cache invalidation system for ShopStream inventory updates:
- Create a function that caches product details with 1-hour TTL
- When stock is updated in the database, invalidate the product cache
- Implement a batch invalidation for multiple products
- Bonus: Use PUBLISH to notify other services of the inventory change
Test Your Knowledge
You're mastering caching at scale. Go deeper.
Join the Best AI Program: The AI-Era Data Engineer โ 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Data Engineer โ