Scatter-gather sends one request to several services in parallel and combines the answers. How to set timeouts, handle partial results and failures, and choose between direct calls and messaging, with a TypeScript price comparison example on Encore.

Some requests need answers from several places at once: a quote comparison asks five suppliers for a price, a search fans out to index shards, and a dashboard loads data from four services, so calling them one after another makes the total latency the sum of all the calls.
Scatter-gather sends the calls in parallel and combines the results. This guide covers the deadline and failure policy, then builds a TypeScript example.
The coordinator broadcasts a request to a set of responders and then aggregates the replies, a pattern described in Enterprise Integration Patterns. With direct calls that is a set of parallel requests and a join. With messaging it is a message published to many consumers and a reply channel the coordinator reads, with a correlation ID tying replies to the request.
Latency becomes the slowest responder instead of the sum, which is a large win only if you stop waiting for stragglers.
Pick a deadline for the whole operation from what the caller will tolerate, then give every call a timeout shorter than that. Without per-call timeouts a single hung responder holds the whole response until the deadline. A reasonable user-facing default is a total of one to two seconds, with a per-call timeout slightly under it. Cancel calls that lose the race, so slow responders do not keep working on answers nobody will read.
Decide the policy before writing code, because it shapes the response type. Three common policies are all-or-nothing, return whatever arrived with the missing sources marked, and return once a quorum has answered. For comparison and search, partial results with an explicit list of failed sources are usually right, since a slower supplier should not take the page down. In JavaScript, Promise.allSettled waits for every promise and reports each outcome without rejecting early, which fits this policy, while Promise.all fails on the first rejection. In Go, errgroup gives the same shape with a shared context for cancellation.
Rank and deduplicate the merged result in the coordinator, and keep each responder's raw answer out of the public response so a provider change does not change your API.
Parallel direct calls are simplest while responders answer within a couple of seconds. When some take longer, publish the request to a topic, let each responder publish a reply carrying the request ID, and store replies as they arrive. The caller then polls or is notified when enough replies are in, which is the asynchronous request-reply pattern combined with fan-out.
Encore is a backend framework with built-in infrastructure, and each responder is an Encore service that the coordinator calls through a generated, type-safe service call instead of a hand-written HTTP client. The same call works under encore run and between services deployed to your AWS or GCP account.
Run in your terminal to get started locally.
// acme/acme.ts
import { api } from "encore.dev/api";
export const quote = api(
{ method: "POST", path: "/acme/quote" },
async ({ sku }: { sku: string }): Promise<{ supplier: string; priceCents: number }> => {
return { supplier: "acme", priceCents: 1999 };
},
);
// compare/compare.ts
import { api } from "encore.dev/api";
import { acme, globex } from "~encore/clients";
interface Offer {
supplier: string;
priceCents: number;
}
interface Comparison {
offers: Offer[];
failed: string[];
}
function withTimeout<T>(p: Promise<T>, ms: number): Promise<T> {
return Promise.race([
p,
new Promise<T>((_, reject) => setTimeout(() => reject(new Error("timeout")), ms)),
]);
}
export const compare = api(
{ method: "GET", path: "/compare/:sku", expose: true },
async ({ sku }: { sku: string }): Promise<Comparison> => {
const sources = {
"acme": () => acme.quote({ sku }),
"globex": () => globex.quote({ sku }),
};
const names = Object.keys(sources) as (keyof typeof sources)[];
const results = await Promise.allSettled(names.map((n) => withTimeout<Offer>(sources[n](), 1500)));
const offers: Offer[] = [];
const failed: string[] = [];
results.forEach((r, i) => {
if (r.status === "fulfilled") offers.push(r.value);
else failed.push(names[i]);
});
offers.sort((a, b) => a.priceCents - b.priceCents);
return { offers, failed };
},
);
The response lists offers sorted by price and names the suppliers that timed out, so the page can render with what arrived. Each call to a supplier shows up as a span in the request trace, which makes the slowest responder obvious. The globex service looks like acme with a different price, and adding a supplier is a new service and one more entry in sources.
A coding agent can add a new supplier service and wire it into the coordinator in the same change, then run the comparison locally with every service started by encore run.
Best fit: Teams that aggregate answers from several internal services in TypeScript and want typed calls and per-call traces without maintaining HTTP clients.
A coordinator sends the same request to several recipients in parallel, the scatter step, and then collects their replies and combines them into one response, the gather step. Price comparison, search across shards and multi-supplier quoting all use it.
Give the whole operation a deadline and each call its own timeout, then decide up front whether to return partial results or fail. Most user-facing endpoints return what arrived in time and mark the missing sources, and only fail when too few answers came back to be useful.
Direct parallel calls fit when the caller waits for the answer within a second or two. Messaging with a reply topic fits slower work, where responders take seconds or minutes, because the coordinator can collect replies as they arrive and the caller polls or receives a callback.
Declare a service per responder and call each one with Encore's generated, type-safe service call functions inside Promise.allSettled with a timeout. Encore traces each call, so the slowest responder is visible in the request trace in every environment.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.