Caching
Metastable failure, fencing tokens, and production postmortems
A stampede lasts 10 seconds. Four hours later the DB is still at 100% CPU. The stampede is over — but the system cannot self-recover. This is the metastable failure pattern: once the system falls into the retry storm, it stays there.
The metastable failure sequence
A cache stampede can escalate into a self-sustaining failure:
- T=0 — TTL fires, herd hits DB. DB CPU saturates.
- T=0–5s — queries queue. Queue depth grows past client request timeout (e.g., 5 s).
- T=5s — client timeouts begin. Most client SDKs auto-retry with exponential backoff. Backoff is short (100 ms to 1 s) because the original timeout looked like a transient network failure, not an overloaded server.
- T=5–30s — retries multiply the herd. Original 10,000 misses become 30,000 in-flight requests (3 retry attempts per client). DB is now serving retries instead of fresh queries. Cache rebuild cannot complete because DB is too busy to answer rebuild queries.
- T=4h — system remains saturated. Every new arrival retries. Retries prevent DB recovery. DB prevents cache rebuild. Cache emptiness generates more retries. Self-sustaining loop.
The system has two stable states: healthy (cache full, low DB load) and storm (cache empty, DB at 100%). A perturbation moved it from healthy to storm. It cannot move back on its own.
Why the system cannot self-recover
The loop is: no cache → retries → DB overload → no DB capacity → no cache rebuild → no cache. Each component is correctly following its designed behaviour. No component “knows” it is in a storm. The client SDK retries because it sees timeouts. The DB processes queries in order. The cache does not rebuild because the DB never answers fast enough.
Recovery requires breaking the loop from outside:
- Load-shedding at the gateway — return
503 Service Unavailableimmediately when DB CPU exceeds 90%. Most client SDKs treat 503 as “do not retry immediately” and back off with long jitter. After 30–60 s the queue drains, the DB recovers, and the cache rebuilds. - Circuit-breaker on the rebuild path — if the rebuild query fails or takes too long, serve
stale foreveruntil the DB recovers. This breaks the dependency between cache rebuild and DB health. - Manual cache warmup — operators write known-good values directly to the cache, bypassing the rebuild path. Immediately returns the system to the healthy state.
| Recovery mechanism | How it breaks the loop | Side effect |
|---|---|---|
| 503 at gateway | Clients stop retrying | 503s visible to users for 30–60 s |
| Circuit-breaker | Rebuild stops depending on DB | Stale data served indefinitely |
| Manual warmup | Inserts cache values directly | Requires operator action |
The fencing token fix for lock TTL races
A Redis lock with SET lock:key uuid EX 30 NX has a race condition when the rebuild outlasts EX:
- Request-1 acquires lock with uuid-A, EX=10 s. Starts a 12-second rebuild.
- T=10 s: lock auto-expires. Request-2 acquires lock with uuid-B.
- T=12 s: request-1 finishes rebuild. Writes to cache.
- T=12 s: request-2 also finishes rebuild. Writes to cache.
- Result: duplicate writes. If there is a concurrent invalidation from a DB write, request-1’s stale value may overwrite a newer-correct value.
Fix 1: increase EX above rebuild p99.9. Most duplicates prevented. Not proof against all races.
Fix 2: fencing token check before write.
# Before writing the rebuilt value:
current_lock = GET lock:key
if current_lock != my_uuid:
ABORT # we lost the lock; someone else is rebuilding
ELSE:
SET cache:key new_value EX 60 # write only if still holding lockFix 3: monotonic version per key. Include a version in every cache write. The DB or cache layer rejects writes with version ≤ current version. Stale rebuilds write with an old version and are silently rejected. Martin Kleppmann’s 2016 critique of RedLock formalised this: distributed locks alone are insufficient for correctness; fencing is required.
XFetch: why the exponential is optimal
Vattani et al. (VLDB 2015) prove no coordination-free algorithm can do better than exponential at keeping expected refreshes per TTL window at exactly 1. Any tighter rule either:
- Requires coordination (distributed lock, lease) to bound variance, or
- Accepts higher variance (some windows get 0 refreshes → stampede, some get many → wasted work).
The exponential is uniquely optimal because: the minimum of N independent Exp(λ) draws is Exp(N·λ). As fleet size N grows, the “winning” reader fires sooner — but the expected total remains 1. No other distribution has this scaling property while remaining memoryless (no state required per-reader).
Four production postmortems
Reddit 2017. A feed-cache change shipped without single-flight. Result: 3× DB peaks at every TTL boundary. On-call paged on DB CPU 30 minutes after deploy; rolled back within 1 hour.
Shopify 2020 (Black Friday). A flash-sale homepage had TTL=30 s and no SWR. At 12:00:30 the TTL fired under 10× normal traffic. The entire storefront edge had multi-second outages at every TTL boundary for the first 10 minutes of the sale. Fixed by deploying stale-grace=5 min on the next push.
Twitter 2022. An unrelated cache invalidation bug triggered 30M concurrent rebuild attempts. The DB connection pool saturated for 4 hours. Recovery required load-shedding + manual cache seed.
Cloudflare 2024. A KV cache coalescing bug briefly allowed N requests for the same key to bypass coalescing during a deploy window. The origin saw proportional load during the window. Fixed by deploying a guard flag that disabled coalescing rollout until the bug was patched.
Pattern across all four: stampede protection was either absent, misconfigured, or had a bug that defeated it under load. The cost was proportional to the protection gap.
Full-stack composition design
A production cache stack for a global high-traffic site needs protection at every tier:
| Tier | Mechanism | Stampede reduction |
|---|---|---|
| CDN edge | SWR + request coalescing | N concurrent misses → 1 origin request |
| Application cache (Redis) | XFetch + single-flight + lock | N concurrent misses → 1 rebuild per TTL window |
| DB | Circuit-breaker, read replicas | Origin load shedding during overload |
Each tier must independently bound its herd — a failure at any tier cascades to the one below. SWR at the CDN absorbs 99% of TTL-boundary traffic. XFetch prevents the next layer’s stampede by refreshing before expiry. Single-flight collapses per-node herds. The Redis lock collapses cross-node herds for the hottest 1% of keys. TTL jitter desynchronises multi-key boundaries. Together they reduce 10,000 concurrent misses to 1 rebuild anywhere in the stack.
Why this works
Bouman et al. (SOSP 2024) formalised the metastable failure pattern as a class: a system in a metastable failure is stable in both the healthy and storm states, and a perturbation moves it between them. The transitions are: healthy → storm (stampede + retry amplification) and storm → healthy (external intervention). Their key result: any system with at-least-once retries and no load-shedding can be pushed into metastability by a sufficiently large transient overload. The fix is an explicit “kill the herd” mechanism (503-on-overload, circuit-breaker) that is separate from the caching logic.
A system enters metastable failure after a stampede. DB CPU stays at 100% for 4 hours. The cache is empty. What is the correct diagnosis?
A Redis lock uses EX=10 s, but rebuilds can take up to 15 s. A rebuild starts at T=0 s. What is the exact failure at T=10 s?
A global news site needs stampede protection at every cache tier. Which stack satisfies: p99 latency under 200 ms during 10× viral spikes AND DB never sees more than 5× steady-state QPS?
- 01Explain the metastable failure shape that follows an unmitigated cache stampede and why the system cannot self-recover.
- 02A Redis lock with EX=10 s produces duplicate writes when rebuilds exceed 10 s. Explain the three-component fix.
- 03Why does XFetch's 'expected 1 refresh per TTL window' property hold regardless of fleet size, while a distributed lock requires explicit cross-node coordination?
A cache stampede that overloads the DB can escalate into a metastable failure lasting hours: client retries from timed-out requests sustain the load that prevents DB recovery, which prevents cache rebuild, which generates more retries. Breaking the loop requires an external mechanism — 503-on-overload at the gateway, circuit-breaking the rebuild path, or manual cache seeding. Distributed lock races (EX too short) require three defences: extended EX, fencing token verification before write, and monotonic version on cache keys. The full production stack layers SWR at the CDN edge, XFetch at the application cache, single-flight per process, and a Redis lock for the hottest keys — each tier reducing the herd by an order of magnitude. Four real incidents (Reddit 2017, Shopify 2020, Twitter 2022, Cloudflare 2024) all share one pattern: protection missing, misconfigured, or defeated under load.
appears again in202
- Why GraphQL gets N+1junior
- DataLoader mechanics: tick-boundary batchingmiddle
- Batch function contracts: ordering, shapes, errorsmiddle
- Federation and lookahead: batching beyond DataLoadermiddle
- Query complexity defences: depth, cost, persisted queriesmiddle
- Senior GraphQL API: scheduling contract, tenant isolation, observabilitysenior
- Why idempotency: making retries safejunior
- Server-side state machine: four states of an idempotency keymiddle
- Outbox and inbox: effectively-once across the dual-write boundarymiddle
- Concurrency and cache architecture for idempotency at scalesenior
- Observability, production failures, and global-scale designsenior
- The event loop: one thread, three queuesjunior
- Tasks, microtasks, and scheduler.yield()middle
- Microtask starvation, Long Tasks, and LoAFsenior
- Node.js event loop: phases, nextTick, and loop lagsenior
- React, Vue, and INP observability in productionsenior
- The render pipeline: six stages from bytes to pixelsjunior
- Stage costs and the renderer process modelmiddle
- Invalidation, dirty bits, and containmiddle
- Compositor layers: promotion, overlap, and GPU memorymiddle
- DevTools flame strip and the frame lifecyclemiddle
- Layout thrash: forced synchronous layoutsenior
- BeginMainFrame, compositor-driven animations, and GPU memorysenior
- Production observability: LoAF, INP, and the full attack surfacesenior
- What V8 is and why performance varies 100×junior
- V8''''s four-tier JIT pipeline and profile-guided tieringmiddle
- Hidden classes, transition trees, and memory layoutmiddle
- Inline caches, IC states, and deoptimizationmiddle
- Orinoco GC: parallel scavenger, concurrent marking, and write barriersmiddle
- TurboFan''''s speculative engine and the deopt-loop trapsenior
- V8 in production: isolates, pointer compression, and real failuressenior
- What workers are and why they existjunior
- Web worker mechanics: dedicated, shared, and OffscreenCanvasmiddle
- Structured clone and transferablesmiddle
- Service worker lifecycle and cache strategiesmiddle
- SharedArrayBuffer, Atomics, and cross-origin isolationsenior
- Service worker edge cases: version skew, durability, and navigation trapssenior
- Worker pools, Comlink, and production observabilitysenior
- What the reconciler does: render vs commitjunior
- The fiber object and the double-buffer treemiddle
- Render phase purity and commit phase sub-stepsmiddle
- Reconciliation: diffing heuristics and the key trapmiddle
- Priority lanes, time-slicing, and useTransitionmiddle
- Bailout, memoisation, and tearingsenior
- React Profiler, the Compiler, and production observabilitysenior
- Rendering strategies: SSG, SSR, ISR, streaming, and hydrationjunior
- SSG, SSR, ISR, streaming, and RSC — how each worksmiddle
- Hydration cost: selective, progressive, islands, resumabilitymiddle
- Hydration mismatch: causes, detection, and the determinism rulesenior
- RSC, per-route strategy, and production observabilitysenior
- Core Web Vitals: what LCP, INP, and CLS measurejunior
- CLS: why layout shifts happen and how to stop themmiddle
- Metric tradeoffs, RUM attribution, and the CI+field loopsenior
- The full picture: URL to LCP to INP as a relay racejunior
- Eight layers traced: from the service worker to the second navigationmiddle
- Five canonical breaks: where production reliably diessenior
- The three-track method: reading traces and building a monitored systemsenior
- What a relation is: tables, rows, keys, and constraintsjunior
- Constraints, keys, and Postgres data typesmiddle
- Normal forms, denormalization, and why schemas stickmiddle
- JSONB, arrays, and when a side table winsmiddle
- Heap storage, TOAST, and column alignmentsenior
- Schema integrity: deferral, versioning, and production failure modessenior
- Relational vs document, wide-column, graph, and key-valuesenior
- Index-only scans, the Visibility Map, and INCLUDEsenior
- Production failure modes and the index audit playbooksenior
- pg_statistic, ANALYZE, and production observabilitymiddle
- Production failure modes and plan stabilitysenior
- MVCC: why readers and writers never wait for each otherjunior
- Row versions and snapshots: the on-disk mechanicsmiddle
- HOT updates and isolation levels: what you gain and what you paymiddle
- Vacuum and bloat: keeping the storage tax boundedmiddle
- CLOG, XID wraparound, and MultiXact: deep visibility internalssenior
- SSI internals and production autovacuum tuningsenior
- Real-world MVCC failures, deployment patterns, and distributed snapshotssenior
- Connection pools: amortising the cost of a Postgres backendjunior
- PgBouncer session, transaction, and statement modesmiddle
- Pool sizing: the (cores × 2) + spindles formula and the two-layer stackmiddle
- Pool exhaustion and idle-in-transaction: the 3 AM failure modemiddle
- Migrating to transaction mode: rollout playbook and PgBouncer 1.21 prepared statementsmiddle
- The Postgres process model and why raising max_connections degrades throughputsenior
- Pooler landscape 2026, serverless connection storms, and the full failure-mode taxonomysenior
- What a schema migration is and why it replaces ad-hoc DDLjunior
- ADD COLUMN: instant in PG 11+ vs rewrite in older Postgresjunior
- The lock-queue failure mode: why instant DDL can freeze the databasemiddle
- Safe DDL patterns: NOT VALID, CONCURRENTLY, and unsafe-op fixesmiddle
- Expand-contract: zero-downtime for breaking schema changesmiddle
- Advisory locks, migration tools, and deploy coordinationsenior
- Migration failure taxonomy and production disciplinesenior
- Why sharding exists: the single-Postgres ceilingjunior
- Shard-key selection: hash, range, list, and directory strategiesmiddle
- Partitioning vs sharding: same word, two different thingsmiddle
- Co-location and Citus: the invariant that makes sharding usablemiddle
- The hot-shard failure mode: detection, isolation, and durable policymiddle
- Schema-based sharding and multi-tenancy alternativessenior
- Online resharding, 2PC, and the operational cost of shardingsenior
- The seven acts: from CREATE TABLE to Citusjunior
- Acts 1–3 in depth: schema, indexes, and planner statisticsmiddle
- Acts 4–6 in depth: MVCC bloat, connection pooling, and safe migrationsmiddle
- Act 7 in depth: sharding, co-location, and the seven-tier tradeoff cascademiddle
- Observability, anti-patterns, and production triagesenior
- Raft roles, terms, and why majority quorums prevent split brainjunior
- How Raft replicates a log entry and decides it is safe to commitmiddle
- Raft leader election: timeouts, voting rules, and the four safety propertiesmiddle
- Raft in the real world: partitions, slow disks, and client routingmiddle
- Raft extensions: pre-vote, learners, snapshots, and linearizable readssenior
- Raft in production: membership changes, Multi-Raft, and observabilitysenior
- Where data fetching happens — and why it decides LCPjunior
- Fetch waterfalls — diagnosis and the Promise.all curemiddle
- React Server Components and Suspense streamingmiddle
- Client-side cache: TanStack Query, SWR, and stale-while-revalidatemiddle
- LCP, prefetch, and race conditions in interactive fetchingmiddle
- Senior internals: RSC payload, caching layers, and production failure modessenior
- The IP envelopejunior
- Reading the IP headermiddle
- The three-way handshakejunior
- Sequence numbers and connection statemiddle
- DNS: what it does and why it existsjunior
- The resolver walk: referrals, record types, and gluemiddle
- TTL, caching, and DNS propagationmiddle
- What TLS does and why it existsjunior
- The 1-RTT handshake: key shares and ECDHEmiddle
- Session resumption and 0-RTTmiddle
- Key schedule, SNI, ALPN, and extensionssenior
- 0-RTT defenses, ECH, hybrid PQ, and production TLSsenior
- WebSocket: the HTTP upgrade handshakejunior
- WebSocket frame format: opcodes, masking, fragmentationmiddle
- WebSocket backpressure: when clients can''''t keep upmiddle
- Reconnection: jittered backoff, thundering herd, message resumptionsenior
- WebSocket at scale: HTTP/2 multiplexing, permessage-deflate, C10Msenior
- WebSocket in production: proxies, security, and distributed architecturesenior
- What reverse proxies dojunior
- Health checks, connection draining, and slow startmiddle
- Session affinity, consistent hashing, and the right fixmiddle
- Retry storms, circuit breakers, and load sheddingsenior
- Resilient LB architecture: anycast, zone-aware routing, and observabilitysenior
- Why QUIC and not TCP+TLSjunior
- Connection IDs and network migrationmiddle
- 0-RTT resumption and packet encryptionsenior
- DDoS: what it is and why it worksjunior
- Amplification attacks and state exhaustionmiddle
- Rate limiting: algorithms and architecturemiddle
- WAFs, firewalls, mTLS, and HSTSmiddle
- DNS cache poisoning and BGP hijackingsenior
- Defense-in-depth architecture and attack economicssenior
- The twelve layers: one URL, seven actorsjunior
- DNS, TCP, TLS in sequence: where the milliseconds gomiddle
- Proxy intercepts and security gates: rate limiters, WAF, mTLSmiddle
- Alternate paths: QUIC 0-RTT, WebSocket upgrade, connection migrationmiddle
- Observability: distributed traces, USE/RED, and samplingsenior
- Resilience: cascading retries, circuit breakers, and error budgetssenior
- What the three signals are: logs, metrics, and tracesjunior
- Why structured logs exist: the diary vs the spreadsheetjunior
- The production log schema: fields every line must carrymiddle
- PII redaction and log injectionsenior
- OTel Logs Data Model and audit logs as a subsystemsenior
- What is OpenTelemetry: API, SDK, Collector, OTLPjunior
- OTel signals, Semantic Conventions, and the OTLP wire formatmiddle
- The OTel Collector: receivers, processors, exporters, and deployment patternsmiddle
- Vendor neutrality, eBPF instrumentation, the Operator, and browser/serverless OTelsenior
- Operating the OTel Collector: reliability, version skew, failure modes, and governancesenior
- SLI, SLO, and the error budget: reliability by the numbersjunior
- Error budget policy, latency SLOs, and composite journeysmiddle
- Production SLO failures, self-observability, security, and the big picturesenior
- What is trace propagation and why broken propagation is worse than nonejunior
- traceparent and tracestate: the W3C header format in fullmiddle
- Baggage and async boundaries: carrying context across queues and callbacksmiddle
- Async context per language, service mesh, B3 migration, and securitysenior
- Production propagation failures, span links, and platform designsenior
- The debugging funnel: SLO → RED → trace → profilejunior
- OTel architecture: one SDK, four signals, one wire formatmiddle
- The incident loop: from pager to postmortem to preventionmiddle
- Scale, security, and the ROI of observable systemssenior
- Cache lines, struct layout, and false sharingmiddle
- SIMD, SoA vs AoS, and memory bandwidthmiddle
- Cache-oblivious algorithms, PGO, and production failuressenior
- GC in production: observability, security, edge cases, and fleet governancesenior
- Batching: amortize fixed cost per operationjunior
- The batching window: size and wait timemiddle
- Batching in Kafka and Postgresmiddle
- io_uring and observability of batchingmiddle
- From Nagle to io_uring: evolution of batchingmiddle
- Backpressure, failure isolation, and batch security in productionsenior
- CI enforcement and RUM: making budgets stickmiddle
- V8 JIT pipeline, HTTP priorities, and bundle securitysenior
- The performance loop: discipline, not a projectjunior
- Classify and fix: matching bottleneck families to remediesmiddle
- Observability stack and CI gates: catching regressions before they shipmiddle
- Incident to enforcement: SLO burn to verified fix in 35 minutesmiddle
- Culture, economics, and org-scale performancesenior
- At-most-once, at-least-once, exactly-once: the three delivery contractsjunior
- The three failure legs — where duplicates and losses actually happenmiddle
- Consumer-side dedup: the cheapest path to exactly-once processingmiddle
- Kafka exactly-once semantics: idempotent producer and transactionsmiddle
- SQS visibility timeout, DLQ, and the outbox patternmiddle
- Exactly-once in production: impossibility proof, hybrid patterns, and real incidentssenior
- What OAuth is and why passwords are not the answerjunior
- Authorization code flow with PKCEmiddle
- ID token validation and JWKS cache managementmiddle
- Refresh token rotation and scope-based least privilegemiddle
- Sender-constrained tokens: DPoP and mTLSsenior
- OAuth in production: audience attacks, observability, and real failuressenior