Single-Flight-Based Cache Miss Coalescing
Single-Flight is a concurrency pattern that merges overlapping calls for the same key into a single execution.
Β· Practice memo Β· 32 min read Β· Easy
Single-Flight is a concurrency pattern that merges overlapping calls for the same key into a single run. When 100 requests for one product pile up the moment the cache is empty, the DB query runs once and the rest receive that run's value or error.
Patterns like Cache-Aside are straightforward under normal conditions, but they reveal a weakness during the brief window when a popular key expires.1 When multiple requests see the same cache miss and each independently falls through to the database or an external API, the load that the cache was absorbing all floods the origin storage at once. This phenomenon is called a cache stampede, or thundering herd. Google Zanzibar addressed this problem by maintaining a per-server lock table, ensuring that only one request among those sharing the same cache key is allowed to begin processing.2
The golang.org/x/sync supplementary module for Go projects includes a singleflight package that implements exactly this behavior. It is not part of the standard library, and the package documentation describes it as "duplicate function call suppression." If a task with the same key is already in progress, subsequent calls wait until the initial execution finishes and receive the same value and error.3
The internal execution model is also worth comparing. In Group.Do, the first caller executes fn directly in its own goroutine. Duplicate goroutines arriving with the same key release the mutex that protects the map, then wait for the result at WaitGroup.Wait(). The global mutex is not held throughout the loader execution. DoChan, by contrast, dispatches the work to a separate goroutine via go g.doCall(...) and immediately returns a result channel. The claim that "Go singleflight never creates background goroutines" holds true for Do but is incorrect when DoChan is included. (Personally, this is still a point of confusion for me, since I'm not yet fluent with Go.)4
What this registry stores is not a completed value but an execution that has not yet finished. Once the task completes, the in-flight entry is removed. TTL, eviction, and stale data remain concerns of the cache policy; Single-Flight only reduces duplicate executions during the overlapping window.
Core formula:
Single-Flight
= N concurrent jobs sharing the same key
β only 1 actual loader execution
β N waiters share the result
In-Flight Registry
= Key β the Promise / Task / Future currently in progress
Cache
= retains completed values for a set duration
Single-Flight
= only temporarily shares work that hasn't completed yet
Invariants:
For the same key, at most one loader runs at a time.
Loaders for different keys run independently of each other.
Once a loader finishes, its entry is removed regardless of success or failure.
If one waiter leaves, the loader still runs for the remaining waiters.1. Problem scenario
Suppose a product detail service uses this cache-aside flow.
Product lookup request
β cache lookup
β if cache hit, return immediately
β if cache miss, query DB
β store in cache
β return resultUnder normal conditions this works without issue. However, if 100 requests arrive simultaneously at the moment the cache for the popular item product-42 expires, the system can behave as follows. (After all, 42 is the answer to life, the universe, and everything, so of course it would be a popular item.)
Request 1 cache miss β DB query
Request 2 cache miss β DB query
Request 3 cache miss β DB query
...
Request 100 cache miss β DB queryHere is the behavior we want.
Request 1
β acquires the loader execution lock for product-42
β performs DB query and cache write
Requests 2β100
β detects that the same product-42 loader is already in-flight
β waits for the existing operation without issuing a separate DB query
β shares the same resultOther products must still be processed in parallel.
product-42 β 1 loader
product-77 β 1 loader
Loaders for both keys can run concurrentlyThe scope here is inside one process, and goes only as far as merging concurrent cache misses on the same cache key into a single loader run. Coordination across multiple servers comes later, in its own section.
2. Core concepts
The shared API looks like this.
SingleFlight.run(key, loader, callerCancellation)
key
= the unit of deduplication
loader
= the shared work that actually queries the DB or API and populates the cache
callerCancellation
= cancels only the individual caller's wait
serviceShutdown
= a parent lifetime signal that can cancel the shared loader itselfOne caller disconnecting must not cancel the shared loader that 99 other callers are waiting on, so caller cancellation and shared-task cancellation are kept separate.
When the return value is a reference type, every waiter receives the same object, so treat the shared result as immutable or copy it at the call boundary. Single-Flight does not fix one waiter's mutation leaking into another waiter's view.
C#
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
public sealed class SingleFlight<TKey, TValue>
where TKey : notnull
{
private readonly ConcurrentDictionary<
TKey,
Lazy<Task<TValue>>> inFlight = new();
private readonly CancellationToken shutdownToken;
public SingleFlight(
CancellationToken shutdownToken = default)
{
this.shutdownToken = shutdownToken;
}
public async Task<TValue> RunAsync(
TKey key,
Func<CancellationToken, Task<TValue>> loaderAsync,
CancellationToken callerToken = default)
{
ArgumentNullException.ThrowIfNull(loaderAsync);
Lazy<Task<TValue>>? candidate = null;
candidate = new Lazy<Task<TValue>>(
() => this.ExecuteAndReleaseAsync(
key,
candidate!,
loaderAsync),
LazyThreadSafetyMode.ExecutionAndPublication);
Lazy<Task<TValue>> selected =
this.inFlight.GetOrAdd(key, candidate);
return await selected.Value
.WaitAsync(callerToken)
.ConfigureAwait(false);
}
public int GetInFlightCount()
{
return this.inFlight.Count;
}
private async Task<TValue> ExecuteAndReleaseAsync(
TKey key,
Lazy<Task<TValue>> entry,
Func<CancellationToken, Task<TValue>> loaderAsync)
{
try
{
return await loaderAsync(this.shutdownToken)
.ConfigureAwait(false);
}
finally
{
this.RemoveIfCurrent(key, entry);
}
}
private void RemoveIfCurrent(
TKey key,
Lazy<Task<TValue>> entry)
{
// If you remove an entry by key alone, you may accidentally delete the new entry that replaced it.
// Delete the current entry only when both the key and value match.
this.inFlight.TryRemove(new KeyValuePair<
TKey,
Lazy<Task<TValue>>>(key, entry));
}
}The value factory of ConcurrentDictionary.GetOrAdd() can be invoked multiple times under contention. Only one of the pre-constructed values ends up in the dictionary; the factory itself is not guaranteed to run exactly once.5 For this reason, wrap the actual loader inside a Lazy<Task<TValue>>, and only start the Value of the Lazy that the dictionary selects.
Each caller cancels only its own wait through WaitAsync(callerToken); the shared loader gets nothing but the service-shutdown shutdownToken.
Use TryRemove(KeyValuePair<TKey,TValue>) for cleanup. This overload removes an entry only when both the key and value match the current entry, preventing a stale task from deleting an entry that was already registered for the same key by a newer task.6
A loader timeout is worth adding alongside the shutdown token. Every waiter waits on one task, so a loader that hangs forever pins the entry and the waiters together. A CancellationToken is not a hard-abort mechanism, though: the timeout only takes effect when the DB driver or HTTP client forwards the token into the actual I/O.
TypeScript
export type AsyncLoader<TValue> = (
shutdownSignal?: AbortSignal,
) => Promise<TValue>;
export class SingleFlight<TValue>
{
readonly #inFlight =
new Map<string, Promise<TValue>>();
readonly #shutdownSignal: AbortSignal | undefined;
public constructor(
shutdownSignal?: AbortSignal,
)
{
this.#shutdownSignal = shutdownSignal;
}
public async runAsync(
key: string,
loaderAsync: AsyncLoader<TValue>,
callerSignal?: AbortSignal,
): Promise<TValue>
{
validateKey(key);
callerSignal?.throwIfAborted();
const sharedPromise = this.#getOrCreate(
key,
loaderAsync,
);
return await waitForSharedAsync(
sharedPromise,
callerSignal,
);
}
public getInFlightCount(): number
{
return this.#inFlight.size;
}
#getOrCreate(
key: string,
loaderAsync: AsyncLoader<TValue>,
): Promise<TValue>
{
const existing = this.#inFlight.get(key);
if (existing !== undefined) {
return existing;
}
const created = Promise.resolve().then(() =>
{
return loaderAsync(this.#shutdownSignal);
});
this.#inFlight.set(key, created);
this.#registerCleanup(key, created);
return created;
}
#registerCleanup(
key: string,
promise: Promise<TValue>,
): void
{
const cleanup = (): void =>
{
if (this.#inFlight.get(key) === promise) {
this.#inFlight.delete(key);
}
};
void promise.then(cleanup, cleanup);
}
}
function waitForSharedAsync<TValue>(
sharedPromise: Promise<TValue>,
callerSignal?: AbortSignal,
): Promise<TValue>
{
if (callerSignal === undefined) {
return sharedPromise;
}
return new Promise<TValue>((resolve, reject) =>
{
let settled = false;
const cleanup = (): void =>
{
callerSignal.removeEventListener(
"abort",
onAbort,
);
};
const onAbort = (): void =>
{
if (settled) {
return;
}
settled = true;
cleanup();
reject(callerSignal.reason);
};
callerSignal.addEventListener(
"abort",
onAbort,
{ once: true },
);
// An abort can occur between the preliminary `throwIfAborted()` call and the listener registration.
// After attaching the listener, check the state again to close that gap.
if (callerSignal.aborted) {
onAbort();
return;
}
sharedPromise.then(
(value) =>
{
if (settled) {
return;
}
settled = true;
cleanup();
resolve(value);
},
(error: unknown) =>
{
if (settled) {
return;
}
settled = true;
cleanup();
reject(error);
},
);
});
}
function validateKey(key: string): void
{
if (key.trim().length === 0) {
throw new RangeError(
"A Single-Flight key cannot be empty.",
);
}
}In JavaScript there is no await between Map.get() and Map.set(), so entry registration completes synchronously within the same event-loop turn. Loader execution is deferred behind Promise.resolve().then(...) so the entry is in place first.
A caller's AbortSignal cancels only the wait inside waitForSharedAsync(). The shared Promise already in flight continues executing for the benefit of other waiters. This aligns with the MDN guideline that an AbortSignal-based Promise API should reject an unfinished operation with the signal's reason.7
The first throwIfAborted() alone is not enough: if cancellation arrives right after that check and just before the abort listener is attached, the event is missed. The example rechecks aborted after attaching the listener, which is usually the safe order.
Python
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable
from typing import Generic, TypeVar
TValue = TypeVar("TValue")
AsyncLoader = Callable[[], Awaitable[TValue]]
class SingleFlight(Generic[TValue]):
def __init__(self) -> None:
self._in_flight: dict[
str,
asyncio.Task[TValue],
] = {}
self._lock = asyncio.Lock()
async def run(
self,
key: str,
loader_async: AsyncLoader[TValue],
) -> TValue:
_validate_key(key)
task = await self._get_or_create(
key,
loader_async,
)
return await asyncio.shield(task)
def get_in_flight_count(self) -> int:
return len(self._in_flight)
async def _get_or_create(
self,
key: str,
loader_async: AsyncLoader[TValue],
) -> asyncio.Task[TValue]:
async with self._lock:
existing = self._in_flight.get(key)
if existing is not None:
return existing
task = asyncio.create_task(
self._execute_and_release(
key,
loader_async,
),
name=f"single-flight:{key}",
)
self._in_flight[key] = task
task.add_done_callback(
_observe_task_completion
)
return task
async def _execute_and_release(
self,
key: str,
loader_async: AsyncLoader[TValue],
) -> TValue:
try:
return await loader_async()
finally:
current_task = asyncio.current_task()
async with self._lock:
current = self._in_flight.get(key)
if current is current_task:
self._in_flight.pop(key, None)
def _observe_task_completion(
task: asyncio.Task[object],
) -> None:
if task.cancelled():
return
task.exception()
def _validate_key(key: str) -> None:
if key.strip() == "":
raise ValueError(
"The Single-Flight key cannot be empty."
)asyncio.shield() prevents the shared task from being canceled when an individual waiter is canceled. This is also why the Python documentation warns you to keep a strong reference to the task passed to shield(): the event loop may hold only a weak reference to a task, so in this example the _in_flight dictionary keeps the shared task alive.8
Cleanup happens in the loader's finally: entry removal timing is clearer than when a completion callback schedules a separate cleanup task. The loader can still fail after every waiter has left, so keep a done callback to observe the exception.
Rust
use std::collections::HashMap;
use std::future::Future;
use std::hash::Hash;
use std::sync::atomic::{
AtomicU64,
Ordering,
};
use std::sync::Arc;
use tokio::sync::{
Mutex,
Notify,
OnceCell,
};
#[derive(Clone, Debug)]
pub enum SingleFlightError<TError> {
Loader(TError),
LoaderTaskFailed(String),
}
struct Entry<TValue, TError> {
id: u64,
result: OnceCell<
Result<TValue, SingleFlightError<TError>>
>,
notify: Notify,
}
impl<TValue, TError> Entry<TValue, TError> {
fn new(id: u64) -> Self {
Self {
id,
result: OnceCell::new(),
notify: Notify::new(),
}
}
}
pub struct SingleFlight<TKey, TValue, TError> {
in_flight:
Mutex<HashMap<TKey, Arc<Entry<TValue, TError>>>>,
next_id: AtomicU64,
}
impl<TKey, TValue, TError>
SingleFlight<TKey, TValue, TError>
where
TKey:
Eq
+ Hash
+ Clone
+ Send
+ 'static,
TValue:
Clone
+ Send
+ Sync
+ 'static,
TError:
Clone
+ Send
+ Sync
+ 'static,
{
pub fn new() -> Arc<Self> {
Arc::new(Self {
in_flight: Mutex::new(HashMap::new()),
next_id: AtomicU64::new(1),
})
}
pub async fn run<TLoader, TFuture>(
self: &Arc<Self>,
key: TKey,
loader: TLoader,
) -> Result<TValue, SingleFlightError<TError>>
where
TLoader:
FnOnce() -> TFuture
+ Send
+ 'static,
TFuture:
Future<Output = Result<TValue, TError>>
+ Send
+ 'static,
{
let (entry, is_leader) =
self.get_or_create(&key).await;
if is_leader {
self.spawn_loader(
key,
Arc::clone(&entry),
loader,
);
}
wait_for_result(entry).await
}
pub async fn get_in_flight_count(&self) -> usize {
self.in_flight.lock().await.len()
}
async fn get_or_create(
&self,
key: &TKey,
) -> (Arc<Entry<TValue, TError>>, bool) {
let mut in_flight = self.in_flight.lock().await;
if let Some(entry) = in_flight.get(key) {
return (Arc::clone(entry), false);
}
let id = self.next_id.fetch_add(
1,
Ordering::Relaxed,
);
let entry = Arc::new(Entry::new(id));
in_flight.insert(
key.clone(),
Arc::clone(&entry),
);
(entry, true)
}
fn spawn_loader<TLoader, TFuture>(
self: &Arc<Self>,
key: TKey,
entry: Arc<Entry<TValue, TError>>,
loader: TLoader,
)
where
TLoader:
FnOnce() -> TFuture
+ Send
+ 'static,
TFuture:
Future<Output = Result<TValue, TError>>
+ Send
+ 'static,
{
let owner = Arc::clone(self);
tokio::spawn(async move
{
// loaderλ₯Ό ν λ¨κ³ μμͺ½ taskλ‘ μ€ννλ€.
// loader λ΄λΆ panic!μ΄λ task abortλ JoinErrorκ° λλ€.
// μ΄λ₯Ό terminal errorλ‘ κ³΅μ ν΄μΌ waiterκ° μꡬ λκΈ°νμ§ μλλ€.
let loader_task = tokio::spawn(async move {
loader().await
});
let result = match loader_task.await {
Ok(Ok(value)) => Ok(value),
Ok(Err(error)) => {
Err(SingleFlightError::Loader(error))
}
Err(join_error) => {
Err(SingleFlightError::LoaderTaskFailed(
join_error.to_string(),
))
}
};
let _ = entry.result.set(result);
entry.notify.notify_waiters();
owner.remove_if_current(
&key,
entry.id,
).await;
});
}
async fn remove_if_current(
&self,
key: &TKey,
entry_id: u64,
) {
let mut in_flight = self.in_flight.lock().await;
let should_remove = in_flight
.get(key)
.is_some_and(|entry| entry.id == entry_id);
if should_remove {
in_flight.remove(key);
}
}
}
async fn wait_for_result<TValue, TError>(
entry: Arc<Entry<TValue, TError>>,
) -> Result<TValue, SingleFlightError<TError>>
where
TValue: Clone,
TError: Clone,
{
loop {
if let Some(result) = entry.result.get() {
return result.clone();
}
let notified = entry.notify.notified();
if let Some(result) = entry.result.get() {
return result.clone();
}
notified.await;
}
}Dependencies you need:
[dependencies]
tokio = {
version = "1",
features = [
"macros",
"rt-multi-thread",
"sync"
]
}The Rust version runs the loader as its own Tokio task: dropping an individual waiter future leaves the loader running, and other waiters still get the same result.
If the loader panics, the original task can end before it fills the OnceCell or removes the entry, so waiters never wake and the entry stays in the map. The example runs the loader in an inner task and maps JoinError to LoaderTaskFailed, so success, business error, panic and abort all become terminal states that every waiter observes.
The ordering in wait_for_result(), where notified() is created before re-checking the result, is also intentional. Tokio's notify_waiters() guarantees that a notification will be delivered to a Notified future created before the notification occurs, even if that future has not yet been polled.9 If you replace this with notify_one() without relying on that guarantee, you must also consider additional enable() handling.
3. Call Site
Single-Flight sits at the cache-miss boundary of cache-aside.
type Product = Readonly<{
productId: string;
displayName: string;
priceCents: number;
}>;
type ProductCache = Readonly<{
getAsync: (
cacheKey: string,
signal?: AbortSignal,
) => Promise<Product | undefined>;
trySetAsync: (
cacheKey: string,
product: Product,
ttlSeconds: number,
signal?: AbortSignal,
) => Promise<boolean>;
}>;
type ProductRepository = Readonly<{
getByIdAsync: (
productId: string,
signal?: AbortSignal,
) => Promise<Product>;
}>;
export class ProductQueryService
{
readonly #cache: ProductCache;
readonly #repository: ProductRepository;
readonly #singleFlight: SingleFlight<Product>;
readonly #cacheTtlSeconds: number;
public constructor(
cache: ProductCache,
repository: ProductRepository,
singleFlight: SingleFlight<Product>,
cacheTtlSeconds: number,
)
{
validateCacheTtl(cacheTtlSeconds);
this.#cache = cache;
this.#repository = repository;
this.#singleFlight = singleFlight;
this.#cacheTtlSeconds = cacheTtlSeconds;
}
public async getByIdAsync(
productId: string,
callerSignal?: AbortSignal,
): Promise<Product>
{
validateProductId(productId);
const cacheKey = productCacheKey(productId);
const cached = await this.#cache.getAsync(
cacheKey,
callerSignal,
);
if (cached !== undefined) {
return cached;
}
return await this.#singleFlight.runAsync(
cacheKey,
(sharedSignal) =>
{
return this.#loadAndCacheAsync(
cacheKey,
productId,
sharedSignal,
);
},
callerSignal,
);
}
async #loadAndCacheAsync(
cacheKey: string,
productId: string,
sharedSignal?: AbortSignal,
): Promise<Product>
{
const cached = await this.#cache.getAsync(
cacheKey,
sharedSignal,
);
if (cached !== undefined) {
return cached;
}
const product = await this.#repository.getByIdAsync(
productId,
sharedSignal,
);
// If the origin lookup succeeded, a cache write failure should
// be handled on a best-effort basis so it does not propagate into a full read failure.
await this.#cache.trySetAsync(
cacheKey,
product,
this.#cacheTtlSeconds,
sharedSignal,
);
return product;
}
}
function productCacheKey(productId: string): string
{
return `product:detail:v1:${productId}`;
}
function validateProductId(productId: string): void
{
if (!/^[A-Za-z0-9_-]{1,64}$/.test(productId)) {
throw new RangeError(
"The `productId` format is invalid.",
);
}
}
function validateCacheTtl(cacheTtlSeconds: number): void
{
if (!Number.isSafeInteger(cacheTtlSeconds)
|| cacheTtlSeconds < 1
|| cacheTtlSeconds > 86_400) {
throw new RangeError(
"The cache TTL must be between 1 and 86,400 seconds (inclusive).",
);
}
}The loader checks the cache once more, because another process may have filled it between the initial miss and the loader run.
The Single-Flight registry and the cache use the same canonical key. If their key builders drift apart, the same item becomes two different operations in the registry, or two different results get coalesced into one.
Cache writes sit on a best-effort boundary. If one failed Redis write turned a successful DB lookup into a failure for every waiter, the cache would be lowering availability.
trySetAsync() records the failure in logs and metrics and returns false; errors from the origin store are raised as loader errors.
Typical separation of concerns:
ProductCache
= stores completed values for the TTL duration
ProductRepository
= the actual I/O that queries the DB or external API
SingleFlight
= shares an in-progress loader for the same key
ProductQueryService
= orchestrates the sequence of cache hit/miss and loader invocation
Caller Signal
= cancels the wait for an individual request
Shared Shutdown Signal
= cancels the shared loader on process shutdown
Cache TTL Policy
= determines data freshness and expiration timingSingle-Flight knows neither Product's business meaning nor its TTL, and the cache does not know how many waiters the current loader is shared by.
None of this is strictly required; it is what I ended up with after putting the examples together myself. It is the average shape, and grasping the rules is all it takes.
An individual caller's cancellation must not cancel the shared loader.
The rest is just how things shift with project scale.
4. Key Sequence
What is the merge key?
β Do requests for the same operation truly use the same key?
β Is the in-flight entry registered before the loader starts?
β Is only one loader executed per identical key?
β Can different keys execute in parallel?
β Is the entry removed after completion, failure, or cancellation?
β Is a loader failure propagated identically to all waiters?
β Does canceling an individual waiter avoid canceling the shared loader?
β Does the loader re-check the cache before proceeding?
β Is it clear whether this registry is process-local or distributed?The first thing to check is not whether a lock exists but the key design that determines what counts as the same operation.
5. Boundaries and Misconceptions
It is important to understand that Single-Flight is not a cache. The two are similar but fundamentally different in nature. A cache stores a value, while Single-Flight stores a task.
Cache:
Reuse completed results for the duration of the TTL
Single-Flight:
Share only identical operations that are currently in-flight
Remove the entry after completionSo even a request that arrives right after the loader finishes needs the cache to be holding the result. Run Single-Flight with no cache write and every sequential request re-runs the loader.
The coalescing key must carry every condition that affects the result.
Bad key:
productId
If the actual result varies depending on:
- tenantId
- locale
- currency
- permission scope
- API version
Recommended key:
tenantId + productId + locale + currency + policyVersionMerging requests with different permissions under one key sends one user's result to another: not a performance bug but data exposure.
The implementation works inside one process only.
Server instance A:
1 product-42 loader
Server instance B:
1 product-42 loader
Overall system:
Up to 2 loaders running concurrentlyIf only one loader may run across instances, you need a distributed lock, a lease, an atomic primitive from the cache provider, or a separate coordinator. A distributed lock only decides who runs; it does not deliver the result. You still have to settle whether the leader fills the cache and followers re-read it, whether results are announced over pub/sub, and whether a fencing token blocks a leader that finishes after its lease expired. One Redis lock on top of process-local Single-Flight does not reproduce the same properties.
Per-waiter cancellation policy matters too. If the first request starts the loader, disconnects, and takes the shared loader down with it, every remaining waiter fails. In this implementation, caller cancellation stops only the wait.
The opposite direction has a cost too.
All waiters are canceled
β the loader can continue executingIf the loader is very expensive, a waiter reference count that cancels when the last waiter leaves is worth considering, but it complicates the cancellation and re-entry race, so do not add it without measurements.
There is a point where failures get shared as well.
Same-key loader hits a DB timeout
β all waiters receive the same timeout error
β in-flight entry is removed
β the next request can execute a new loaderDo not leave a failed result sitting in the in-flight registry: the failure replays even after a transient fault has recovered. If the actual problem is repeated lookups of a missing product, that is a negative caching policy, not Single-Flight.
A small wave still forms right after a failure.
loader fails once
β 500 waiters receive the failure simultaneously
β all upper layers immediately retry
β 500 requests pile into the next single-flight loader againConsider retry caps, exponential backoff, jitter, and a short failure damping or negative cache together. Single-Flight cuts duplicate runs; it does not replace a retry policy.
Loaders need an upper time bound. Every waiter waits on the same completion object, so one loader that never finishes holds the registry entry and the waiters with it; the timeout belongs on the shared loader's I/O boundary, not just on individual waiters.
Key cardinality needs a bound as well. When external input becomes the key and an attacker sends a different value every time, each key becomes a new, unmerged operation, so add whatever you need: a max in_flight size, key validation, request rate limiting, a waiter cap, admission control.
Cases that fail in production:
A new loader is created on every cache miss
The loader starts before the in-flight entry is registered
Entries are not removed after completion, causing memory growth
Failed Promises or Tasks are retained indefinitely
Caller cancellation propagates to and cancels the shared loader
Tenant and permission information is excluded from the key
Using an object reference as the key prevents semantically identical requests from being merged
Process-local coalescing is mistaken for a distributed guarantee
The cache is not checked again inside the loader
Single-Flight is mistakenly assumed to also handle TTL and stale policies
Without a loader timeout, entries and waiters remain indefinitely
Attacker-controlled input causes in-flight key cardinality to grow without bound
Waiters mutate the shared mutable result, interfering with each other
After a loader failure, all waiters immediately retry at once
A distributed lock is assumed to also handle result sharing6. Bad Example
async function getProductBadAsync(
productId: string,
cache: ProductCache,
repository: ProductRepository,
signal?: AbortSignal,
): Promise<Product>
{
const cacheKey = productCacheKey(productId);
const cached = await cache.getAsync(
cacheKey,
signal,
);
if (cached !== undefined) {
return cached;
}
const product = await repository.getByIdAsync(
productId,
signal,
);
await cache.trySetAsync(
cacheKey,
product,
300,
signal,
);
return product;
}For a single request this is correct cache-aside code; with concurrent requests a race opens up.
Request A cache miss
Request B cache miss
Request C cache miss
A DB query
B DB query
C DB queryWhy it's bad:
There is no concurrency coordination between a cache miss and the DB query.
When a popular key expires, backend requests spike momentarily.
The same operation that populates the cache runs multiple times.
Every request consumes a DB connection and network slot individually.
The problem is difficult to detect in normal testing.One global mutex around the whole function is not a good fix either.
global lock:
Looking up product-42
β product-77 lookup also waitingDo not forget that Single-Flight must serialize only the same key, not the whole service.
7. Production Scaling
Pin the core contract with concurrency tests.
import assert from "node:assert/strict";
import test from "node:test";
test("Concurrent requests for the same key execute the loader only once", async () =>
{
const singleFlight = new SingleFlight<string>();
let loaderCallCount = 0;
const loaderAsync = async (): Promise<string> =>
{
loaderCallCount += 1;
await delayAsync(20);
return "product-42";
};
const results = await Promise.all(
Array.from(
{ length: 50 },
() => singleFlight.runAsync(
"product-42",
loaderAsync,
),
),
);
assert.equal(loaderCallCount, 1);
assert.deepEqual(
new Set(results),
new Set(["product-42"]),
);
assert.equal(
singleFlight.getInFlightCount(),
0,
);
});
test("Requests for different keys each execute their own loader", async () =>
{
const singleFlight = new SingleFlight<string>();
let loaderCallCount = 0;
const loadAsync = async (
value: string,
): Promise<string> =>
{
loaderCallCount += 1;
await delayAsync(10);
return value;
};
const [first, second] = await Promise.all([
singleFlight.runAsync(
"product-42",
() => loadAsync("first"),
),
singleFlight.runAsync(
"product-77",
() => loadAsync("second"),
),
]);
assert.equal(loaderCallCount, 2);
assert.equal(first, "first");
assert.equal(second, "second");
});
test("Failed entries are removed so that the next request can retry", async () =>
{
const singleFlight = new SingleFlight<string>();
let attemptCount = 0;
await assert.rejects(() =>
{
return singleFlight.runAsync(
"product-42",
async () =>
{
attemptCount += 1;
throw new Error("temporary failure");
},
);
});
const result = await singleFlight.runAsync(
"product-42",
async () =>
{
attemptCount += 1;
return "recovered";
},
);
assert.equal(attemptCount, 2);
assert.equal(result, "recovered");
});
test("Cancellation by one waiter does not cancel the shared loader", async () =>
{
const singleFlight = new SingleFlight<string>();
const controller = new AbortController();
let loaderCallCount = 0;
const loaderAsync = async (): Promise<string> =>
{
loaderCallCount += 1;
await delayAsync(30);
return "shared-result";
};
const cancelledWaiter = singleFlight.runAsync(
"product-42",
loaderAsync,
controller.signal,
);
const survivingWaiter = singleFlight.runAsync(
"product-42",
loaderAsync,
);
controller.abort(
new Error("caller disconnected"),
);
await assert.rejects(
() => cancelledWaiter,
);
assert.equal(
await survivingWaiter,
"shared-result",
);
assert.equal(loaderCallCount, 1);
});
function delayAsync(
milliseconds: number,
): Promise<void>
{
return new Promise((resolve) =>
{
setTimeout(resolve, milliseconds);
});
}Operational metrics:
single_flight.request.count
single_flight.loader.started.count
single_flight.waiter.shared.count
single_flight.loader.succeeded.count
single_flight.loader.failed.count
single_flight.in_flight
single_flight.waiter.duration_ms
single_flight.loader.duration_msPin a derived value together with its formula: with only the name coalescing ratio, some teams mean the reciprocal.
requests per loader
Total Single-Flight requests / Actual loader executions
e.g.,
1,000 requests
50 loader executions
requests per loader = 20
loader suppression ratio
= 1 - (Actual loader executions / Total Single-Flight requests)
loader suppression ratio = 95%Do not include raw product IDs or full cache keys as metric attributes. Use only fields with limited cardinality, such as operationName, result, and cacheRegion. OpenTelemetry also warns that attributes with ever-growing unique combinations, such as user IDs or raw URLs, can drive up memory costs and cause cardinality overflow.10
Items to check together in production:
- cache hit ratio
Number of concurrent waiters per key
- loader p95/p99 latency
DB connection pool usage
Loader failure rate
Requests per loader per instance
Number of in-flight keys and number of rejected key creations
Current number of waiters per key and number of waiter limit reached eventsIf loader calls drop after adding Single-Flight but waiter latency grows too much, you may have a hot-key problem where too many requests are tied to one slow loader.
8. Comparison Notes: C# / TypeScript / Python / Rust
Language | Idiomatic Expression | Key Caveats |
|---|---|---|
C# |
| Must distinguish between duplicate factory invocations and the observation cost of |
TypeScript |
| The Promise must be registered in the map before the loader starts |
Python |
| Requires the assumption that everything runs within a single event loop |
Rust |
| waiter drop, loader panic, and entry cleanup must all be handled together |
- Language
C#
- Idiomatic Expression
ConcurrentDictionary<TKey, Lazy<Task<T>>>- Key Caveats
Must distinguish between duplicate factory invocations and the observation cost of
Count
- Language
TypeScript
- Idiomatic Expression
Map<string, Promise<T>>- Key Caveats
The Promise must be registered in the map before the loader starts
- Language
Python
- Idiomatic Expression
asyncio.Lock, sharedTask,asyncio.shield- Key Caveats
Requires the assumption that everything runs within a single event loop
- Language
Rust
- Idiomatic Expression
Arc<Entry>,Mutex<HashMap>,Notify, background task- Key Caveats
waiter drop, loader panic, and entry cleanup must all be handled together
In C#, assume the value factory of ConcurrentDictionary.GetOrAdd() can run more than once under contention, so put the real work inside Lazy<Task<T>> and let only the winning entry execute it.
The GetInFlightCount() method in the example is intended for debugging and low-frequency observation. Because ConcurrentDictionary.Count uses an internal lock, calling it on every request can cause contention.11
If you need high-frequency metrics, keep a separate Interlocked-based counter or an approximation that does not disturb lifetime management; increment when an entry is registered successfully and decrement when the current entry is removed successfully.
In TypeScript a Promise is itself a shareable completion representation, but calling the loader first and inserting it into the map afterwards can throw in the synchronous segment or admit a duplicate, so entry registration and loader start have to keep a fixed order.
In Python, task cancellation of one waiter propagates into the shared task it is awaiting, so use asyncio.shield() to detach the shared work's lifetime from the individual waiter's.
The get_in_flight_count() method in the Python example reads len() directly under the assumption that the object is confined to a single event loop and thread. Within the same loop, since there is no await inside this method, no other coroutine can modify the map in between. Usage that accesses _in_flight or asyncio.Lock from another thread is not supported. This safety should not be explained as relying on CPython's GIL, because Python supports a free-threaded build, and asyncio synchronization primitives are not tools for synchronization between OS threads.12
In Rust, dropping a waiter's future drops the work inside that future too. Spawning the loader as a separate Tokio task and sharing the result entry via Arc detaches individual waiter lifetime from the shared work. Spawning alone is not enough, though: without turning a loader panic into a terminal result, waiters stay behind forever.
Failure representation differs by language. In C#, TypeScript and Python a loader exception naturally becomes the failed state of the shared Task, Promise or asyncio.Task. A panic in a spawned Rust task is not the business error TError, so the example promotes JoinError to SingleFlightError::LoaderTaskFailed. That distinction lets the caller record an error returned by the loader separately from an abnormal termination of the task itself.
The details differ, but the common ground comes down to this.
Represent any in-flight work for the same key as a single completion object,
and have all waiters wait on that same completion object.9. Further Considerations
If the result varies by tenant, locale or permission, what must go into the Single-Flight key?
Should one waiter's cancellation stop the shared loader, or is cutting that waiter's wait enough?
Is it worth keeping the loader running to fill the cache after every waiter has left?
Is process-local coalescing enough, or do you need distributed coordination across instances?
Should loader failures be retried right away, or is a short negative cache or backoff needed?
When thousands of waiters hang on one slow loader for a hot key, how do you bound memory and tail latency?
Single-Flight is not always needed: with a cheap loader and low call volume, the registry and cancellation policy cost more than they save. Forcing independent requests onto one key only adds latency, and merging writes loses per-call side effects or audit records. For payments, message publishing or sequence-number issuance, start from whether requests that look identical must still run separately.
10. Summary
Single-Flight merges concurrent loader runs for the same key into one.
The cache holds completed values; Single-Flight shares only work in progress.
Register the entry before the loader starts and always remove it when the work ends, success or failure.
Keep individual caller cancellation and shared loader cancellation on separate lifetimes.
Keys carry the tenant, permission, locale and policy version that affect the result.
Put caps on loader timeout, key count, waiter count and retry bursts.
Set the boundary so a cache write failure cannot turn a successful origin lookup into a failure.
In-process Single-Flight does not stop duplicate runs across server instances.
Rule of thumb:
Cache
The value lands after the work completes
TTL, eviction and invalidation stay in place
Later requests reuse the value
Single-Flight
Exists only while the work is running
The entry is removed on completion, failure or cancellation
Later requests start a new run
If the same cache key is missing concurrently,
don't let each request fall through to the backend independently; let one fetch the data and have the rest wait for that result.Personal note: The core logic I picked up this time is a pattern I discovered while studying code from Chinese sources.
Cache<K, V>maps K β a completed V
SingleFlight<K, Task<V>>maps K β the currently in-flightTask<V>. There is actually a somewhat tricky point here.The fundamental instinct in programming is really the same as in management: deciding what the minimum unit of control and identity should be. This is the management approach when you treat the Task as that minimum unit.
The long sections above are essentially a manual summarized point by point, but the simple idea is this: instead of treating multiple calls as separate requests, you group them into a single in-flight operation that produces one shared result.
Most programming problems reduce to the following questions.
What do you treat as a single unit?
What do you manage independently?
How far do you define an atomic (minimum-unit) state transition?
What do you assign identity and lifetime to?
It can be simple, or it can be complex.
Footnotes
- Microsoft Azure Architecture Center. Cache-Aside pattern β©
- Pang, R. et al. Zanzibar: Google's Consistent, Global Authorization System (PDF). 2019 USENIX Annual Technical Conference. The cache stampede section of the paper describes per-server lock tables and the coalescing of requests for the same cache key. β©
- Go Project. `golang.org/x/sync/singleflight`: duplicate function call suppression β©
- Go Project. `x/sync/singleflight` source. You can see
Do's direct execution andWaitGroupwaiting, as well asDoChan's goroutine execution. β© - Microsoft. `ConcurrentDictionary<TKey,TValue>.GetOrAdd` Remarks. Explains that
valueFactoryruns outside the lock and may be called multiple times during contention. β© - Microsoft. `ConcurrentDictionary<TKey,TValue>.TryRemove(KeyValuePair<TKey,TValue>)`. Removes only the entry where both the key and value match. β©
- MDN Web Docs. `AbortSignal`. Includes examples of abort handling for Promise APIs and passing
signal.reason. β© - Python Software Foundation. `asyncio.shield`. Explains the separation between caller cancellation and shared task cancellation, along with a note on keeping strong references to tasks. β©
- Tokio. `tokio::sync::Notify`. Explains the wake-up guarantees between
notify_waiters()and theNotifiedfutures that were created beforehand. β© - OpenTelemetry. Metrics cardinality limits. Explains why unique attribute combinations increase metric state and memory usage. β©
- Microsoft. Slow `ConcurrentDictionary.Count` lookup. Explains the locking cost of
Countand alternatives when it is called at high frequency. β© - Python Software Foundation. asyncio and free-threaded Python. Explains that event loops and tasks must not be shared across threads, and that primitives such as
asyncio.Lockare not intended for thread synchronization. β©