MapReduce
The idea behind the mapreduce(MR) paper originated when google noticed that it kept running the same kind of program repeatedly. These were programs consuming web request logs, document crawlers and other large datasets. These datasets were far too large for a single machine, so the work had to be spread across multiple computers.
To make the operations concrete, we will use an example: counting the occurrence of a word in a collection of documents.
Map
The map function takes a key/value pair as input and emits a list of intermediate key/value pairs.
map(key1, value1)
→ emit IntermediateData(list(key2, value2))
In our word-count, key1 is the file name and value1 is the file’s content. For every word it encounters, the map function emits the word as key2 and value "1".
map("file.txt", "file content")
→ emit IntermediateData(list(word, "1"))
A word that appears three times in a document(s) therefore emits three (word, "1"). These intermediate pairs are then grouped together and prepared for the reduce phase.
Reduce
Before the reduce runs, the intermediate data is shuffled(partitioned by key so every occurence of a word lands in the same reduce task, then fetched) and sorted by key. The reduce function takes this sorted intermediate pair and reduces the values to possibly a smaller set.
reduce(key2, Iterator(value2))
→ emit Output(operation(list(value2)))
In our example, reduce receives a word as key2 and a list of occurrences recorded for it, such as ["1", "1"].
reduce("word", ["1", "1"])
→ emit Output("2")
How it works
The user submits a program to the system. The MapReduce library first splits the inputs into M pieces, typically 16-64MB each, then starts multiple copies of this program across the cluster. One copy is designated the master, the rest are workers.
Master
The master is the coordinator. It assigns tasks to workers, tracks the state of every task(idle, in-progress, completed) along with the health of every worker, records the location of intermediate data so that reduce workers know where to fetch from, and finally returns control to the user program once the job is complete.
It is worth noting that is there is only one master, which unarguably is a single point of failure. Later in this section we will explore the authors’ rationale towards this decision.
Worker
Workers are processes that execute the user’s map and reduce functions. They run on separate machines and carry out their tasks in parallel.
Process
A MapReduce job is divided into M map tasks and R reduce tasks. Both M and R are expected to be way larger than the available workers. This improves dynamic load balancing and speeds up recovery when a machine fails. The master makes O(M + R) scheduling decisions and holds O(MxR) state, which bounds how large they can go.
During the map phase, a worker reads its assigned input splits and runs the user’s map function over them. The intermediate key/value pairs are buffered into memory then gradually written to the worker’s local disk where they are written into partitions using a partitioning function, typically in the form of hash(key) % R. The worker then reports the location back to the master.
For a reduce task, the worker, via an RPC reads the intermediate key/value from the local disk of the map task workers, sorts that data by key then runs the client reduce function and writes the result to the GFS. Each reduce task produces exactly one output file, so a complete job leaves behind R output files. Once every task is completed, the master wakes up the user program.
Fault Tolerance
In a distributed system, we expect machines to fail, and MapReduce is built around that assumption. The master has the duty of constantly checking the health of workers by pinging them periodically.
If a map worker crashes, its completed work lives on the local disk which is no longer accessible, so these tasks are reset to idle and re-executed elsewhere. Completed reduce tasks need no re-execution, since their output is already in GFS. In-progress map and reduce tasks on the failed worker are likewise reset to idle and rescheduled.
Now consider a more nuanced case where the master pings a worker and receives no response. It cannot tell whether the worker crashed or a network partition has cut it off, so it reassigns the task. If the original worker is still alive, both workers may complete the same task. For map tasks this is harmless, since the master keeps the first and ignores duplicate completion messages; meanwhile for a reduce task, correctness comes from atomic commits: output is written to a temporary file first and only renamed to its final name after completion, ensuring only one version wins.
Earlier we mentioned that there is only one master, so the probability of it failing during any given job is low. The paper notes that periodic checkpointing would be straightforward to implement, but the authors chose instead to abort the job and let the client retry, on the ground that master failure is unlikely, therefore doesn’t justify the complexity.
Performance
The major bottleneck in this system is the network bandwidth, so MapReduce tries to avoid it. The master attempts to schedule each map task on a machine that already holds a replica of the corresponding input split, so the data is read locally rather than over the network. If this is not possible, it tries a machine near a replica usually on the same network switch. This strategy works because GFS typically keeps three replicas of every block.
Another problem the authors encountered was stragglers. These are the machines that take an unusually long time to finish the last few map or reduce tasks, often due to reasons such as a bad disk or contention for resources if other tasks are scheduled to run on the machine. The mitigation for this is backup tasks. As the job approaches completion, the master schedules another copy of the remaining in-progress tasks, and whichever one finishes first is the one that counts.
An example from the paper shows that without backup execution, a sort job takes 44% more time to complete.
Combiner Function
Recall that in our word-count example, each word is emitted as many times as it appears in the document. Since network bandwidth is already a bottleneck, running the job this way is a poor choice. The MapReduce authors therefore provide the option for a combiner function, which partially merges the data before it is sent to the reduce task. The combiner is typically the same as the reducer; the only difference is how the MapReduce library handles the output. The result of a reduce function is written to the final output file, whereas the result of a combiner is written to an intermediate file.
Key Takeaways
The authors began by figuring out what the most important property of the system should be, and settled on abstraction for developers. By requiring the users to write only map and reduce functions, MapReduce keeps the complexity of distributed execution hidden beneath the surface. The constraint is leverage.
They also designed for failures from the start. Machines will die mid-job, and the recovery machinery handles it enough that the users never have to know it happened.
This paper gives us a solid entry to distributed systems.