jonnifyechomq

The Cache · module 01

Cache-aside,
two layers

Declare the near-cache, serve reads from ETS, fill from Valkey on a miss, frame every value with its version.

The near-cache is declared, not discovered. Every table registers its full specification in the directory at start — kind, TTL, loader, coherence mode — and a cache absent from the directory does not exist. The read path is cache-aside at ETS speed: a hit stays in the caller’s process, a miss falls through to the L2 Valkey, and if that misses too, the declared loader runs. A write frames every value with its mint-time version — the seed of the coherence that module 03 builds on. All real code, grounded in echo/apps/echo_store.

The declared near-cache, layer by layer

Pick a component to see its role. The cache is declared at the top; the read path tries each layer in order and stops at the first that answers; the write frames both layers with the same mint-time version. All three components are real: L1 is an ETS table in the caller’s process, L2 is Valkey keyed ecc:{table}:id, and the version is a 14-byte branded id.

Cache-aside, two layers · select a component

EchoStore.Directory register/3 · tables/0 · monitors owner L1 · ETS :public :named_table caller-side lookup L2 · Valkey ecc:{table}:id version <> value loader.(id) declared 1-arity fun nil fetch/3 → {:ok, value, :hit | :l2 | :fill}

A cache that is declared is a cache that can be enumerated, monitored, and governed — the directory is the guarantee that what exists is always knowable.

The three dives

Each dive grounds in the real echo/apps/echo_store code — the directory and the two tiers, then the read path from the kind gate through the L2 frame, then the write that frames both layers with a mint-time version. Follow them in order or start at the one that matters most.

Redis Patterns Applied

This module is the depth behind two chapters in Redis Patterns Applied: 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

Every value in this module is framed with a 14-byte mint-time version — the seed of coherence. The third module, Coherence, builds the newer-wins comparison on exactly that version. The manuscript chapter these figures realize is The Branded Component System · the Store.

References

Sources

Related in this course