Course contentsShow
← Node.js
Lesson 1250 of 1,98011. Task Queues and Background ProcessingFree lesson

Implementing Scatter-Gather Patterns

Dispatch a single task to multiple workers and aggregate their results into a final consolidated state.

In this lesson, you will learn how to parallelize work across multiple message workers and synthesize their individual outputs into a single consolidated result.

The Scatter-Gather Pattern

The Scatter-Gather pattern is a messaging strategy used when a complex request needs to be broken down into smaller, concurrent tasks. Instead of waiting for one worker to process everything sequentially, the "scatter" phase dispatches sub-tasks to multiple independent workers. The "gather" phase then waits for these workers to respond, collecting their results to form a final answer.

How it Works in Node.js

Imagine you are building a search aggregator. When a user searches for a product, your service needs to query three different inventory databases.

  1. Scatter: Your producer publishes a request message to a "Fan-out" exchange. Each inventory worker service (the consumers) receives the task simultaneously.
  2. Gather: Your producer needs a way to track these replies. Since message queues are asynchronous, you should assign a unique correlationId to your initial request. Each worker includes this ID in their reply.
  3. Aggregation: The producer uses a temporary response queue or a shared state store (like Redis) to collect these replies until all expected results arrive or a timeout occurs.
// A conceptual look at the Scatter phase
const correlationId = generateUUID();
const workers = ['inventory-us', 'inventory-eu', 'inventory-asia'];

workers.forEach(worker => {
  channel.sendToQueue(worker, Buffer.from(JSON.stringify(payload)), {
    correlationId,
    replyTo: 'response-queue'
  });
});

The "gatherer" component listens to the response-queue. It maintains a local object indexed by the correlationId to track which workers have reported back. Once all responses are present (or the timeout triggers), the aggregator compiles the data and fulfills the original client request.

Crucial Considerations

  • Timeouts: Because you are dependent on multiple systems, you must implement a strict timeout for the gather phase. If one worker hangs, your entire operation shouldn't fail indefinitely.
  • Partial Success: Decide how your application behaves if one worker returns an error. Can you return a partial result, or should you treat the whole request as a failure?

Key Takeaway: Scatter-Gather allows you to trade increased concurrency for higher system complexity by orchestrating multiple background workers to produce a unified result.