jonnifyechomq

← 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

node A put/3 mints version id : version 29 bytes · two names, nothing else broadcast · PUBLISH ecc:{t}:coh job · Lanes.enqueue ecc.coh.t node B drop_l2/4 newer? → DEL ecc:{t}:id stored version <> value drop_l2/4 → {:ok, 1} dropped · {:ok, 0} kept

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.

built page ready to read

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

Related in this course