Senior · Concurrency

Concurrent cache and stampedes

How do you avoid 1,000 identical cache misses triggering 1,000 database reads?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

Reveal a strong answer
  1. A map lock protects map consistency but does not collapse identical loads. Use keyed in-flight work so callers for one key can share a result, with explicit error and cancellation behavior.

  2. Avoid holding a global mutex across network I/O. Bound concurrent loads across distinct keys as well, because duplicate suppression alone does not limit a high-cardinality miss storm.

  3. Define expiration jitter, negative-cache policy, stale serving and invalidation. A cache key must include the tenant and all query dimensions that affect the result. Instrument hit rate, load latency and origin demand.

Common trap

Singleflight on one key does not solve many distinct misses or cross-replica stampedes.

Try it

One caller cancels while nine still await the same key. Should it cancel the shared load?

Worked solution

Not automatically. Separate each waiter’s cancellation from a bounded shared load lifetime, and decide how to stop work after all waiters leave. Document whether stale data is acceptable during a refresh.

Change the constraint

How would you prevent a slow old refresh from overwriting a newer cached value?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.