Senior LevelSystem Design & Architecture
How does Single-Flight (Request Coalescing) eliminate duplicate concurrent backend requests?
β Interview Prompt: Explain Single-Flight pattern (Go `singleflight` package), collapsing identical concurrent requests into a single execution, and preventing backend stampedes.
When 500 concurrent requests arrive simultaneously for the exact same resource (e.g. 'GET /api/v1/prices/BTC'), executing 500 DB queries is redundant:
Single-Flight Solution:
The Single-Flight pattern intercepts duplicate in-flight requests, executing the underlying query ONLY ONCE, and sharing the single returned response with all 500 callers simultaneously!
Code Implementation Example
typescript
import { SingleFlight } from './single-flight';
const sf = new SingleFlight();
export async function getBtcPrice(currency: string) {
// Collapses 100 concurrent requests into 1 execution!
return sf.do('btc-price:\${currency}', async () => {
return await fetchPriceFromExternalApi(currency);
});
}π‘Collapsing duplicate concurrent requests using Single-Flight.
π― Interview Key Takeaway
Use Single-Flight request coalescing to suppress duplicate concurrent requests for identical resources.
#System Design#Single-Flight#Request Coalescing#Performance#Senior
Back to all System Design & Architecture Questionsβ