Scatter-Gather: working with results and errors
Understand the output structure and composite errors before transforming results from multiple routes.
Scatter-Gather sends the same message down two or more routes concurrently and waits for all of them. Use it when you need several independent things done to one message — enrich from a CRM, check stock in the ERP, fetch pricing — and none of them depends on the others.
The output is a map, not a list
This is where most of the time goes on a first attempt. The aggregated payload is an object keyed by route index as a string, and each entry is a full Mule message with its own payload and attributes.
{
"0": { payload: {...}, attributes: {...} },
"1": { payload: {...}, attributes: {...} },
"2": { payload: {...}, attributes: {...} }
}
// so the transform reaches in by index
%dw 2.0
output application/json
---
{
customer: payload["0"].payload,
stock: payload["1"].payload,
pricing: payload["2"].payload
}
Because the keys are positional, reordering routes in the canvas silently rewires your transform. Name the routes in the flow and keep a comment mapping index to meaning, or you will renumber something at 5pm on a Friday.
Variables do not escape
Anything you set inside a route stays inside that route. If a route needs to hand something back, it has to come out in that route's payload — the aggregated map is the only channel out.
One failure, one composite error
If any route fails, Scatter-Gather waits for the others and then raises
MULE:COMPOSITE_ROUTING. The error carries the results of the routes that
succeeded alongside the failures, which means you can still salvage a partial
answer.
Decide deliberately which routes are mandatory. A pricing lookup failing might be fatal; a "recommended products" lookup failing almost certainly is not. If a route is optional, wrap it in a Try with On Error Continue and return a null or a default from inside it — then the scope never fails for a reason you did not care about.
Set the timeout
Scatter-Gather waits for the slowest route. Without an explicit timeout, one hung dependency holds a thread and the caller for as long as the connector's own timeout allows. Set it to something shorter than your caller's patience.
Scatter-Gather or Parallel For Each?
- Scatter-Gather — different logic, same message. Three routes, three different things done to one payload.
- Parallel For Each — same logic, different items. One set of processors run against every element of a collection.
If the number of routes is fixed at design time, it is Scatter-Gather. If it depends on how many items arrived, it is Parallel For Each.
Building something where this matters?
Discuss a focused integration pilot covering the agreed workflow, error handling, testing and handover. Talk to our team about available new-client launch benefits.
