Fan-Out / Fan-In

Parallel ProcessingMedium
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

Related Patterns

Need Architecture Help?

Our team designs custom automation architectures.

Get in Touch
CortexAgent Customer Service

Want to skip the form?

Our team is available to help you get started with CortexAgent.

This chat may be recorded for quality assurance. You can view our Privacy Policy.