---
schema_version: "1"
id: 78d6159cd7d833ded49c7dd30b8028929865e5a35dd46bfd19ef47480c0d615c
title: Parallel For Each (<parallel-foreach/>)
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/anypoint-code-builder/acb-component-parallel-for-each.md
canonical_url: https://docs.mulesoft.com/anypoint-code-builder/acb-component-parallel-for-each
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 662389b842ce02ccb4dc29d5f270506a115b4814e86879b3969ee9952a9d67f8
description: MuleSoft Documentation Site
latest: true
---
---
product: Anypoint Code Builder
version: master
---

# Parallel For Each (<parallel-foreach/>)

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

Process a collection of messages by splitting the collection into parts that are simultaneously
processed.

After processing all messages, the component aggregates the results following the same order they
were in before the split, and then the flow continues.

Parallel For Each buffers all processing routes' results in a list to return it after the scope
finishes processing, which can cause out-of-memory errors when processing a large number of entries.
To process large payloads, use Batch Processing instead.

## Component XML

This component supports the following XML structure:

```xml
<parallel-foreach
  doc:name="Parallel foreach"
  doc:id="tbfiiz"
  collection="#[payload]"
  timeout=30000
  maxConcurrency=15 />
```

Parallel For Each (`<parallel-foreach/>`) attributes are configurable through the UI and XML.

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Attribute XML</th><th>Description</th></tr></thead><tbody><tr><td><p><strong>Parallel foreach</strong> (default)</p></td><td><p><code>doc:name</code></p></td><td><div><div><p>Editable name for the component to display in the canvas.</p></div></div></td></tr><tr><td><p>N/A</p></td><td><p><code>doc:id</code></p></td><td><div><div><p>Automatically generated identifier for the component.</p></div></div></td></tr><tr><td><p><strong>Collection</strong></p></td><td><p><code>collection</code></p></td><td><div><div><p>An expression that returns a collection. Defaults to <code>#[payload]</code>.</p></div></div></td></tr><tr><td><p><strong>Timeout</strong></p></td><td><p><code>timeout</code></p></td><td><div><div><p>Specifies the timeout in milliseconds for each parallel route.</p></div></div></td></tr><tr><td><p><strong>Max concurrency</strong></p></td><td><p><code>maxConcurrency</code></p></td><td><div><div><p>Specifies the maximum level of parallelism for the router to use.</p></div><div><p>+ By default, all routes run in parallel.</p></div></div></td></tr><tr><td><p><strong>Target Variable</strong></p></td><td><p><code>target</code></p></td><td><div><div><p>Name of the variable in which you want to store message data. Names can only include numbers, characters, and underscores. For example, hyphens are not allowed in the name. See <a href="https://docs.mulesoft.com/mule-runtime/latest/target-variables">Enrich Data with Target Variables</a> in the Mule documentation.</p></div></div></td></tr><tr><td><p><strong>Target Value</strong></p></td><td><p><code>targetValue</code></p></td><td><div><div><p>Value of the data to store in the target variable. By default, the value is the message payload (payload). The field accepts any value that a variable accepts: any supported data type, DataWeave expressions, the keywords <code>payload</code>, <code>attributes</code>, and <code>message</code>, but not the keyword <code>vars</code>. See <a href="https://docs.mulesoft.com/mule-runtime/latest/target-variables">Enrich Data with Target Variables</a> in the Mule documentation.</p></div></div></td></tr></tbody></table>

## Examples

### Example: Appending Values

The following example appends the string `"-result"` to every element in the collection:

```xml
<flow name="myFlow">

  <parallel-foreach collection="#[['apple', 'banana', 'orange']]">
      <set-payload value="#[payload ++ '-result']"/>
  </parallel-foreach>

</flow>
```

### Example: Modifying Variables

The following example demonstrates that changes to existing variables inside the Parallel For Each
scope are temporary and do not persist after the execution finishes:

```xml
<flow name="myFlow">

  <set-variable variableName="var1" value="var1"/>
  <set-variable variableName="var2" value="var2"/>
  <parallel-foreach collection="#[['apple', 'banana', 'orange']]">
      <choice>
          <when expression="#[payload == 'apple']">
              <set-variable variableName="var2" value="newValue"/>
              <set-variable variableName="var3" value="appleVal"/>
          </when>
          <when expression="#[payload == 'banana']">
              <set-variable variableName="var3" value="bananaVal"/>
          </when>
          <otherwise>
              <set-variable variableName="var3" value="otherVal"/>
              <set-variable variableName="var4" value="val4"/>
          </otherwise>
      </choice>
  </parallel-foreach>

</flow>
```

The example produces the following output:

```json
{
  "var1": "var1",
  "var2": "var2"
}
```

Every execution of the Parallel For Each scope starts with the same variables and values as before
the execution of the block.

New variables or modifications of already existing variables while processing one element are not
visible while processing another element. All of those variable changes are not available outside
the Parallel For Each scope. The set of variables (and their values) after the execution of the
Parallel For Each Scope remains the same as before the execution.

## Error Handling

Because the scope processes every route in parallel, if an error occurs in one route, processing
continues in all of the other routes until all finish processing. After that, Parallel For Each
aggregates all results (and any errors) and processes them by the error handler, as shown in the
following example:

```xml
<flow name="myFlow">

  <parallel-foreach collection="#[['banana', 'apple']]">
      <choice>
          <when expression="#[payload == 'banana']">
              <!-- Processor that throws error -->
          </when>
          <otherwise>
              <set-payload value="#[payload ++ '-result']"/>
          </otherwise>
      </choice>
  </parallel-foreach>
  <error-handler>
      <on-error-continue type="COMPOSITE_ROUTING">
          <!-- This will have the error thrown by the previous processor -->
          <logger message="#[error.errorMessage.payload.failures['0']]"/>
          <!-- This will be a null value -->
          <logger message="#[error.errorMessage.payload.failures['1']]"/>
          <!-- This will be a null value -->
          <logger message="#[error.errorMessage.payload.results['0']]"/>
          <!-- This will have the result of this (correctly executed) route -->
          <logger message="#[error.errorMessage.payload.results['1']]"/>
      </on-error-continue>
  </error-handler>

</flow>
```

### Throws

- `MULE:COMPOSITE_ROUTING`

## Differences between For Each and Parallel For Each Scopes

Both For Each and Parallel For Each split the defined collection, and the components within the
scope process each element in the collection. Also, in both cases, each route runs with the same
initial context. The difference between these two scopes are:

- For Each works sequentially, while the Parallel For Each processes in parallel. This difference
  affects _error handling_:

  Because of the processing differences, the execution of For Each execution is interrupted when an
  error is raised (and the Error Handler is invoked), while Parallel For Each processes every route
  before invoking the Error Handler with a `MULE:COMPOSITE_ROUTE` error type.

- For Each does not modify the payload, while the Parallel For Each outputs a collection of the
  output messages from each iteration.

## See Also

- [For Each Scope](https://docs.mulesoft.com/anypoint-code-builder/acb-component-for-each).

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