For Each and Parallel For Each Comparison in MuleSoft (Mule 4)


For each is used to perform similar set of activities on same payload of a collection. For each will expect collection as input payload and it will iterate over the collection. Point to remember. If it’s a Java collection then it will split the payload by default.

The Foreach scope splits a collection into elements and processes them iteratively through the processors embedded in the scope, then returns the original message to the flow. The collection can be any supported content type, such as application/jsonapplication/java, or application/xml.

General considerations about the For Each scope:

  • By default, For Each tries to split the payload. If the payload is a simple Java collection, the For Each scope can split it without any configuration. The payload inside the For Each scope is each of the split elements. Attributes within the original message are ignored because they are related to the entire message.
  • For Each does not modify the current payload. The output payload is the same as the input.
  • For non-Java collections, such as XML or JSON, use a DataWeave expression to split data. Use the Collection field for this purpose.
  • In the following example, the Collection field in For Each is set to iterate over an array stored in


The For Each scope stores each item of the collection in payload during each iteration.

You can also split an array into batches to enable quicker processing. Each batch is treated as a separate Mule message. For example, if a collection has 200 elements and you set Batch Size to 50, the For Each scope iteratively processes 4 batches of 50 elements, each as a separate Mule message.

Parallel For Each

The Parallel For Each scope enables you to process a collection of messages by splitting the collection into parts that are simultaneously processed in separate routes within the scope of any limitation configured for concurrent-processing. After all messages are processed, the results are aggregated following the same order they were in before the split, and then the flow continues.

Like for-each scope, parallel for each scope also splits the collection of messages into elements.

But, unlike the for-each scope, it processes each element simultaneously in separate routes and the result is the collection of all messages aggregated in the same sequence they were before the split.


  • 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 high number of entries. To process large payloads, use Batch Processing instead.
  • Anypoint Studio versions prior to 7.6 do not provide this feature in the Mule Palette view. To use Parallel for Each in those versions, you must manually configure Parallel For Each scope in the XML.


The Parallel For Each scope can be configured through the following fields:

Child elementDescription
Collection (collection)Specifies the expression that defines the collection of parts to be processed in parallel. By default, it uses the incoming payload.
Collection Expression (collection)An expression that returns a collection. By default, the payload is taken as the collection to split.
Timeout (timeout)Specifies the timeout in milliseconds for each parallel route. By default, there is no timeout.
Max Concurrency (maxConcurrency)Specifies the maximum level of parallelism for the router to use. By default, all routes run in parallel.
Target Variable (target)Specifies a variable to use for storing the processed payload. By default, the output is saved in the flow’s payload.
Target Value (targetValue)Specifies an expression to evaluate against the operation’s output value. The outcome of this expression is stored in the target variable. By default, this is the same as the operation’s output value.

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.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.



Leave a Reply

Your email address will not be published. Required fields are marked *