The same question, thousands of times Thousands of users open product 42. Each request makes the app run the same database query and get the same answer.
A cache keeps a copy of recent answers in memory, closer and faster than the database. There are caches at every layer: the browser, the CDN at the edge, the app's cache server, and the database's own buffer pool. This lesson is about the app's cache server.
Key idea The database stays the source of truth. A cache is a copy that can be lost or out of date, and the design must accept both.
Cache miss: ask the DB, then fill the cache With cache-aside, the app owns the logic. It first asks the cache for key product:42. The key is not there, so the cache answers nil. That is a miss.
The app queries the database, returns the result to the user, and stores it in the cache with a time to live: SET product:42 <json> EX 300. The key will disappear on its own after 300 seconds.
Cache hit: the answer comes from memory The next request for product 42 finds the key. The cache returns the stored value from RAM, usually in under a millisecond on the same network, and the database is not touched.
The share of lookups that hit is the hit ratio: hits / (hits + misses). Redis and Valkey count both in INFO stats as keyspace_hits and keyspace_misses.
Key idea A cache only pays off when the same data is read many times before it changes. Data read once, or changed on every request, gains little.
A write updates the DB and deletes the key The admin changes the price of product 42. The app writes the database first, then deletes the cache key with DEL product:42. The next read misses and loads the new price.
Deleting is safer than writing the new value into the cache. If two updates race, a delete cannot leave an older value behind the way two out-of-order SETs can.
Stale data: the copy can be wrong for a while Another service changes the price directly in the database and never touches the cache. Users keep seeing the old price until the TTL runs out. That gap is the stale data window.
Even with deletes there are rare races: a slow reader can load the old row just before the update and write it back just after the DEL. The TTL is the upper bound on how long any of these mistakes can last.
Watch out Pick the TTL from how stale the data may be, not from how fast you want it. A price might allow 60 seconds; a stock count for checkout should not come from the cache at all.
Memory is full: the eviction policy decides The cache lives in RAM, and maxmemory caps how much it may use. When a new SET would go over the cap, the maxmemory-policy decides what happens.
allkeys-lru removes the least recently used keys, and allkeys-lfu the least frequently used. volatile-* policies only remove keys that have a TTL. The default is noeviction: writes fail with an OOM error, which is right for a data store but wrong for a pure cache.
Watch out For a cache, set maxmemory and an allkeys-* policy, and watch evicted_keys. Steady evictions with a falling hit ratio mean the cache is too small for the working set.
A hot key expires and every request hits the DB at once Product 42 is on the front page at 5,000 requests per second. Its key expires. For the next tens of milliseconds every request misses, and all of them run the same query on the database together.
This is a cache stampede (thundering herd). The database slows down, each query takes longer, so even more requests pile up as misses before anyone refills the key.
Let one request refill, spread out expiry Lock: the first request that misses takes a short lock (SET lock:product:42 1 NX EX 5) and loads the DB. The others wait briefly or serve the old value. Only one query reaches the database.
Jittered TTL: add a random spread, such as 300 s ± 10%, so keys loaded together do not all expire together. Early refresh: refresh a hot key in the background before it expires, so it never goes missing.
The cache server dies and the DB takes everything The cache server restarts. Its memory is empty, or it is unreachable for a minute. Every lookup now misses, and the database gets the full traffic the cache used to absorb, about 20 times more if the hit ratio was 95%.
If the app waits on cache timeouts, every request also gets slower. A cold cache after a restart causes the same flood, even when the server is back.
Watch out Size the database, or put a rate limit in front of it, for traffic with the cache gone. Use short cache timeouts so the app falls back fast. Run the cache with replicas so one failure does not empty it.
A fast copy with an expiry date Cache-aside reads check the cache, fall back to the database on a miss, and fill the key with a TTL. Writes go to the database and delete the key.
The TTL bounds how stale data can get. maxmemory and the eviction policy decide what goes when RAM is full. Locks, jittered TTLs and early refresh stop stampedes, and the database must still survive without the cache.
Summary Watch three numbers: hit ratio (keyspace_hits / misses), evicted_keys, and database load when the cache restarts.