jonnifyechomq

← the depth behind redis-patterns R1 · Caching

The Cache · pillar III

Serve reads
from the near-cache

Answer from memory when you can, fall through to the store when you must — and never let a herd fill it twice.

The Cache is the third pillar, and it stands in front of the other two. Where the Queue distributes work and the Bus broadcasts signals, the Cache serves reads. It is a declared near-cache — an L1 ETS table in front of the L2 Valkey the bus already runs on — read cache-aside: a hit is a caller-side :ets.lookup that never enters the owning process, and the owner is consulted only on a miss, where the second law holds: one fill per herd. Concurrent misses coalesce onto a single loader; expiry is jittered so no cohort dies together; and a full cache degrades to pass-through, it never fails. Every value is framed with its writer's mint-time version — the seed of the coherence that keeps the caches honest. It is all real code, grounded in echo/apps/echo_store.

The read, cache-aside

A cache-aside read tries the layers in order and stops at the first that answers. The fast path never leaves the caller: a hit is a lookup in a public ETS table, in your process, costing nothing but the lookup. Only a miss consults the owner — and there a miss in front of a thundering herd becomes one fill, not a thousand. Pick a path to read what happens, the Valkey command beneath it, and the source the fetch/3 answer is tagged with.

Cache-aside · pick a path

caller fetch(id) + N concurrent misses all coalesced → 1 flight L1 · ETS :ets.lookup caller-side ttl ± ttl·jitter :sweep reclaims owner single flight {:fill, id} miss L2 · Valkey GET ecc:{q}:id loader.(id) SET … PX ttl nil fetch/3 → {:ok, value, :hit | :l2 | :fill}

One read, tried in order: L1 answers in your own process, L2 answers from the shared store, the loader answers once for the whole herd — and the clock, jittered, makes sure no cohort ever asks again all at once.

The modules

Three modules and the closing workshop are ready — each module a hub and three dives, the workshop one end-to-end build. Each grounds in the real echo/apps/echo_store code — the declared tiers and the cache-aside read, then single-flight and the jittered clock that keep it cheap under a herd and bounded under pressure.

built page ready to read

Redis Patterns Applied

This is the depth behind the Redis Patterns Applied chapters that door here: R1 · Caching — the cache-aside, stampede-prevention, and session patterns this pillar applies in real code, and R5 · Streams & Events — the bus-coherent invalidation that keeps a versioned fill current. There the pattern is the door; here is the near-cache it names.

The version on every row

A cache that serves a read must also learn when that read goes stale. Every value here is framed with its writer's mint-time version — a 14-byte branded id stored as version <> value — and coherence is the rule that compares two such versions and lets the newer win, with no coordinator and no clock but the one inside every id. That is the third module. The manuscript chapter these figures realize is The Branded Component System · the Store; the durable floor a coherence job survives a crash on is Echo Persistence.

The cache and the two executors

The cache sends ordinary Valkey commands: GET, SET … PX and DEL on its ecc: keys, PUBLISH on the broadcast lane, and CLIENT TRACKING for server-assisted invalidation. Two of its moves are scripts: the conditional drop is an inline Lua script, and the job lane enqueues through EchoMQ’s fair lanes, which run only as scripts. Every connection it opens is an EchoMQ connector on its default wire 4, which reads {emq}:version on connect: it seeds the wire 4 brand when the key is absent and stops on any other brand. Neither script names a native verb, so a wire: 6 connection would refuse both, and the cache serves from a wire 4 node. Trafikist teaches the native executor.

The rest of the system

The Cache is one reading of the shared substrate. The Queue distributes work over the same wire; the Bus broadcasts signals and keeps a replayable log; the Proof chapter shows the whole system holds. The Protocol below them all — the keyspace the ecc: prefix stands beside — is ready to read now.

Every chapter is ready to read: the Protocol, the Queue, the Bus, the Cache and the Proof.

References

Sources

Related in this course