Redis/Cache lab

Cache Lab: Thundering Herd

This lab shows what happens when many workers miss the same expired cache key at the same time, then fixes it with a short Redis fill lock.

Break modeNaive cache-aside. Every worker calls the backend on a miss.
Fix modeOne worker acquires SET NX PX. Others wait and retry GET.
InvariantBackend calls should be close to one per cache expiry cycle.
GET
MISS
SET NX PX
SET value

Redis Commands

GET key

Reads the cached value. Redis returns nil when the key does not exist or has expired.

SET key value EX ttl

Stores the backend result with a TTL. In this lab the Go client passes a duration, which Redis applies as expiry.

SET key value NX PX ttl

Creates a short fill lock only if it does not already exist. This is the single-flight gate.

DEL key

Deletes the fill lock after the winner stores the cache value. This lock protects recomputation, not business correctness.

Keys In Redis

KeyTypePurposeExpiry
product:42:priceStringCached product price.3 seconds in the lab.
product:42:price:fillingStringTemporary marker for the worker filling the cache.10 seconds in the lab.

Run Targets

The runnable lab is labs/redis/cache, backed by the shared Redis Go driver under labs/redis/cmd/cache.

make break

Runs naive mode. Look for many GET MISS lines and many backend calls.

make test

Runs fix mode. Look for one backend call per round and many workers waiting for the fill.

make load

Runs a bigger fixed scenario with more workers and rounds.

How To Read The Output

The important line is backend calls. In broken mode it grows with worker count. In fixed mode it should stay close to the number of expiry rounds.

Production Notes

Code Pointers

CodeWhy it matters
cmd/cache/main.goThe naive cache miss path, single-flight fill marker, and backend-call counter.
cache/MakefileTargets for broken, fixed, and larger load scenarios.
cache/compose.yamlRedis container used by the lab.
cache/README.mdShort explanation of the cache-stampede invariant and expected output.
Rate limit Locks Streams Pub/Sub