Cache failure and recovery
What you need to know
Cache failure and recovery require a deliberate policy for stale data, origin errors, concurrency and capacity. A cache improves reuse but must not silently corrupt the application’s meaning.
By the end, you can: define stale and unavailable behaviors; plan observability.
Coalesce concurrent reads in a single process
const pending = new Map<string, Promise<string>>();function once(key: string, read: () => Promise<string>) { const active = pending.get(key); if (active) return active; const job = Promise.resolve().then(read).finally(() => pending.delete(key)); pending.set(key, job); return job;}Expected behavior: Calls for the same key while the first is pending share the promise. The map is process-local; this partial example omits capacity, timeout and generation guards.
How it works
A stampede occurs when several requests miss or refresh the same key and all call the origin. Coalescing identical in-flight reads can reduce duplicate work within one process; across replicas this requires a reviewed distributed strategy, not just a local promise map. Bound concurrency and choose a timeout policy. A failed background refresh can keep a previously approved public value only within the application's freshness and safety limits; when an entry is expired under the policy, return a controlled unavailable result if a fresh read fails. Never serve stale authorization decisions, private results or a purchase price as final merely to maintain availability. Track hit/miss, origin latency, stale age, invalidation failures and error counts without logging credentials. The model uses an explicit clock, a finite fixture, injected failures and a single-flight map. It tests a possible application policy, not the internal Next.js behavior. Remove in-flight entries after either success or failure so later attempts can recover. If invalidation can remove and replace an in-flight job, its cleanup must check that the map still contains that exact job before deleting it. A late result must also be fenced against the current generation before populating a cache. Neither guard makes a process-local map a distributed cache.
Define the recovery behavior before introducing a cache
Public entry age 400, expire 1800, refresh fails -> may keep last approved value under lab policyPublic entry age 1800, fresh read fails -> unavailablePrivate read fails -> unavailable, never substitute another user’s dataNext eligible retry -> bounded and observableExpected behavior: The outcomes are an explicit teaching policy, not universal framework guarantees.
Common mistake
Do not catch every cache or origin failure and return an arbitrary default as if it were fresh. Do not retain rejected promises forever or start unbounded retries. Cache availability must not override correctness or authorization.
Practice and verification
Run the independent model tests for concurrent misses, failure cleanup, retry and expiration. In an actual Next.js lab, measure origin calls and visible results using the approved cache adapter, and document which failure policies are observed versus application-defined.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components enabled in a separate fixture. Framework snippets are staged and not executed; local pure models are not framework evidence. Exact patched dependencies and platform contracts are pending.