Tutorial

Single Flight Dedup

Collapse concurrent identical expensive calls into one in-flight work so the result is computed once and shared with all waiters.

On this page

  1. Introduction
  2. Key by args
  3. Await the existing
  4. Clear on settle
  5. Share success and failure
  6. Combine with cache
  7. Test concurrency
  8. Sources

Introduction

This guide covers single flight deduplication, a recurring concern in the resilience scenario. When a cache misses and ten requests arrive at once for the same key, you do not want to compute the expensive thing ten times. Single flight collapses them into one in-flight computation, and every waiter gets the same result, which is a cheap way to avoid a thundering herd on a cold cache.

Key by args

Build a key from the request arguments so two calls with different args are not collapsed, which would be a silent bug.

The trade-off worth naming is that build a key from the request arguments so two calls with different args are not collapsed, which would be a silent bug adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const inflight = new Map();
async function singleFlight(key, fn) {
  if (inflight.has(key)) return inflight.get(key);
  const p = fn().finally(() => inflight.delete(key));
  inflight.set(key, p);
  return p;
}

Await the existing

If a key is in flight, return the same promise so the caller waits on the shared computation rather than starting its own.

The trade-off worth naming is that if a key is in flight, return the same promise so the caller waits on the shared computation rather than starting its own adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Clear on settle

Delete the in-flight entry in a finally so a settled promise does not stay forever, which would block future calls.

The trade-off worth naming is that delete the in-flight entry in a finally so a settled promise does not stay forever, which would block future calls adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Share success and failure

All waiters receive the same result or rejection, so the work is done once whether it succeeds or fails, and each can retry independently after.

The trade-off worth naming is that all waiters receive the same result or rejection, so the work is done once whether it succeeds or fails, and each can retry independently after adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Combine with cache

Use single flight behind a cache so a miss computes once and the result is then cached, which stops the herd on a cold key.

The trade-off worth naming is that use single flight behind a cache so a miss computes once and the result is then cached, which stops the herd on a cold key adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function get(key, fn) {
  const hit = cache.get(key);
  if (hit) return hit;
  const v = await singleFlight(key, fn);
  cache.set(key, v);
  return v;
}

Test concurrency

Fire the same key ten times at once and assert one computation and ten equal results, so single flight is real.

The trade-off worth naming is that fire the same key ten times at once and assert one computation and ten equal results, so single flight is real adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: singleflight, Thundering herd.

FAQ

Single flight or request coalescing?
They are the same idea under different names; the point is one in-flight computation per key, so concurrent callers share the work instead of duplicating it.
What if the one call fails?
All waiters receive the rejection, which is correct; each can retry on its own, and the next single-flight batch tries again.

Related Examples