Redis as Cache: 30-Minute Tutorial
Redis is the simplest cache. The walkthrough gets you to measurable latency improvement.
Step 1: Run Redis
docker run -d -p 6379:6379 redis
Verify: redis-cli ping → PONG.
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
First call: 50ms (DB hit).
Second call: 0.5ms (cache hit).
100x speedup on cached path.
Step 4: TTL and invalidation
setex sets TTL.
On update: r.delete(f"user:{uid}") to invalidate.
Pattern: write-through invalidation.
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.