Parallel For Each: results, variables and performance
Explore result aggregation, variable behaviour and the trade-offs to assess before using parallel processing.
Parallel For Each looks like a drop-in upgrade to For Each. It is not. The behaviour differs in three ways that matter, and only one of them is speed.
It does give you the results back
Unlike For Each, this scope aggregates. The payload after it is a collection of Mule messages, one per input element, in the original order even though they executed concurrently. So the accumulator variable you had to write for For Each is unnecessary here — but the shape is a collection of messages, not a plain array, so your transform needs to reach into each one.
%dw 2.0
output application/json
---
payload map ((item) -> item.payload)
It swallows your variables
Variables set inside the scope are not visible after it. This is the exact
opposite of For Each and it is the single most common bug when someone converts one
to the other. Code that read vars.results after the loop keeps
compiling, keeps running, and quietly reads whatever the variable held beforehand.
Errors arrive as one composite
If any route fails, the scope still lets the others finish, then raises
MULE:COMPOSITE_ROUTING. The error object carries the failures keyed by
index, so you can report precisely which elements failed and why. Handle that error
type explicitly — catching a generic ANY and logging
error.description throws away the detail that makes the incident
diagnosable.
When parallel is genuinely slower
Concurrency costs threads, and threads are not free. Parallel For Each pays off when the work inside each iteration is I/O bound and slow — an HTTP call, a database round trip, a file write. It loses when:
- The collection is small. Ten items finishing in 20 ms each will not notice.
- The work is CPU bound, such as a heavy DataWeave transform. You are competing for the same cores and adding scheduling overhead.
- The downstream system rate-limits you. Twenty concurrent calls into an API that permits five produces 429s, retries, and a slower net result than going one at a time.
Set maxConcurrency deliberately rather than leaving it at the default. It should reflect what the downstream system will tolerate, not what your worker can emit.
Transactions
Do not assume routes run concurrently inside a transactional flow. Transaction context does not span parallel routes the way people expect, and behaviour here has caught out enough teams that the safe move is explicit: if a unit of work must be atomic, keep it sequential and make the atomicity visible in the flow rather than implied by the scope.
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.
