Highlights

Contents

Part IV
04

Invalidation and consistency

We keep the cache’s copy honest when the source changes. Phil Karlton’s joke that cache invalidation is one of the two hard things in computer science is a joke because it is true. We will cover the four ways to invalidate, the races that undo them and how leases and versions close those races, and negative caching, warming and staleness budgets.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Why cache, and where2Caching patterns3Expiry, eviction andsizing4Invalidation andconsistency5Cache failure modes6Redis data structures7Redis internals8Redis at scale9HTTP caching and CDNs10The menu request end toend

#Section 10Four ways to invalidate

Every cached copy eventually differs from its source. Cache invalidation is how a system notices and stops serving the old copy. Wren uses four techniques, often together.

The first is the TTL from Part III. Wait long enough and the entry disappears. It needs no code at the writer and catches every kind of change, including ones made by tools nobody remembered. Its weakness is that staleness lasts up to the full TTL.

The second is delete on write. The code that changes the database deletes the affected keys after the commit, as in cache-aside. Staleness drops to milliseconds in the normal case. Its weaknesses are that every writer must know which keys depend on the data it changed, and a write from anywhere that skips the delete, a migration, a support script, a second service, leaves stale data until the TTL. Dependencies are the harder half. Changing the price of one biryani affects menu:9, the search results that include it, and perhaps a “popular dishes” list. Missing one key is a common bug.

FOUR WAYS TO STOP SERVING OLD DATAFOUR WAYS TO STOP SERVING OLD DATATTLwait it outdelete on writethe writer erasesversioned keypoint to a new namechange eventsthe database tellsmenu:9DELmenu:9:v41menu:9:v42WAL → stream → invalidator
Figure 7.18TTL, delete on write, versioned keys and change events. Most systems combine two or three.

The third is a versioned key. Instead of deleting, the writer bumps a version number stored with the source, and readers include the version in the key. Restaurant 9’s row has menu_version = 41, readers use menu:9:v41, and when the menu changes the transaction sets the version to 42. Every reader after the commit asks for menu:9:v42, misses once and loads the new menu. The old entry is never read again and expires by TTL.

VERSIONED KEYS: BUMP A NUMBER, AND THE OLD ENTRY IS SIMPLY NEVER READ AGAINVERSIONED KEYS: BUMP A NUMBER, AND THE OLD ENTRY IS SIMPLY NEVER READ AGAINrestaurantmenu_version941 → 42menu:9:v41orphan, expires by TTLmenu:9:v42new reads land hereno race with a slow writer: a stale SET goes to the old name, which nobody readscosts one small lookup for the version, often itself cached
Figure 7.19Bump the version and the old entry becomes an orphan. A late SET lands on a name nobody reads.

Versioned keys remove the delete-and-race problem, because a slow reader that loaded old data writes it under the old version’s name. They also make bulk invalidation trivial. Bumping a single version can invalidate a whole family of keys that include it. The cost is one lookup for the current version, which is small and often cached itself for a second. The same idea at the edge is the fingerprinted asset URL from Part IX.

The fourth technique drives deletes from the database’s own change log, the subject of the next section.

#Section 11Change events and closing the races

A delete in application code depends on every writer remembering. Wren’s catalogue service, its admin tool and a nightly price-import script all change menus, and the script was written by someone who never heard of Redis. Event-driven invalidation moves the responsibility to the database. A change data capture reader, such as Debezium, follows PostgreSQL’s write-ahead log and publishes each committed change to a stream such as a Kafka topic. An invalidator service consumes the stream and deletes or refreshes the affected keys.

EVENT-DRIVEN INVALIDATION: THE DATABASE LOG DRIVES THE DELETESEVENT-DRIVEN INVALIDATION: THE DATABASE LOG DRIVES THE DELETESPostgreSQLCDC readerorder-events / menu-eventsinvalidatorRedismenu 9 changedmenu 9 changedmenu 9 changedcatches every writer, including scripts and admin tools that forget to delete keysthe log is ordered, so a later change is never undone by an earlier delete
Figure 7.20The WAL feeds a stream, and an invalidator turns each change into deletes.

