jonnifyechomq

The Cache · module 02

One fill
per herd

A cache that does not stampede and does not grow without bound.

Module 01 built the declared near-cache and showed how a read resolves across the two layers. Module 02 is the second half: the discipline that keeps it cheap under a herd and bounded under pressure. Concurrent misses on the same key coalesce onto a single in-flight load — one fill, not a thousand. Expiry is jittered so a cohort never dies in step, and a sweeper reclaims dead rows on a fixed tick. And when the table is full and no expired rows exist, a fill still serves its caller from L2 and the loader: the cache degrades to pass-through, it never fails. All three laws are in EchoStore.Table, real shipped code.

A herd, one flight

Pick how many callers miss simultaneously on the same key and watch the owner's mailbox coalesce them into a single loader call. The :coalesced counter records each waiter that did not start its own flight. Pick a path to see the call sequence and the counter update.

Herd coalescing · pick a caller count

callers 1 miss on same id owner handle_call {:fill, id} flights map — {:fill, id} flight spawn_monitor'd GET → loader → SET first caller waiters 0 +appended result one {:ok, value, source} to every waiter

The owner's mailbox is the serialization point — the first {:fill, id} starts a flight; every other appends. The loader runs once; every waiter reads that one answer. The :coalesced counter records every saved loader call.

The dives

Three dives, each a law from EchoStore.Table: the coalescing flight, the jittered clock, and the graceful degrade under pressure.

References

Sources

Related in this course