ForEach & Parallel ForEach Scopes โ Interview Questions & Scenarios (2026)
Sequential vs parallel record processing, exception isolation, payload mutability, and memory impact.
๐ ForEach & Parallel ForEach Scopes
Sequential vs parallel record processing, exception isolation, payload mutability, and memory impact.
What is ForEach? How does it work and what is its output?
ForEach iterates over a collection sequentially โ one item at a time. For each iteration: payload =
current item, vars.counter = current index (1-based).
vars.results) and append each iteration's result to it.๐ฌ CROSS-QUESTIONS INTERVIEWERS ASK ABOUT FOREACH
"After ForEach โ is the payload still the original array or the processed results?" โ Original array โ
payload is UNCHANGED. Collect results in a variable: vars.results = vars.results ++ [payload] inside
the loop. After loop: payload = original, vars.results = collected results.
"I set a variable inside ForEach โ is it visible after the loop?" โ YES. Variables (vars) set inside ForEach
ARE visible after the loop exits. Inside Scatter-Gather, vars set in one route are NOT visible to sibling routes running in parallel, but once all routes complete, Mule 4 merges/propagates them back into the main flow (same-named vars with different values merge into an Array). By contrast, vars set inside an Async scope never propagate back to the main flow.
"What is vars.counter inside ForEach?" โ 1-based index of the current iteration. First item = counter 1,
second = counter 2. Useful for batch numbering or logging position.
If a record fails inside ForEach, what happens? How do you handle it?
By default: ForEach stops processing and propagates the error to the flow's error handler. All
subsequent records are abandoned.
Fix: Wrap the content of ForEach in a scope with on-error-continue . The failed record is handled
locally, ForEach continues with the next record.
ForEach with per-record error handling
What is Parallel ForEach? How is it different from ForEach?
Parallel ForEach processes all collection items simultaneously using thread pool. Dramatically faster
for large collections when items are independent.
| ASPECT | FOREACH | PARALLEL FOREACH |
|---|---|---|
| Processing | Sequential โ one at a time | Parallel โ all at once |
| Order | Guaranteed order | Order NOT guaranteed |
| Use when | Items depend on each other, order matters | Items are independent, performance critical |
| Thread safety | Single thread โ no conflicts | Beware shared mutable state (variables) |
| In our Enterprise Architecture | Sequential SAP calls with dependencies | 50 parallel account updates โ 60% faster |
Project: Aviation โ Customer 360 Aggregation API.
Problem: Sequentially calling 3 backend systems (Amadeus GDS, Salesforce Loyalty Cloud, Snowflake Data Lake) took over 3.2 seconds, breaching client SLA thresholds during high-load periods.
What I Did: Orchestrated the backend calls using a Scatter-Gather router with a 1500ms timeout, wrapping each branch in a local Try scope with On Error Continue fallback payloads.
Result: Slashed end-to-end response latency from 3.2s to 680ms (a 78% speedup) while guaranteeing graceful degradation if one backend was slow.