---
schema_version: "1"
id: 78194cddceacd45f439c527e49a75b648cf07431e49363113c87eb4ecd31dd3d
title: Scatter-Gather Router
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-runtime/latest/scatter-gather-concept.md
canonical_url: https://docs.mulesoft.com/mule-runtime/latest/scatter-gather-concept
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 36b3bc6e56ace011683f524edfc9fa4a5a9c67a1b78a59da1cb5ebb44489e186
description: MuleSoft Documentation Site
release: "4.13"
latest: true
---
---
product: Mule Runtime
version: 4.13
is-latest-version: true
---

# Scatter-Gather Router

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Scatter-Gather component is a routing event processor that processes a Mule event through
parallel processing routes that contain different event processors. Each route receives a reference
to the Mule event and executes a sequence of one or more event processors. Each route uses a
separate thread to execute its event processors. The resulting Mule event is either the same Mule
event without modifications or a new Mule event with its own payload, attributes, and variables.
After every route completes successfully, the Scatter-Gather component combines the Mule events that
each route returns into a new Mule event and passes the new event to the next event processor.

The Scatter-Gather component executes each route in parallel, not sequentially. Parallel execution
can increase the efficiency of your Mule application and can provide more information than
sequential processing.

The Scatter-Gather component works with repeatable streams. It doesn’t process nonrepeatable
streams, which can be read only once before they’re lost. By default, all streams in Mule are
repeatable unless you configure a component’s streaming strategy as nonrepeatable.

This diagram shows the behavior of the Scatter-Gather component:

![A Scatter-Gather component processing multiple events in a Mule application](/mulesoft/current/en-us/mule-runtime/latest/_images/mruntime-scatter-gather.png)

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>The Scatter-Gather component receives a Mule event and sends a reference to this Mule event to each processing route.</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>All processing routes start executing in parallel. After all processors inside a route finish processing, the route returns a Mule event. This Mule event is either the same Mule event without modifications or a new Mule event that the processors in the route create.</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>After all processing routes finish, the Scatter-Gather component creates a new Mule event that combines the resulting Mule events from each route. Then the Scatter-Gather component passes the new Mule event to the next component in the flow.</td></tr></tbody></table>

> [!WARNING] Configure your Scatter-Gather component with at least two routes. Otherwise, your Mule
> application throws an exception and doesn’t start.

## Result

After all routes complete, the component outputs the results in this format:
`{0: messageFromRoute0, 1: messageFromRoute1, …​}`

To extract all resulting payloads to an array, use a DataWeave script, such as this one:

```dw
flatten(valuesOf(payload) map ((item, index) -> item.*payload))
```

## Variable Propagation

Every route starts with the same initial variable values. Changes to a variable in one route don’t
affect other routes. If one route adds or modifies a variable, the value after aggregation is the
value that route defines. If multiple routes add or modify the same variable, the value after
aggregation is a list of all the values that the routes define for that variable. For example:

```xml
<set-variable variableName="var1" value="var1"/>
<set-variable variableName="var2" value="var2"/>
<scatter-gather doc:name="Scatter-Gather" doc:id="abc665e0-6119-4ecb-9f8b-52dbcbb1d488" >
	<route >
		<set-variable variableName="var2" value="newValue"/>
        <set-variable variableName="var3" value="appleVal"/>
	</route>
	<route >
		<set-variable variableName="var3" value="bananaVal"/>
	</route>
	<route >
		<set-variable variableName="var3" value="otherVal"/>
        <set-variable variableName="var4" value="val4"/>
	</route>
</scatter-gather>
```

After aggregation, the variables are:

`{var1: "var1", var2: "newValue", var3: ["appleVal, bananaVal, otherVal"], var4: "val4"}`

## Error Handling Inside Scatter-Gather Routes

To handle errors that a route’s event processors generate, use a Try scope in each route of the
Scatter-Gather component. After all routes execute, if any route fails with an error, the
Scatter-Gather component throws an error of type `MULE:COMPOSITE_ROUTING`. Event processing doesn’t
continue past the Scatter-Gather component in the flow. Instead, the flow branches to your
error-handling event processors.

