Applicability
When to Use
✓Work can be divided into independent subtasks
✓Processing time needs to be minimized through parallelism
✓Results from multiple sources need to be aggregated
✓Rate limits or quotas allow concurrent operations
Overview
How It Works
The Fan-Out/Fan-In pattern splits work into multiple independent subtasks (fan-out), processes them concurrently, and then combines the results (fan-in). A coordinator manages the distribution of work and the aggregation of results. This pattern is excellent for multi-source data collection, parallel API calls, and distributed analysis tasks.
Implementation
Code Example
javascript
class FanOutFanIn {
constructor(options = {}) {
this.concurrency = options.concurrency || 5;
}
async execute(items, workerFn, aggregateFn) {
// Fan-out: process items in parallel batches
const results = [];
for (let i = 0; i < items.length; i += this.concurrency) {
const batch = items.slice(i, i + this.concurrency);
const batchResults = await Promise.all(
batch.map(item => workerFn(item))
);
results.push(...batchResults);
}
// Fan-in: aggregate all results
return aggregateFn(results);
}
}
// Usage
const fanout = new FanOutFanIn({ concurrency: 3 });
const report = await fanout.execute(
repositories,
async (repo) => analyzeRepo(repo),
(analyses) => generateReport(analyses)
);Quick Info
CategoryParallel Processing
ComplexityMedium