JavaScript incident
Latest query loses to stale search results
Failure signals
The clues you start with
Overview
What failed in production?
Start by making the bug legible: what the user saw, why it mattered, and what the product was doing underneath.
Symptom
User impact
Environment
Evidence pack
What you can inspect before answering
Request order
Current cache + response handling
const cached = cache.get(query);
if (cached) {
resultsStore.items = cached.items;
renderResults(resultsStore.items);
}
const res = await fetch(`/api/search?q=${query}`);
const data = await res.json();
resultsStore.items = data.items;
renderResults(resultsStore.items);Observed behavior
Stage 1: likely root cause
What is actually going wrong?
Stage 2: debug priority order
What would you check first? Put the best three at the top.
- #1
Reproduce on a slow network while logging the query text, whether data came from cache or network, and the request ids. - #2
Check whether cached results and network responses both write into the same shared results list. - #3
Confirm that the data being rendered belongs to an older query when the flash happens. - #4
Rewrite the search endpoint to return smaller payloads first. - #5
Increase debounce to 600ms before you understand the race.
Only the top 3 positions are scored.
Expected:
Expected:
Expected:
Stage 3: best fix set
Which changes should be part of the first robust fix?
Stage 4: regression guard
What is the best way to keep this from coming back?
You treated it like a traffic problem, not a correctness problem
Mostly right, but one safety check is still missing
You kept the latest query in charge
Ideal runbook
How a strong answer should flow
- 1
Reproduce the bug and log cached and network results together. - 2
Prove that one shared update path accepts old data after a newer query. - 3
Add guards so only the current query can update the list. - 4
Protect the flow with a cache-plus-delayed-response test.
Reflection
Reflection note
The easy trap
Interview lens
Stronger interview framing
- 1
Name it as an async race condition. - 2
Point out that both sources write into the same list. - 3
Explain why the latest query must always win.