Redis as Cache
Redis is the simplest cache. The walkthrough gets you to measurable latency improvement.
Step 1: Run Redis
Container Redis is the fastest path to a working cache: one command, no config files, throw it away when you are done.
- Start.
docker run -d -p 6379:6379 redislaunches the official image bound to the default port. - Verify.
redis-cli pingreturnsPONG; if it hangs, check the port is not already bound. - Persistence. The container is ephemeral; for the tutorial that is fine, for prod add a volume and AOF config.
- Cleanup.
docker stop $(docker ps -q --filter ancestor=redis)when you are done with the lab.
Step 2: Cache-aside in Python
def get_user(uid): cached = r.get(f"user:{uid}") if cached: return json.loads(cached) user = db.fetch(uid) r.setex(f"user:{uid}", 60, json.dumps(user)) return user
Step 3: Measure
The point of caching is observable latency reduction; instrument the call and watch the second hit drop into microseconds.
- Cold call. First request returns ~50ms from the underlying database.
- Warm call. Second request returns ~0.5ms from Redis memory; that is the 100x payoff.
- Hit ratio. Track
cache_hits / (cache_hits + cache_misses); below 70% the cache is mostly noise. - Tail latency. Watch p99, not mean; cache misses still take 50ms and dominate the tail.
Step 4: TTL and invalidation
The hard part of caching is staleness. TTL is the floor; explicit invalidation on writes is what stops drift.
- TTL.
setexbounds maximum staleness; pick a value matching the freshness the feature actually needs. - Write-through invalidation. On update, call
r.delete(f"user:{uid}")so the next read repopulates fresh. - Stampede protection. Add a short lock or single-flight on misses so a popular key does not hit the DB N times at once.
- Per-key inspection.
redis-cli ttl user:42andget user:42are the debugging tools when staleness is suspected.
Antipatterns
- No TTL. Stale forever.
- Cache without invalidation. Data drift.
- Caching everything. Memory exhaustion.
What to do this week
Three moves. (1) Run the tutorial end-to-end on your own laptop / sandbox. (2) Apply the pattern to one production workload. (3) Document the variations you needed; share with the team.