When to reach for it
- Large inputs can be split into independent chunks.
- Aggregation logic is clearly definable.
- Throughput scalability is critical.
Map over chunks. Reduce to one answer.
A large task is mapped over independent chunks and subsequently aggregated or reduced into a single result.
Input is split, mapped in parallel, then reduced.
| Stage | Input | Calls | Output |
|---|---|---|---|
| Split | 300 reviews | 0 | 6 chunks of 50 reviews |
| Map | One chunk per call | 6 | Per chunk, a list of complaints with counts |
| Reduce | The 6 partial lists | 1 | One ranked list for all 300 reviews |
from concurrent.futures import ThreadPoolExecutor
def top_complaints(reviews, chunk_size=50):
chunks = [reviews[i:i + chunk_size] for i in range(0, len(reviews), chunk_size)]
with ThreadPoolExecutor() as pool:
# map: the same call for every chunk
partials = list(pool.map(extract_complaints, chunks))
# reduce: one call over the partial lists
return merge_and_rank(partials)When to reach for it
When it backfires
The reducer only sees per-chunk outputs and misses relationships that span chunk boundaries.
Countermeasure
Include chunk metadata (position, overlap) in mapper output, or use a two-pass reducer that first reconciles boundaries.
One chunk is 10x larger than the others. Its mapper dominates latency and the whole job waits.
Countermeasure
Use dynamic chunk sizing or a size-aware splitter that balances load across mappers.
Next pattern
Search patterns, frameworks, and pages.