Coalesce cache misses so one key loads once
When a hot key expires, every request that misses in those few milliseconds runs the same slow query. Keep the in-flight promise in a map and hand the same one to the next caller.
const inflight = new Map<string, Promise<unknown>>();
function load<T>(key: string, fn: () => Promise<T>): Promise<T> {
let p = inflight.get(key);
if (!p) {
p = fn().finally(() => inflight.delete(key));
inflight.set(key, p);
}
return p as Promise<T>;
}One query per key per process, whatever the traffic. Across processes, use a short lock in Redis or accept a few duplicates.
cachingperformance