The cache hands the person who filtered by one city the list somebody else asked for.
Context, code, logs and metrics, the same material you would have in production, with nobody pointing at the problem.
No multiple choice and no single right answer: you write what you think is happening, what you would do and what it costs.
The moment you submit, the solution written by an engineer opens next to your answers. Nobody scores you: the comparison is yours to make, and it stays open to reread.
The jobs-api serves the job search on the site. The URL accepts ?city=, ?level= and ?page=, and since the query takes about 300ms, somebody put a five minute cache in front of it.
const cache = new Map<string, { expiresAt: number; body: JobsPage }>();
export async function listJobs(req: Request, res: Response) {
const page = Number(req.query.page ?? 1);
const key = `jobs:page:${page}`;
const cached = cache.get(key);
if (cached && cached.expiresAt > Date.now()) {
return res.json(cached.body);
}
const body = await searchJobs({
city: req.query.city,
level: req.query.level,
page,
});
cache.set(key, { expiresAt: Date.now() + 5 * 60 * 1000, body });
return res.json(body);
}A few times a day support gets the same complaint, "I filtered by Recife and a São Paulo job showed up", and nobody on the team can reproduce it, because opening the reported URL returns the right result.
The access log caught these two consecutive requests, from different people:
10:02:11 ?city=sao-paulo&level=pleno&page=1 MISS 318ms
10:02:19 ?city=recife&level=junior&page=1 HIT 1msWhat opens
Reading is free; answering is what uses a credit. Every new account starts with 3, no card.