← the depth behind redis-patterns R5 · Streams & Events
The Cache · module 03
Coherence —
newer wins
A write on one node must not leave a stale read on another — settled by the clock already inside every id.
A cache that serves reads from local ETS is fast precisely because it does not consult the store
on every hit — which means the moment another node writes a new value, this node’s L1 row is wrong and
nothing on the hot path will notice. Coherence is the third law: a write on one node must not leave a
stale read on another. It is carried as a message about a name — an invalidation
holds exactly two identities, the cached id and the writer’s mint-time
version, twenty-nine bytes, nothing else. Newer wins by comparing the
eleven-byte snowflake payloads, across kinds, so coherence needs no coordinator, no lock, and no
clock but the one inside every id. Two lanes carry the same payload; one inline Lua applies it. All real
code, grounded in echo/apps/echo_store.
The message in flight
Walk the message from a write on one node to a dropped row on another. Node A mints a new version and writes; the 29-byte invalidation travels a lane to node B; node B’s conditional drop compares the incoming version against the one framed into its stored row and deletes only if the incoming is newer. Pick a stage to read what crosses the wire and the rule that decides.
Coherence · a message about a name
One write becomes one small message about a name; every other node decides newer or not by reading the clock minted into that name — no coordinator listening, no lock held, no second clock to keep.
The three dives
Coherence is small enough to read end to end. First the vocabulary — the 29-byte message and the cross-kind comparison that needs no clock. Then the two lanes that carry it and the substrates that apply each. Then the one inline Lua that makes either lane safe to deliver twice or out of order, and the journal that keeps its verdict durable past a crash.
A message about a name
payload/2, parse/1, newer?/2 — two names, twenty-nine bytes, and the eleven-byte comparison that holds across kinds with no clock.
The two lanes
Broadcast vs job, chosen by the cost of staleness: EchoStore.Ring at-most-once, EchoStore.Journal at-least-once.
Newer wins: the conditional drop
The pillar’s one Lua, in two beats: drop_l2/4, then the :coherence_drop body — one transition, so a late stale message can never erase a newer row.
Redis Patterns Applied
This is the depth behind the Redis Patterns Applied chapters that door here: R5 · Streams & Events — the bus-coherent invalidation that keeps a versioned fill current, and R1 · Caching — the cache-aside, stampede-prevention, and session patterns this pillar applies in real code. There the pattern is the door; here is the rule that keeps a fleet of those caches honest.
The lane that remembers
The job lane’s worker writes to Echo Persistence — a transactional
outbox standing beside the bus whose applied table is the lane’s memory of the
last version per name. It survives the node, the cache, and the bus, so a replayed old intent answers
stale from the journal even after L1 has forgotten the row. That is the durable floor a
crash-surviving coherence message folds into; the third dive opens the door.
Which executor carries it
Nothing in coherence runs on the native executor. The broadcast lane is a plain
PUBLISH; the drop is an inline Lua script; and the job lane’s
enqueue/5 is a lane enqueue, and lanes run only as scripts. The native module has no
grouped enqueue or grouped claim, and neither script names a native verb, so a
wire: 6 connection refuses both scripts.
References
Sources
- King — Announcing Snowflake — the time-ordered id whose byte order is the coherence comparison.
- Valkey — PUBLISH — the one wire hop of the broadcast lane,
ecc:{table}:coh. - Valkey — Cluster specification — the
{table}hashtag co-locating the coherence channel with the cache’s keys. - Helland — Life Beyond Distributed Transactions — the entity addressed by a name; coherence as a message about a name.
- Erlang/OTP — the ets module — the L1 row a newer version evicts.
Related in this course
- Single-flight & jittered TTL — the module before, where every row first gets its version.
- Cache-aside, two layers — the unconditional
invalidate/3admin drop this module guards against. - The Bus — the wire the broadcast lane and the job lane both ride.
- The Branded Component System · the Store — the manuscript chapter this pillar realizes.
- Echo Persistence — the durable floor beneath the volatile tiers.