What you will learn
- The cache-aside pattern and how to invalidate on writes.
- Why TTL jitter matters, and other stampede defences.
- How
maxmemoryand eviction policies decide what Redis drops.
Cache-aside
The application owns the logic: read from Redis; on a miss, read the database and populate Redis with a TTL. On a write, update the database and delete the cached key so the next read reloads fresh data.
"""Caching with Redis: cache-aside, TTL jitter, and LRU eviction under maxmemory."""
import json
import os
import random
import time
import redis
r = redis.Redis(port=int(os.getenv("REDIS_PORT", 6379)), decode_responses=True)
r.flushdb()
DB_CALLS = 0
def load_product_from_db(product_id):
global DB_CALLS
DB_CALLS += 1
time.sleep(0.05) # pretend this is a slow query
return {"id": product_id, "name": f"Product {product_id}", "price": 100 + product_id}
def get_product(product_id, ttl=300):
key = f"product:{product_id}"
cached = r.get(key)
if cached is not None:
return json.loads(cached) # hit
product = load_product_from_db(product_id) # miss: load, then populate
jitter = random.randint(0, ttl // 10) # spread expiries so keys don't all expire together
r.set(key, json.dumps(product), ex=ttl + jitter)
return product
def update_price(product_id, price):
# write to the database first (not shown), then delete the cached copy
r.delete(f"product:{product_id}")
start = time.perf_counter()
for _ in range(20):
get_product(7)
print(f"20 reads, DB calls: {DB_CALLS}, took {time.perf_counter() - start:.2f}s")
update_price(7, 150)
get_product(7)
print("after invalidation, DB calls:", DB_CALLS)
print("ttl within 300-330:", 300 <= r.ttl("product:7") <= 330)
# Eviction: cap memory and let Redis drop the least recently used keys
r.config_set("maxmemory", "4mb")
r.config_set("maxmemory-policy", "allkeys-lru")
r.set("hot", "keep me")
for i in range(5000):
r.set(f"bulk:{i}", "x" * 1000)
if i % 100 == 0:
r.get("hot") # touched regularly, so it stays recently used
info = r.info("stats")
print("keys now:", r.dbsize(), "| evicted:", info["evicted_keys"], "| hot survived:", r.get("hot") == "keep me")
r.config_set("maxmemory", "0") # restore: no limit
Output from one run (eviction counts vary between runs and machines):
20 reads, DB calls: 1, took 0.05s
after invalidation, DB calls: 2
ttl within 300-330: True
keys now: 2300 | evicted: 2702 | hot survived: True
Twenty reads cost one database call; after the price update, one more.
Why delete instead of update?
Updating the cache on write invites races: two writers can update the database in one order and the cache in the other, leaving the cache stale indefinitely. Deleting is simpler: the next reader loads the current value. A small race remains (a slow reader can put an old value back after the delete); TTLs bound how long that lasts, and some systems delete twice with a short delay.
Stampedes and TTL jitter
If thousands of keys are written at the same moment with the same TTL, they also expire together and the database gets a burst of misses. Adding random jitter to TTLs spreads expiries out. For a single very hot key, also consider:
- A short lock: the first miss takes
SET lock:product:7 1 NX PX 2000and reloads; others briefly wait or serve stale data. - Early refresh: refresh in the background when the TTL is low, before it hits zero.
The System Design course’s caching lesson covers these patterns at the architecture level.
Eviction: when memory is full
maxmemory caps memory use; maxmemory-policy decides what happens at the cap:
| Policy | Evicts |
|---|---|
noeviction (default) | Nothing: writes fail with an error |
allkeys-lru | Least recently used key, any key |
allkeys-lfu | Least frequently used key, any key |
volatile-lru / volatile-lfu | LRU/LFU among keys with a TTL only |
volatile-ttl | Keys with the nearest expiry |
allkeys-random / volatile-random | Random keys |
The demo caps memory at 4 MB with allkeys-lru and writes 5,000 1 KB values. Redis evicted thousands of keys, but the hot key survived because it was read regularly. Redis’s LRU is approximate: it samples a few keys (maxmemory-samples, default 5) and evicts the oldest of the sample, which is cheap and close to true LRU. You implement exact LRU in the LLD course.
For a pure cache, allkeys-lru or allkeys-lfu is the usual choice. For a Redis holding both cache and must-keep data, use volatile-* and give only cache keys a TTL, or better, separate instances.
Key takeaways
- Cache-aside: read Redis, fall back to the database, populate with a TTL; delete on write.
- Add TTL jitter and protect hot keys from stampedes with a short lock or early refresh.
- Set
maxmemoryand an eviction policy;noevictionmakes writes fail when full. - Redis LRU/LFU are approximations based on sampling.
Exercises
- Add stampede protection to
get_productwithSET NX PXso only one caller reloads a missing key. - Rerun the eviction demo with
allkeys-lfuand withvolatile-lru(no TTLs set). What happens to writes? - Measure your cache hit ratio from
INFO stats(keyspace_hitsandkeyspace_misses).
Quiz
- Why delete the cache entry on write instead of updating it?
- What does TTL jitter prevent?
- What happens with
noevictionwhen memory is full?
Show answers
- Concurrent writers can update the database and cache in different orders, leaving a stale value; deleting makes the next read load the current value.
- Many keys expiring at the same moment and sending a burst of misses to the database.
- Commands that would add memory fail with an out-of-memory error; reads still work.