QA
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→

More System Design & Architecture Interview Questions

Beginner LevelSystem Design & Architecture

Explain the CAP Theorem and PACELC Theorem with real-world database examples.

Beginner LevelSystem Design & Architecture

What is Vertical Scaling (Scale Up) vs Horizontal Scaling (Scale Out)?

Beginner LevelSystem Design & Architecture

How do Load Balancers distribute traffic (Round Robin, Least Connections, IP Hash)?