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
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.
Declared, not discovered
The directory, owner monitoring, the ETS tier, the Valkey keyspace, the ecc:{table}:id hashtag.
The cache-aside read
fetch/3: the kind gate, the caller-side L1 lookup, the miss path, the three sources :hit | :l2 | :fill.
Writing both layers
put/3-4 frames version <> value into L2 + L1; invalidate/3 drops both unconditionally.
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
- Erlang/OTP — the ets module — the public, read-concurrent L1 table a hit reads directly, in the caller’s process.
- Valkey — Cluster specification — the
{table}hashtag lands everyecc:key on one of 16384 slots. - Valkey — GET — the L2 probe a miss issues to find the framed value.
- Valkey — SET — the write command that places
version <> valuewithPX ttl_ms. - Valkey — DEL — the L2 drop used by
invalidate/3. - Söderqvist — A new hash table (Valkey, 2025) — the L2 the near-cache fronts.
- Helland — Life Beyond Distributed Transactions — the entity addressed by a key, cached close to its use.
- King — Announcing Snowflake (2010) — the mint-time id every cached value is framed with.
Related in this course
- The Cache — the chapter landing for pillar III.
- The Protocol — the keyspace and branded-id gate the
ecc:prefix stands beside. - The Queue — the other pillar over the same wire.
- The Bus — the broadcast wire a coherence message travels over.
- The Branded Component System · the Store — the manuscript chapter this module realizes.
- Overview — the three pillars and the system as a whole.