Skip to content

.tip

ยท one of the small things

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 give the next caller that one.

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>;
}

That's one query per key per process, whatever the traffic. Across processes, use a short lock in Redis or accept a few duplicates.

cachingperformance