Redis Caching Patterns: Cache-Aside, Write-Through
On this page
Choosing a caching pattern: a product page under load
The product page reads from PostgreSQL and takes 40 ms. At 5,000 requests per second the database struggles, so you put Redis in front of it. Three questions follow immediately: when does data get into the cache, what happens on writes, and what happens when a popular entry expires? The answers are the three patterns below and one problem they share.
Cache-aside (lazy loading)
The application owns the logic. Read the cache; on a miss, read the database, store the result with a TTL, return it. The database stays the source of truth, and only data someone asked for ends up in the cache.
This is by far the most common pattern. Facebook's 2013 paper "Scaling Memcache at Facebook" calls it a "demand-filled look-aside cache", the same idea at enormous scale.
Its weakness is staleness. After a price change in the database, the cache serves the old price until the entry expires, unless the write path also deletes the key. The common rule: on update, write the database, then DEL the cache key, so the next read loads fresh data.
Even that leaves a small race. A reader misses, reads the old row and stalls. Meanwhile a writer updates the row and deletes the key. Then the stalled reader writes the old value into the cache, where it stays until the TTL runs out. It's rare, and the TTL bounds it, which is one reason to keep TTLs even on keys you invalidate. Facebook's paper fixes it with leases: the cache hands out a token on a miss, a delete cancels the token, and the late, stale write is rejected.
Write-through caching
Every write updates the database and then the cache, in the same request. Reads never see a cached value older than the database's, as long as both writes succeed.
You pay on every write: two writes instead of one, and you must decide what happens if one fails. You also cache everything that's written, including data nobody reads. It fits data that's read often right after it changes and must not be stale for long.
Write-behind (write-back) caching
The write goes to the cache, the client gets an answer immediately, and a background process flushes changes to the database, often in batches. Write latency is lowest, and the database sees fewer, bigger writes.
The risk is loss. Anything not yet flushed is gone if the cache node dies, and Redis replication is asynchronous (Day 6). Use it only for data where some loss is acceptable, such as view counters, or where the queue of pending writes is itself durable.
| Pattern | Reads can be stale? | Write cost | Main risk |
|---|---|---|---|
| Cache-aside | Until TTL or invalidation | Database only, plus a DEL | Stale reads, stampedes on hot keys |
| Write-through | Rarely | Two writes per update | Failure handling between the two writes |
| Write-behind | No, but the database lags | Lowest for the client | Losing unflushed writes |
How to prevent a cache stampede
A popular product's cache entry expires. In the next 40 ms, hundreds of requests miss at the same moment, and every one of them queries the database for the same row. That's a cache stampede, and it can take down the database the cache was protecting.
Two fixes work well together: let only one request rebuild the entry, and add random jitter to TTLs so many keys don't expire at the same second.
# cache.py: cache-aside with a short refresh lock so one request rebuilds a hot key
import json, random, time, uuid
import redis
r = redis.Redis(decode_responses=True)
TTL = 300
def get_product(product_id: int, load_from_db) -> dict:
key = f"product:{product_id}"
for _ in range(50): # up to ~2.5 s of waiting
cached = r.get(key)
if cached is not None:
return json.loads(cached) # hit
token = str(uuid.uuid4())
if r.set(f"{key}:lock", token, nx=True, px=5000): # first miss rebuilds
try:
cached = r.get(key) # filled while we were acquiring?
if cached is not None:
return json.loads(cached)
product = load_from_db(product_id)
ttl = TTL + random.randint(0, 60) # jitter: keys don't all expire together
r.set(key, json.dumps(product), ex=ttl)
return product
finally:
r.eval("if redis.call('GET', KEYS[1]) == ARGV[1] then "
"return redis.call('DEL', KEYS[1]) end return 0", 1, f"{key}:lock", token)
time.sleep(0.05) # someone else is rebuilding; retry the cache
return load_from_db(product_id) # give up waiting, serve from the databaseI tested this with 100 threads missing the same key at once against a slow fake database: the database was called once per run, every time. Without the second r.get after taking the lock, a request that missed just before the rebuild finished could take the freed lock and query again. The lock release uses the safe compare-and-delete covered tomorrow.
In ASP.NET Core, HybridCache (the Microsoft.Extensions.Caching.Hybrid package) gives you the single-rebuilder part without writing it: only one concurrent caller per key runs the factory, and the others wait for its result. That coordination is per application instance, so with 20 servers the database can still see up to 20 rebuilds of the same key (Microsoft docs).
Check yourself
Delete the product's cache key right after the database update commits, so the next read loads the new price. The TTL then only bounds staleness if a delete is missed.
Write-behind: increment in Redis and flush totals to the database periodically. The possible loss is acceptable and the database load drops sharply.
They were all loaded at the same time with the same TTL. Add random jitter to TTLs, and protect hot keys with a single-rebuilder lock.
HybridCache's stampede protection works within one instance, so each server rebuilds once. If 20 is too many, add a shared Redis lock like the one above.
When a hot key expires, many requests miss at once and all query the database to rebuild the same value. A lock taken with SET lock:key token NX EX 10 lets only the first request rebuild, while the others briefly wait or serve the stale value, so the database sees one query instead of hundreds.