jonnify·redis /redis-patterns/caching

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 EchoCache — the declared near-cache, an L1 of ETS tables over the shared L2 Valkey (echo/apps/echo_cache) — in front of the Exchange Platform's instrument catalog; the chapter closes by caching that catalog tier end to end. This is the first chapter because caching depends on nothing else.

§1

Why & when

echo/apps/echo_cache

A 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 instrument listing read thousands of times between edits — 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.

The read path · an L1 hit serves from ETS, a miss falls through to L2 Valkey and the source
reader EchoCache the source fetch(id) miss → loader SET ecc:{t}:id PX

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.

§2

The patterns

six modules · one workshop

Six teaching modules, then a workshop. Each module is a hub with three dives.

built ready to read soon specified
§3

How to apply

name the constraint · the pattern follows

The hard part is not the commands; it is choosing the strategy. Name the constraint that matters most for a workload, and the pattern follows.

Pick the constraint · get the pattern
readout · Select a constraint to see which pattern matches and the trade-off it accepts.

There is no best caching pattern, only the one that matches the constraint you cannot relax.

§4

The workshop

echo/apps/exchange · EchoCache

The chapter closes with R1.07: cache the Exchange Platform's instrument catalog end to end. The instrument listing and a single instrument are served from EchoCache with cache-aside, kept consistent on edits through coherence, protected from a stampede on the most-viewed instrument, and measured for hit rate — the read path the later chapters build on.

Notes on Valkey. EchoCache 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

§5

Up next

R2–R8

After the read path comes coordination — the atomicity foundation the queue chapters need.

R2soon

Coordination

Atomicity first — locks and atomic Lua moves.

R3soon

Reliable Queues

Wait, active, done, recover.

R4soon

Time, Delay & Priority

The sorted set as a clock.

R5soon

Streams & Events

The durable log.

R6soon

Flow Control

Staying stable under load.

R7soon

Data Modeling

How data lives in RAM.

R8soon

Production & Ops

Running the tier at scale.

§6

References

sources · related

Sources

Related