The `MULE:COMPOSITE_ROUTING` error object gathers the errors from the routes that failed and the
Mule events from the routes that completed successfully. Your error-handling event processors can
then process the Mule events from the completed routes. For the full list of fields available in the
error, see [MULE:COMPOSITE\_ROUTING Error Fields](#composite_routing_fields).

These two cases show how error handling works in a Scatter-Gather component:

- Each route in a Scatter-Gather component contains a Try scope.  
  One route generates an error. The `on-error-continue` error handler in that route’s Try scope
  handles the error, so the route completes successfully. The Scatter-Gather component consolidates
  the Mule events from all routes into a new Mule event and passes the consolidated event to the
  next event processor.

- One route in a Scatter-Gather component has no Try scope, has a Try scope with an error handler
  that can’t handle the error type, or has an `on-error-propagate` error handler.  
  An error occurs in this route and causes the route to fail. As a result, the Scatter-Gather
  component throws a `MULE:COMPOSITE_ROUTING` error. The flow branches to your error-handling event
  processors, which can process the Mule events from the completed routes.

This example shows how to handle these errors:

```xml
<flow name="errorHandler">
    <scatter-gather>
        <route>
            <raise-error type="APP:MYERROR" description="Details on what caused this application error"/>
        </route>
        <route>
            <set-payload value="apple"/>
        </route>
    </scatter-gather>
    <error-handler>
        <on-error-continue type="MULE:COMPOSITE_ROUTING">
            <!-- Returns the error thrown by the first route -->
            <logger level="WARN" message="#[error.errorMessage.payload.failures['0']]"/>
            <!-- Returns APP:MYERROR, the type set in the raise-error component -->
            <logger level="WARN" message="#[error.errorMessage.payload.failures['0'].errorType]"/>
            <!-- Returns "Details on what caused this application error", the description set in the raise-error component -->
            <logger level="WARN" message="#[error.errorMessage.payload.failures['0'].description]"/>
            <!-- Returns null -->
            <logger level="WARN" message="#[error.errorMessage.payload.failures['1']]"/>

            <!-- Returns null -->
            <logger level="WARN" message="#[error.errorMessage.payload.results['0']]"/>
            <!-- Returns the result of the second route, which completed successfully -->
            <logger level="WARN" message="#[error.errorMessage.payload.results['1']]"/>
            <!-- Returns "apple", the payload set in the second route -->
            <logger level="WARN" message="#[error.errorMessage.payload.results['1'].payload]"/>
        </on-error-continue>
    </error-handler>
</flow>
```

### MULE:COMPOSITE\_ROUTING Error Fields

When the Scatter-Gather component throws a `MULE:COMPOSITE_ROUTING` error, the error message payload
(`error.errorMessage.payload`) contains the outcome of every route. Both `failures` and `results`
use the route index as the key. The route index is a string that matches the order of the routes in
the XML configuration, starting at `'0'`. Each route appears in only one of the two collections, so
looking up its index in the other collection returns `null`.

<table><colgroup><col> <col></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><p><code>error.errorMessage.payload.failures</code></p></td><td><p>Errors from the routes that failed, keyed by route index. For example, <code>failures['0']</code> returns the error thrown by the first route. If the route at that index completed successfully, the value is <code>null</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.failures['&lt;index&gt;'].errorType</code></p></td><td><p>Type of the error that the route threw. The component that raised the error defines this type. In the example, this value is <code>APP:MYERROR</code>, which matches the <code>type</code> attribute of the <code>raise-error</code> component. To get each part of the type separately, use <code>errorType.namespace</code> and <code>errorType.identifier</code>, for example, <code>APP</code> and <code>MYERROR</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.failures['&lt;index&gt;'].description</code></p></td><td><p>Description of the error. The component that raised the error defines this description. In the example, this value is <code>Details on what caused this application error</code>, which matches the <code>description</code> attribute of the <code>raise-error</code> component.</p></td></tr><tr><td><p><code>error.errorMessage.payload.failures['&lt;index&gt;'].detailedDescription</code></p></td><td><p>Description of the error that’s the same as or more extensive than <code>description</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.failures['&lt;index&gt;'].errorMessage</code></p></td><td><p>Optional Mule message that the failing component associates with the error, such as the response of a failed HTTP request. If the component doesn’t provide a message, as with the <code>raise-error</code> component, the value is <code>null</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.failures['&lt;index&gt;'].cause</code></p></td><td><p>Underlying Java <code>Throwable</code> that caused the route to fail.</p></td></tr><tr><td><p><code>error.errorMessage.payload.results</code></p></td><td><p>Mule messages from the routes that completed successfully, keyed by route index. For example, <code>results['1']</code> returns the message from the second route. If the route at that index failed, the value is <code>null</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.results['&lt;index&gt;'].payload</code></p></td><td><p>Payload of the message that a successful route returns. In the example, <code>results['1'].payload</code> is <code>apple</code>.</p></td></tr><tr><td><p><code>error.errorMessage.payload.results['&lt;index&gt;'].attributes</code></p></td><td><p>Attributes of the message that a successful route returns.</p></td></tr><tr><td><p><code>error.childErrors</code></p></td><td><p>Collection of the errors that the failed routes threw.</p></td></tr></tbody></table>

Each failure provides the same fields as any other Mule error. For more information, see
[Selector Expressions for Mule Errors](https://docs.mulesoft.com/mule-runtime/latest/mule-error-concept#selector_expressions).

## Handle Timeout Errors in a Scatter-Gather Component

If you configure a timeout for a Scatter-Gather component and a route doesn’t complete processing
before the timeout expires, the route throws a `MULE:TIMEOUT` error. The Scatter-Gather component
handles this error the same way as any other error from a route. After each route completes, either
successfully or by throwing a `MULE:TIMEOUT` error, the Scatter-Gather component collects the
successful results and the errors in the `MULE:COMPOSITE_ROUTING` error. Your configured error
handler then processes this error.

## Example Project

To learn more about how to use the Scatter-Gather component, download and open the example project
_Scatter-Gather Flow Control_ from Anypoint Exchange in Anypoint Studio. This example shows how to
use the Scatter-Gather component to aggregate data in parallel and return the result in JSON.

The example uses prepared data as input for two resources to aggregate. The data represents
information about two contacts and has this structure:

<table><colgroup><col> <col> <col> <col> <col></colgroup><tbody><tr><td><p>Resource</p></td><td><p><code>firstname</code></p></td><td><p><code>surname</code></p></td><td><p><code>phone</code></p></td><td><p><code>email</code></p></td></tr><tr><td><p>contacts-1.csv</p></td><td><p>John</p></td><td><p>Doe</p></td><td><p>096548763</p></td><td><p><a href="mailto:john.doe@texasComp.com">john.doe@texasComp.com</a></p></td></tr><tr><td><p>contacts-2.csv</p></td><td><p>Jane</p></td><td><p>Doe</p></td><td><p>091558780</p></td><td><p><a href="mailto:jane.doe@texasComp.com">jane.doe@texasComp.com</a></p></td></tr></tbody></table>

A DataWeave script aggregates the contact information into a JSON structure that represents the data
from both resources.

To download and open this example project in Anypoint Studio, click the Exchange icon in the
upper-left corner. In the window that opens, log in to Anypoint Exchange and search for the project
name.

## Scatter-Gather XML Reference

<table><colgroup><col> <col></colgroup><thead><tr><th>Element</th><th>Description</th></tr></thead><tbody><tr><td><div><div><p><code>scatter-gather</code></p></div></div></td><td><div><div><p>Sends a request message to multiple targets concurrently. It collects the responses from all routes and aggregates them into a single message.</p></div></div></td></tr><tr><td colspan="2"><div><div><p><strong>Attributes</strong></p></div></div></td></tr><tr><td><div><div><p><code>timeout</code></p></div></div></td><td><div><div><p>Sets the timeout for responses from sent messages, in milliseconds. A value of 0 or lower thatn 0 means no timeout.</p></div></div></td></tr><tr><td><div><div><p><code>maxConcurrency</code></p></div></div></td><td><div><div><p>Determines the maximum number of concurrent routes to process.<br>By default, all routes run in parallel.</p></div><div><p>If you set this value to 1, the Scatter-Gather component processes the routes sequentially.</p></div></div></td></tr><tr><td><div><div><p><code>target</code></p></div></div></td><td><div><div><p>Name of the target variable.</p></div></div></td></tr><tr><td><div><div><p><code>targetValue</code></p></div></div></td><td><div><div><p>Value of the data to store in the target variable.<br>If not set, the default value is <code>#[payload]</code>.<br>This field accepts any value that a variable accepts:</p></div><div><ul><li><p>Any supported data type</p></li><li><p>DataWeave expressions</p></li><li><p>The keywords <code>payload</code>, <code>attributes</code>, and <code>message</code>, but <em>not</em> the keyword <code>vars</code></p></li></ul></div></div></td></tr></tbody></table>

<table><colgroup><col> <col></colgroup><thead><tr><th>Child Element</th><th>Description</th></tr></thead><tbody><tr><td><p><code>route</code></p></td><td><p>A route in the Scatter-Gather router.</p></td></tr></tbody></table>

### Throws

- `MULE:ROUTING`

- `MULE:COMPOSITE_ROUTING`

## See Also

- [Core Components](https://docs.mulesoft.com/mule-runtime/latest/about-components)

- [How Transactions Affect Scopes and Routers](https://docs.mulesoft.com/mule-runtime/latest/transaction-management#tx_scopes_routers)

- [Error Handling](https://docs.mulesoft.com/mule-runtime/latest/error-handling)

- [Try Scope](https://docs.mulesoft.com/mule-runtime/latest/try-scope-concept)