This catches every writer, because every write goes through the database. It is ordered, because the log is ordered, so a later change is never undone by an earlier delete arriving late. And it decouples writers from caches, since the catalogue service no longer needs to know which keys exist. The costs are a pipeline to run, Unit VIII covers change data capture in depth, and a delay of tens to hundreds of milliseconds between commit and invalidation. Wren uses it for menus and restaurant data, and keeps direct deletes for user-facing writes where the delay would be noticed.

Two races remain even with good invalidation. The first is the stale SET from Part II, a reader that loaded old data before the write and stores it after the delete. Three fixes exist. Versioned keys make the late SET land on an orphaned name. Leases, as at Facebook, give each miss a token that a delete invalidates, so the late SET is rejected. And a delayed double delete deletes the key at commit and again a short time later, say 500 ms, to catch any SET that slipped in between. The last is a heuristic, not a guarantee, and Wren prefers versions.

The second race is replication. If invalidation deletes from the Redis primary and a reader reads from a replica that has not yet applied the delete, the reader sees old data. Reading cache keys from the primary, or tolerating that small window, are the only choices.

The deeper point is that a cache can never be strongly consistent with its source without coordinating every write, which would cost more than the cache saves. The design goal is bounded staleness. Wren writes down, per kind of data, how stale a read may be in the worst case and which mechanism enforces it.

#Section 12Negative caching, warming and staleness budgets

Not every lookup finds something. Wren’s apps request GET /orders/999 for orders that were deleted, restaurants that closed, or promo codes users typed wrongly. Cache-aside caches only values that exist, so every such request misses Redis and queries the database. Negative caching stores the fact that something does not exist. On a database miss, Wren writes a special marker, SET order:999 "∅" EX 30, and later lookups return 404 straight from Redis.

NEGATIVE CACHING: REMEMBER THAT SOMETHING DOES NOT EXISTNEGATIVE CACHING: REMEMBER THAT SOMETHING DOES NOT EXISTappRedisdatabaseorder 999?not foundSET order:999 "∅" EX 30next 30 s of lookupsfor 999 stop at Redisshort TTL, and delete itwhen the row is created
Figure 7.21The database said “not found” once. Redis says it for the next 30 s.

Negative entries need short TTLs and care at creation time. If order 999 is created a second later, the marker would hide it for up to 30 s, so the creation path deletes any negative marker for the new key. Negative caching also defends against the cache penetration attack in Part V, where an attacker requests millions of ids that do not exist.

A cache that starts empty is a cold cache, and every early request misses. After a Redis failover to an empty replica, a new cluster or a key format change in a deploy, the database briefly takes the full load. Cache warming fills the cache before traffic arrives. Wren warms by loading the 10,000 most-read keys from the previous day, identified from access logs, before shifting traffic to a new Redis cluster. With that done, the hit ratio starts near 86% instead of zero.

A COLD CACHE VERSUS A WARMED ONE AFTER A SWITCHOVER (ILLUSTRATIVE)A COLD CACHE VERSUS A WARMED ONE AFTER A SWITCHOVER (ILLUSTRATIVE)seconds after switchhit ratiocold: the database takes every misswarmed with the top 10,000 keys firstwarm by replaying recent reads or copying the hottest keys before shifting traffic
Figure 7.22Illustrative. A cold cache climbs slowly while the database takes the misses. A warmed one starts near its usual level.

Warming has a cost of its own. Loading 10,000 keys at full speed is itself a burst of 10,000 database reads, so warmers run with rate limits and jittered TTLs. Many teams prefer to shift traffic gradually instead, sending 1%, then 10%, then everything to the new cache, which warms it naturally without a spike.

Each cached kind of data in Wren carries a staleness budget, a sentence a product owner has agreed to. Menus may be up to 5 minutes stale in the worst case and are normally fresh within a second through change events. Restaurant open or closed status may be 60 s stale. Order status may be 5 s stale. Payment status is never cached. Writing the budget down turns arguments about caching into simple checks against a number.