13 / 35 · Distributed Computation
The MapReduce model
Turn input records into intermediate key-value pairs, route each key to one reducer and trace retries of both stages.
The sales totals need two local operations and one exchange. We first produce pairs of store and amount. Then all amounts for one store meet at the same destination. MapReduce exposes these responsibilities to the programmer and lets the runtime handle distribution and retry.
Map records to keys#
A map task reads its assigned split of the input. Its map function can emit zero or more intermediate key-value pairs per input record. For the sales example, it emits the store ID and amount in cents. Reducers receive the values associated with a key. A reduce function consumes those values and emits the total. The framework handles routing. (Dean and Ghemawat, 2004, §§2–3; Apache Hadoop 3.4.2, Mapper and Reducer)
def map_sale(store_id, amount_cents):
yield store_id, amount_cents
def reduce_store(store_id, values):
yield store_id, sum(values)
With two reducers, the toy partitioner (store_id - 1) % 2 sends store 1 to partition 0 and store 2 to partition 1. Equal keys must select the same partition. In the shuffle, reducers fetch the assigned pieces of map output. The runtime groups equal keys and provides their values together. The totals are 800 and 550 cents. One reducer may handle several keys if more stores exist.
The key determines the reducer
A combiner is optional#
Map worker A emits amounts 100 and 200 for store 1. It can combine those into one partial of 300 before the reducer fetches the data. Local combining can shrink transfer. The reduce function must also work without it. Frameworks may combine a subset of values or apply the combiner more than once. Addition in a sufficiently wide integer representation gives the same result in each case. Sending a bare local mean would break this property. (Dean and Ghemawat, 2004, §4.3; Apache Hadoop 3.4.2, Mapper)
Retry tasks whose output is missing#
In the original Google implementation, map output lives on workers’ local disks. If a worker is lost, its completed map work can be rerun from the input split. The coordinator tells reducers which map output to fetch. The reducer writes its own result to a temporary file, then publishes its output through an atomic rename. These rules make repeated attempts compatible with one result per task when computation is deterministic. They do not undo a network request or another external effect emitted by a user function. (Dean and Ghemawat, 2004, §§3.3–3.4)
A sequence of separate MapReduce jobs can express more stages. Writing and reading their durable intermediate results can be costly when several calculations repeatedly use the same data. This motivated execution models with reusable partitions. (Zaharia et al., 2012, §§1–2)
Next#
The Spark execution model keeps the idea of shuffling by key and follows a longer chain of dependencies. It can run adjacent local transformations in one stage and recompute missing partitions from their inputs.
References#
- Jeffrey Dean and Sanjay Ghemawat. MapReduce: Simplified Data Processing on Large Clusters. OSDI, 2004, §§2–4 on functions, partitioning, fault recovery and combining.
- Apache Software Foundation. MapReduce Tutorial. Hadoop 3.4.2 documentation on the mapper, partitioner, shuffle and reducer.
- Matei Zaharia et al. Resilient Distributed Datasets. NSDI, 2012, §§1–2 on reuse across iterative computations.