Caching: Ferron Fox and the Long Ride to Oakhollow
ONELINE
A cache is a copy, so once the original can change underneath it, you need a deliberate freshness strategy — and a timer expiring isn't the same thing as being told the copy is wrong.
Cheat sheet: the fairground map
| Story | System |
|---|---|
| Ferron Fox and his Fireside Stew cart | One running service instance (a single server/process) |
| Pip's cart, and the rest of the fairground's carts | The fleet — other instances of the same service, running alongside Ferron's |
| Hazel's larder, kept in Oakhollow Forest, a half-day's ride away | The origin / backing data store — physically distant, so every round trip costs real time regardless of how busy it is |
| The venison stew, fourteen hours to simmer | An expensive computation worth keeping once produced |
| Mustard, chili flakes, the day's price slate | Small, individually-cheap data requested often enough that the total cost adds up |
| Ferron's own cart shelf | A local (in-process) cache — fast, but each instance stocking its own full working set duplicates effort across the fleet |
| The one shared cart at the fairground's center | A single-node remote cache — shared across instances, removing the duplication, but still one node with a hard capacity ceiling |
| Several shared carts, each stocked with a different slice | A distributed remote cache — that same shared layer spread across multiple nodes, each owning a subset of the data |
| Shelf first, then the shared cart(s), then Oakhollow — restocking the layers on the way back | A cache hierarchy: fastest layer checked first, fall through only on a miss, backfill on the way back |
| The chalked "good until sundown" label | A TTL (time-to-live) |
| Warden Olwen's recall call | Explicit cache invalidation — an active signal, not an expiring timer |
| The ledger back at Hazel's larder | The system of record a write must eventually reach |
Keep these at separate levels: Ferron is the whole running service, never the shelf itself. The shelf, the shared cart(s), and Hazel's larder are three different places the same stock can live — which one you're looking at changes how fast it answers and how far it can drift from the truth, never who's asking.
What problem is a cache actually solving?
A cache exists to avoid redoing work that's expensive for one of two different reasons: reaching the answer takes real time because it lives far away or because producing it takes real effort, or reaching it too often adds up even when any single trip looks cheap. Either way, keeping a copy closer to where it's needed also keeps traffic off whatever's slow or far away in the first place — the origin can't take unlimited visits any more than Hazel's larder in Oakhollow Forest can take every cart riding out to it at once.
The shelf that had to hold everything
Ferron Fox ran the Fireside Stew cart at the edge of the fairground, nearest the tree line. Everything that went into a bowl — the venison, the mustard, a pinch of chili flakes, even the little slate that listed the day's prices — technically all came from the same place: Hazel Hedgehog's larder, a full day's round trip away in Oakhollow Forest. Nobody minded the occasional ride. The trouble was it wasn't occasional.
A bowl of stew needed mustard. The next customer wanted extra chili flakes. The one after that just wanted to see the price slate. None of these were big requests on their own — but each one, fetched from Hazel's larder specifically, cost Ferron the same twelve-hour round trip as if he'd gone for a full wagonload. He couldn't run his cart and be on the road to Oakhollow at the same time. The stew made things worse before anything got better: a proper batch of his venison stew took fourteen hours to simmer from nothing, no matter how badly someone wanted a bowl right now.
So Ferron started keeping things. A jar of mustard, a tin of chili flakes, a chalked copy of the price slate — and, once a batch finished simmering, a pot of the stew itself — all on a shelf at the back of his own cart. Reaching for any of it took about as long as reaching for anything does. No ride, no fourteen-hour wait, just whatever was already sitting there.
It worked for Ferron. It also meant that to never make the ride, he'd eventually have to keep a little of everything Hazel's larder carried — not just the handful of things he reached for most, but genuinely everything he might someday need, just in case. And he wasn't the only cart doing this. Pip Otter's pickle cart kept its own shelf. So did the next cart, and the one after that. Six carts in, the fairground wasn't storing Hazel's stock once — it was storing most of it six separate times, once per cart, each shelf a little bigger than it needed to be for any single customer.
NAPKIN — Tech translation
Story: Each cart's shelf is fast and simple on its own — the problem only shows up once you count every cart's shelf together.
Python: This is a local (in-process) cache used with no sharing between instances: each running copy of a service keeps its own in-memory copy of whatever it needs. Reachable instantly from inside that one process, invisible to every other process — so if ten instances all need the same data, you don't get one cached copy, you get ten, and total memory used grows with the number of instances, not with how much data actually exists.
One shared cart, checked in order
The fix the carts landed on, eventually, was obvious enough once somebody said it out loud: stop keeping six shelves, keep one. A single shared cart went up at the center of the fairground, stocked with the staples every cart reached for, restocked from Oakhollow as needed, and open to any cart's runner.
Ferron's own habit changed to match. Check his own shelf first — if it's there, done. Not there? Send a runner to the shared cart instead of riding anywhere himself. Only if the shared cart didn't have it either did anyone saddle up for the full ride to Oakhollow — and whatever they brought back got left in two places on the way through: a jar on the shared cart for the next runner, and a smaller jar on Ferron's own shelf, in case he needed it again before anyone else did.
It removed the fleet-wide duplication of the shared stock — one shared stockpile instead of six copies of it, even though Ferron's own shelf still kept a little of its own. It didn't, on its own, make the fair infinitely scalable. By midsummer the fairground had grown from six carts to forty, and forty carts' worth of runners were queuing at the same one shared cart for everything none of them had on their own shelf. The cart wasn't any roomier than any other single cart at the fair — it was just the one expected to hold the entire fairground's working stock, for everyone, at once. Long before Oakhollow's actual supply ran thin, the shared cart's own shelf space, and the line of runners waiting on it, became the real limit.
NAPKIN — Tech translation
Story: One shared cart removes the duplication from before — every cart now reaches the same stock through the same place — but it's still exactly one cart, with one cart's worth of room and one cart's worth of hands serving runners.
Python: This is a single-node remote cache — one separate shared service (one Redis instance, say) that every instance of the application calls into over the network instead of keeping its own copy. It solves the fleet-wide duplication from the local-cache beat, but introduces a different limit: the whole shared working set has to fit on that one node, and that one node has to keep up with every instance's traffic at once.
Too much for one cart to carry
Stocking more onto the one shared cart wasn't really an option — a cart only has so much room, and forty carts' worth of staples doesn't fit on one, no matter how it's arranged. So the fairground split it instead. Three shared carts went up around the center instead of one, each stocked with a different slice of what Oakhollow carried: one carried grains and spices, one carried root vegetables, one carried dairy and preserves. No single cart held everything anymore — but between the three of them, everything was still there, somewhere.
Ferron didn't start asking the grain cart, then the root-vegetable cart, then the dairy cart, hoping one of them had his chili flakes. The carts had already agreed among themselves which one owned what, so a runner went straight to the grain cart for chili flakes and straight to the dairy cart for a jar of preserves, no asking around required. Nothing about Ferron's own habits changed otherwise: his shelf was still checked first, the three carts together were still the one layer he reached for next, and Oakhollow was still the one place anyone rode to if even that layer came up empty. It was the same three-layer order as before — shelf, shared layer, Oakhollow — the middle layer had just stopped being one cart and become three, each covering a slice of what the single cart used to try to cover alone.
NAPKIN — Tech translation
Story: Splitting the stock across carts solves the one-cart capacity ceiling — but only because something, in advance, decided which cart owns what; without that agreement, Ferron really would be asking around.
Python: This is a distributed cache — the same shared layer from the last beat, now spread across multiple cooperating nodes, each owning a partition (a shard) of the full key space, typically decided by hashing the key. It's still a remote cache from any one instance's point of view — "remote" and "distributed" describe different things: where the cache lives (not in-process) versus whether that remote layer is one node or several. Splitting the data buys more room and more throughput, in exchange for needing that routing agreement — not a strictly better cache in every respect, just a different tradeoff than one node.
The recall that beat the label
Every jar that went up on a shelf or a shared cart got chalked with a label — "good until sundown," usually, since that's about how long a fair day's prepared stock was trusted to sit out. Nobody checked much beyond that. If the chalk hadn't run out, the jar was fine.
One afternoon, a batch of the dipping sauce turned bad at two o'clock — hours before any label chalked that morning said it should. Warden Olwen, the fairground's food-safety owl, caught it on an inspection round at ten past two and called the recall over the signal-horn network the carts used for exactly this. But a recall has to physically reach every shelf and every shared cart before any of them stop serving from it, and that took the better part of half an hour to make the rounds. Ferron, three bowls deep into the lunch rush with his horn turned down against the noise, kept serving straight off the shelf the whole time — not because he'd ignored anything, but because the only thing he'd actually checked was the label, and the label, chalked that morning, still said "good until sundown." It had no way of knowing anything had happened at the larder at all.
NAPKIN — Tech translation
Story: The label's countdown and Olwen's recall are two completely different mechanisms, and only one of them actually knows the sauce went bad. The chalk just counts down on a clock set that morning; the recall is the one message actually watching whether anything's still true.
Python: This is TTL (time-to-live) versus explicit invalidation. A TTL bounds how long an entry is trusted without being rechecked — cheap, automatic, and completely indifferent to whether the underlying data actually changed in the meantime. Invalidation is an active signal — something that knows the origin changed reaches in and removes (or corrects) the cached copy immediately, regardless of how much TTL is left. A short TTL narrows the gap; it never closes it the way an explicit invalidation call does, delivery time permitting.
WATCHOUT
A TTL's job is to bound how long you'll trust a copy without hearing otherwise — it is not a guarantee that the copy is still right. TTL and invalidation solve different problems, and data where staleness is unsafe often benefits from combining both.
- TTL alone — cheap, handles ordinary drift, but leaves exactly the kind of window this beat just dramatized
- Explicit invalidation alone — catches changes immediately, but only if something is watching the origin and the message actually gets delivered
- Both together — TTL as a safety net, invalidation for anything that can't wait, like a recall
RECALL
Olwen's recall has to physically reach every shelf and every shared cart before any of them stop serving the bad batch. What happens to a cart that isn't listening on the signal horn at that exact moment — and what would actually have to change for the recall to guarantee delivery, rather than just announce it?
ASIDE
This lesson doesn't cover what decides which items get bumped off a full shelf when something new needs the room. Eviction policies like LRU or LFU are a separate, later question from whether an entry is right or wrong in the first place.
Three ways to write it down
Reading from a cache only covers half of what Ferron's cart does. Every restock, every custom order, every new batch eventually has to be written down somewhere — not just on his shelf, but back on Hazel's actual ledger at the larder, the record the rest of the fair's accounting trusts. Where that write lands first, and when the other copy catches up, turned out to matter more than Ferron expected.
On an ordinary morning restock, Ferron does both before moving on to anything else: chalk the new count on his shelf, and send word to Hazel's ledger confirming the same number, not considering the restock finished until both agree. It costs him a few minutes he could be using to serve someone, every single time he restocks, but his shelf and Hazel's ledger are never caught disagreeing.
A customer orders something odd — double chili, no venison, extra mustard — a combination nobody's likely to ask for again before the day's out. Ferron sends word straight to Hazel's ledger and leaves his own shelf alone entirely. There's no reason to spend shelf space holding a combination that probably won't come up a second time; if it ever does, the next order misses the cache layers and eventually checks the ledger.
At the height of the lunch rush, Ferron updates his own shelf the instant he preps something new, so the line in front of his cart never stops moving — word to Hazel's ledger can wait for a quieter minute between orders. That quieter minute doesn't come. The fairground's one shared generator sputters out before he gets to send anything, and whatever he chalked up during that stretch never reaches Hazel's ledger at all. Not late. Gone — because nothing else at the fair knew to tell her either.
NAPKIN — Tech translation
Story: All three answer the same question differently — when Ferron writes something new, does his shelf get it first, does Hazel's ledger get it first, or do both get it at the same moment?
Python: Write-through: cache and origin updated together, synchronously — safest, costs latency on every write. Write-around: origin only; the cache only picks the value up later, on a read that misses. Write-back (write-behind): cache first, origin deferred — fastest at the call site, but anything written in that deferred window is only as durable as the cache, until the deferred write actually lands.
Putting it together: a shelf that can actually hear a recall
Here's roughly what Ferron's shelf looked like before the gotcha scene — fast, but with exactly one way for an entry to go away: its own clock running out.
import time
class ShelfCache:
def __init__(self):
self._entries = {}
def get(self, key):
entry = self._entries.get(key)
if entry is None:
return None
value, expires_at = entry
if time.monotonic() > expires_at:
del self._entries[key]
return None
return value
def set(self, key, value, ttl_seconds):
self._entries[key] = (value, time.monotonic() + ttl_seconds)
There's no path here for anything outside to reach in and say "this one's wrong, right now" — a recall, a correction at the larder, nothing. The only exit is the clock. Warden Olwen's recall needs a second path that doesn't wait on that clock at all:
class ShelfCache:
def __init__(self):
self._entries = {}
def get(self, key):
entry = self._entries.get(key)
if entry is None:
return None
value, expires_at = entry
if time.monotonic() > expires_at:
del self._entries[key]
return None
return value
def set(self, key, value, ttl_seconds):
self._entries[key] = (value, time.monotonic() + ttl_seconds)
def invalidate(self, key):
self._entries.pop(key, None)
The recall becomes a single call — shelf_cache.invalidate("sauce_batch_17") — fired the moment Olwen's message reaches Ferron's cart, not whenever the label's countdown happens to finish. The TTL is still underneath it, catching anything nobody ever explicitly corrects; the invalidate() call is what actually closes the gap the gotcha scene dramatized.
And here's what the shared cart from earlier actually bought the fairground, computed honestly rather than asserted. Say thirty carts each need one fresh jar of chili flakes restocked once during the fair. Without any shared cart, each of those thirty restocks is its own round trip to Oakhollow: 30 × 720 minutes = 21,600 minutes of riding, spread across the fair. With one shared cart doing a single ride and everyone else's runner just walking over to it: one 720-minute ride to stock the shared cart, plus twenty-nine roughly one-minute trips to the shared cart itself — 720 + 29 ≈ 749 minutes total. That's about 29 times less riding, for the exact same thirty restocks.
Closing distinction: invalidation, or just eviction?
Not every time an entry disappears from a cache is the gotcha scene's kind of problem. If Ferron's shelf fills up and the oldest jar nobody's touched in hours gets pushed off to make room for something new, nothing about that jar was wrong — it was simply time for something to go, and the shelf picked the least-used thing. That's eviction: routine housekeeping under capacity pressure, with no claim that the evicted entry was incorrect.
Invalidation is a different claim entirely: the entry is sitting right there, with plenty of room to spare, and it's still wrong, because whatever it's a copy of changed at the origin. Olwen's recall doesn't care how full or empty the shelf is; it cares that the sauce is bad. In code terms, cache.evict_lru() and cache.invalidate(key) are triggered by two unrelated conditions — one by how much room is left, the other by whether the origin has moved on without telling the cache yet — and conflating them is how a correctness problem quietly gets treated as a capacity problem, or the other way around.