Walkthrough
Follow one key through a cold load, a peer read, a database update, and a Redis outage.
Three instances use the quickstart setup with the same namespace and Redis service. Each has its own L1. Redis holds L2 and carries Pub/Sub events. The database holds the authoritative user record.
Request → Server A: L1 ─┐
Request → Server B: L1 ─┼→ Shared Redis L2 → Database loader on a miss
Request → Server C: L1 ─┘
↑
Redis Pub/Sub carries invalidations and eligible loaded values1. A cold request loads user 42
Server A receives a request for user:42. Its repository calls:
const user = await cache.getOrSet('user:42', async ({ signal } = {}) => {
const row = await db.users.findById('42', { signal })
return row ?? undefined
})L1 misses. A registers the in-flight load for the key, then reads Redis. The script that finds user:42 empty also takes its per-key lease, so no other instance can fill the key in between and A runs the loader without reading again.
When the loader succeeds, one more script checks the lease token, writes the encoded value and TTL, and releases the lease. The whole miss is two Redis round trips. A lost lease cannot publish that result as a successful fill. L1 admission then retains the value when its memory budget allows it.
The execution paths explain the checks and failure branches in detail.
2. Concurrent callers share the work
More requests for user:42 arrive at A while its load is in flight. They join the same promise instead of starting another loader.
If B also misses, its Redis read finds A's lease held and B waits. A's set broadcast wakes B as soon as the value is stored, and B reads it from Redis instead of querying the database. In the benchmark in BENCHMARKS.md, run against an in-memory Redis double, the waiting server had the value a median 0.81 ms after the other's load returned, against 36.11 ms when polling every 50 ms without a bus. This wait is bounded. At the contention deadline, eligible stale data is returned or a DistributedLockTimeoutError reaches the caller.
Different keys use different leases. A bounded origin gate also limits concurrent loader work within each cache instance. See stampede protection for limits and timing.
3. The loaded value can warm peers
After a successful load, A publishes a set event carrying the value. B and C can admit it directly into their L1 without another database query. A filters out its own event.
Peer priming is conditional:
getOrSetloader results broadcast, including loads started throughprewarm. See the API comparison for lower-level writes.setupCachesets a 32 KiB broadcast ceiling. Oversized events are skipped, and peers can read L2 when needed. The low-level constructor has no ceiling unless you set one.- Delivery must succeed, and each peer's memory budget must admit the value.
By default L1 retains encoded bytes, and a later L1 hit decodes a private copy rather than returning a shared object reference. With valueMode: 'reference', L1 keeps the object itself and a hit skips the decode; see value modes.
4. A database write invalidates the key
Someone updates user 42. Commit the change before invalidating:
const updated = await db.users.update('42', { name: 'Ada Lovelace' })
await cache.invalidate('user:42')A removes the cached value and related local state, fences older in-flight work, and publishes a del event. Peers that receive it drop their local copies. The next read reloads the committed row.
Invalidating before the database commit could let a concurrent loader read and cache the old row again. Invalidation is still separate from the database transaction, so applications that must retry a failed invalidation need durable delivery logic of their own.
5. A late event arrives
Suppose C has applied generation 8 and then receives a set event carrying generation 7 for the same key. C discards the older event. Event IDs suppress duplicates, and source filtering prevents self-echoes.
These checks do not establish a global order across every writer or make pattern invalidation atomic. Read event ordering before relying on stronger consistency.
6. Redis becomes unavailable
Redis failures are treated as cache failures rather than authoritative answers. L2 can be skipped, and reads can reach the origin loader. Circuit breakers and bounded queues limit work against failing dependencies.
With managed Redis Pub/Sub, a subscription gap also makes local invalidation state untrusted. The cache bypasses L1 and local fallback state until reconnect recovery restores trust. Do not assume that every L1 hit remains usable during a bus outage.
The request can still fail if the origin fails, the work queue is full, or a deadline is reached without an eligible stale value. Fail-open caching does not guarantee a successful application response.
Where to next
- Production setup turns these behaviors into deployment settings.
- Failure handling covers timeouts, overload, and recovery.
- API reference provides signatures for the methods used here.