Parallel work needs a valid way to combine results
Parallel decomposition helps when independent work exceeds coordination overhead. A reduction must use a suitable identity and associative combination if partitions may be grouped arbitrarily.
fun combineCounts(first: Map<String, Long>, second: Map<String, Long>): Map<String, Long> {
val result = first.toMutableMap()
for ((word, count) in second) result[word] = (result[word] ?: 0L) + count
return result
}
Word-count maps can be combined in any grouping under bounded integer arithmetic. Floating-point addition is not exactly associative, so parallel grouping can alter the last bits. Define deterministic or tolerance-based expectations accordingly.
MapReduce additionally moves grouped keys across machines. Data skew can overload one reducer, and retries require side-effect policies that tolerate repeated execution. A parallel loop alone is not a distributed MapReduce system.
Exercise
Partition a token list several ways, count locally, combine maps, and compare with a sequential baseline. Include one dominant key to discuss skew. Test identity with an empty map.
Check: account for serialization, communication, startup, and sequential portions; adding workers does not imply linear speedup.