R1 · Caching
Caching —
the read path.
The most common Valkey use: serving reads fast, and keeping the cache honest when the source changes.
Six patterns from the read-path family — cache-aside, write-through, write-behind,
client-side caching, stampede prevention, and session storage — each with the trade-off that decides when to
reach for it. The grounding is EchoStore — the declared near-cache, an L1 of ETS tables over the shared
L2 Valkey (echo/apps/echo_store) — in front of the codemojex's emoji set;
the chapter closes by caching that catalog tier end to end. This is the first chapter because caching depends
on nothing else.
Why & when
echo/apps/echo_storeA cache trades freshness for speed. It pays off when reads vastly outnumber writes, when the source is slow or expensive, and when a small window of staleness is acceptable. It hurts when writes are frequent and must be seen immediately, when a stale read is incorrect rather than merely old, or when the working set does not fit in memory. The pattern you pick is a position on that trade-off — not a default.
Read-heavy catalog
An emoji-set listing read thousands of times between rounds — the canonical cache-aside case.
Fresh-after-write
A record that must reflect an edit on the very next read — write-through territory.
Write-burst smoothing
Updates written far faster than the database wants — write-behind buffers them through the journal.
Hot key under load
One popular key expiring while thousands ask for it — stampede prevention earns its place.
Every pattern in this chapter is a different answer to one question: when the cache and the source disagree, who wins, and for how long.
The patterns
six modules · one workshopSix teaching modules, then a workshop. Each module is a hub with three dives.
Cache-aside (lazy loading)
On miss, fetch and populate; on write, invalidate.
Write-through
Write cache and the source synchronously; reads stay fresh.
Write-behind (write-back)
Write Valkey now, sync the source asynchronously through the journal.
Server-assisted client-side caching
Cache in app memory; the server pushes invalidations.
Cache stampede prevention
Stop a thundering herd regenerating one expired key.
Session management
Store sessions with TTL expiry.
Workshop
Cache codemojex's emoji set end to end.
How to apply
name the constraint · the pattern followsThe hard part is not the commands; it is choosing the strategy. Name the constraint that matters most for a workload, and the pattern follows.
There is no best caching pattern, only the one that matches the constraint you cannot relax.
The workshop
echo/apps/codemojex · EchoStoreThe chapter closes with R1.07: cache codemojex's emoji set end to end. The emoji-set listing and a single set are served from EchoStore with cache-aside, kept consistent on edits through coherence, protected from a stampede on the most-viewed set, and measured for hit rate — the read path the later chapters build on.
EchoStore, in the manuscript
Part IV builds the near-cache the patterns apply: branded keys, the ecc:{table}:id keyspace, L1 ETS over L2 Valkey, coherence by mint time.
EchoStore, the running near-cache
The cache-aside read path this chapter teaches is walked rung by rung in the Cache pillar: EchoStore's two layers, single-flight fills, and jittered-TTL staleness on the real near-cache.
Notes on Valkey. EchoStore writes both layers with SET key value PX <ms> —
value and TTL set in one command; a separate EXPIRE would leave an immortal key if the writer died
between the two. valkey.io/commands/set
Up next
R2–R8After the read path comes coordination — the atomicity foundation the queue chapters need.
Coordination
Atomicity first — locks and atomic Lua moves.
Reliable Queues
Wait, active, done, recover.
Time, Delay & Priority
The sorted set as a clock.
Streams & Events
The durable log.
Flow Control
Staying stable under load.
Data Modeling
How data lives in RAM.
Production & Ops
Running the tier at scale.
References
sources · relatedSources
- Redis — Patternsthe canonical write-ups of the core caching access patterns
- Valkey — SETset a value with a PX expiry, the cache-aside miss-fill on the engine the connector is gated against
- Redis — Client-side cachingserver-assisted invalidation via CLIENT TRACKING
- Valkey — Client-side cachingthe RESP3 invalidation push EchoStore's coherence frames with a mint-time version
Related
- R0 — Overviewwhere Valkey sits under codemojex, and the reading protocol
- R2 — Coordinationthe next chapter — atomicity first
- The Branded Component SystemPart IV builds the EchoStore near-cache these patterns apply
- Functional Programming in Elixirthe functional-Elixir and OTP craft behind the echo data layer