MapReduce
Read first: Partitioning and sharding
MapReduce, published in 2004, maps input files to key-value pairs, groups the pairs by key, and reduces each group to output files on GFS. A failed task is re-run, and a straggler gets a speculative second copy. It is a batch system, not a database and not a low-latency store.
MapReduce takes input files, runs a function that emits key-value pairs, groups every pair that shares a key, and runs a second function on each group. Dean and Ghemawat published it at OSDI in 2004. Input and output are files on GFS. The job finishes when the tasks finish, and the result is more files. It is a batch system. It is not a database, and it is not a low-latency store. If a design needs a point read on a user-facing clock, this is the wrong box, and the rest of the machinery will not change that.
The problem
You have a pile of records that already lives in files, and you want one answer per group. Count tokens. Build an index by term. Aggregate yesterday’s events by account. The input is far too large for one careful loop on one machine, and the machines you do have will die in the middle. A programmer who has to think about which machine holds which file will write that logic badly, once, inside every job.
The shape of the work is the same across those jobs. Look at each record independently and emit intermediate pairs. Move all the pairs for one key to one place. Look at that group and write an output. The independent part parallelizes. The group-by-key part is the only place the records have to meet. Failures should recompute a piece from the files, because the files are still there.
What the problem is not: a query. Nobody is waiting on the other end of an RPC for one key with a latency budget. There is no secondary index to maintain, no transaction to commit, no row to update in place. The user of MapReduce is a job, and the user of the job is a person or a pipeline who can wait until the output files exist.
That boundary is the whole lesson. The programming model turned out to be more durable than any one cluster. Later batch engines still look like map, group by key, reduce. People still say “MapReduce” when they mean a database. Correct the sentence before you draw anything.
The design
You write two functions. Map takes one input record and emits zero or more key-value pairs. Shuffle groups the pairs by key, so every value for a key arrives together. Reduce takes one key and its group and writes an output. The system applies map across the input files, runs the shuffle, applies reduce, and leaves output files on GFS.
The toy below is an illustration of that dataflow. It is not a Google workload and it is not a benchmark.
input lines, assumed for the example
red blue
blue green
red red
map emits
red 1, blue 1
blue 1, green 1
red 1, red 1
shuffle groups by key
red -> 1, 1, 1
blue -> 1, 1
green -> 1
reduce writes the group out
red 3
blue 2
green 1
Each map task reads its own input split from GFS and writes intermediate pairs. Each reduce task reads the groups it was assigned and writes an output file. Tasks that have finished stay finished. The job’s output is the set of those files, not a row that appears in a table as each key completes.
Failure is a re-run. A worker dies. The tasks it had not finished are run again, on another worker, reading the same input files. This is possible because the input is still on GFS and because the task’s job is to produce a file from that input. You recompute the lost piece. You do not restart the tasks that already produced their output.
Put numbers on that, and label them as assumptions. They are there so the fraction is visible.
assumed map tasks in the job 1,000
assumed tasks on the dead worker 20
work to re-run 20/1000 = 2%
Two percent of the map work, not the job from the beginning. That arithmetic is the reason a batch system of this shape can lose machines and still finish. It is also the reason a task must not send mail, charge a card, or increment a remote counter. A re-run would do it again. The output channel the design gives you is a file.
The straggler. The job is done when the last task is done. One sick disk, one overloaded machine, one task that is simply behind, and every consumer of the output waits on that task. Speculative execution starts a second copy of a task that is lagging. Whichever copy finishes first supplies the output. You paid for the same input to be read twice so that one slow machine would not set the clock for the batch.
The hot key. Shuffle is a partition of the intermediate data. Same key, same reducer. That is partitioning applied to the group-by. A key with a huge group lands entirely on one reducer. Adding reducers moves other keys onto other machines and leaves that group where it is. A hot key is not fixed by more partitions. If the toy’s “red” had been almost every token, one reducer would have done almost the whole job while the others finished instantly and waited.
flowchart LR
input[Input files on GFS]
map[Map tasks]
shuffle[Group by key]
reduce[Reduce tasks]
output[Output files on GFS]
input --> map --> shuffle --> reduce --> output
Two more arrows belong on that drawing. A failed map or reduce task is a new attempt of that task, reading input that is still on GFS. A straggler is a second copy racing the first. Neither arrow turns the drawing into a serving path. There is still no place a user request reads one key and returns.
What they traded
| Choice | What you gain | What you spend |
|---|---|---|
| Map, then group by key, then reduce | One programming model for a large class of batch jobs | Anything that is not “independent map, then one key’s group” is a poor fit |
| Input and output are files on GFS | A dead worker’s task can be re-run from the same input | You have files when the job ends, not a row in the middle |
| Re-run the failed task only | One dead machine costs a fraction of the job | The task must be safe to execute twice, because it will be |
| Speculative copy of a straggler | One slow machine does not set the job’s end time | Extra load, and no help when every task is slow |
| One reducer per key | The reduce function sees the whole group | A hot key is a one-machine reduce, and more reducers do not split it |
| Batch boundary | Failures and retries have a simple story | A user request cannot wait here for a point read |
The failure mode that surprises people who liked the 2% arithmetic is skew, not death. Machines stay up. The job still runs as slow as the largest group. You can watch every reducer finish except one, and that one is chewing a key the shuffle was required to keep intact. The symptom looks like a straggler, and speculative execution will start a second copy of the same enormous group. Now two machines are each processing the whole hot key. You spent the speculative machinery on a problem it cannot split.
The second failure is a side effect hiding inside map. The function looks pure in review. In production it calls another service “just to record progress.” A worker dies after the call and before the task is marked done. The re-run calls again. Your output file is correct and your other service is double-counted. The design gave you re-execution because the output is a file. The moment the task reaches outside that file, re-execution is a duplicate event. Keep the side effect out, or put it in a system that knows how to dedupe, and do not expect MapReduce to become that system.
The third failure is using the output as if it were a table that gets updated. Readers who consume output files while reducers are still writing will see a partial job. The result is the files at the end. Publishing a partial job is a different system. This one gives you the files when the tasks are done.
What to steal
Steal the boundary. Batch in, files out, recompute the piece that failed, and watch the max rather than the average. Steal the hot-key warning in particular. Any pipeline that groups by a key, including ones that have never been called MapReduce, will put the largest group on one worker. Say what that key is before you quote a machine count.
Steal the discipline on side effects. If a task can be re-run, it has to be safe to re-run. Writing a file from an immutable input is safe. Sending a message is not, unless the receiver treats a repeat as the same message. That sentence is the part worth taking into a small codebase that shells out to one process and a folder of files.
Leave the cluster on the shelf when the input fits on one disk and the job finishes before lunch on one machine. A distributed scheduler, a shuffle, and speculative copies are how you spend engineering time to go slower than a loop. Also leave it off the request path. A product read of one id is a database problem or a cache problem. Pointing it at a batch engine gives you file latency, a job boundary, and no index.
If an interviewer says “we’ll MapReduce the timeline,” ask whether they mean a batch rebuild of a derived file, or a read of one user’s rows. The first can be this shape. The second needs a store that answers a lookup. The programming model outlived the original system, which is why the confusion survives. The files-in, files-out boundary is how you end the confusion in one sentence.
Check yourself
A worker dies after finishing some of its map tasks and before finishing others. What is re-run, and why is that enough?
The unfinished map tasks are re-run. They read the same input files from GFS and emit their pairs again. Tasks that already finished keep their output. This works because the input is still there and the task writes a result rather than mutating a shared row. In the teaching numbers, 20 tasks out of an assumed 1,000 is 2 percent of the map work, not a full restart.
Almost every intermediate pair has the same key. Why does adding more reducers leave the job slow?
Shuffle keeps one key on one reducer so the reduce function sees the whole group. Extra reducers take other keys. A key that has almost all the values still arrives as one group on one machine. A hot key is not fixed by more partitions. A speculative second copy of that reducer processes the same huge group twice.
Speculative execution starts a second copy of a lagging task. When does that second copy fail to shorten the job?
When the slowness is everywhere rather than in a straggler. The job ends when the last task ends, so a second copy helps if the first copy is stuck on one sick machine and the copy runs on a healthy one. If every task is slow, both copies are slow and you have doubled the read and the compute. The hedge pays off for a minority of slow machines.
Someone proposes serving get-by-id traffic from MapReduce so the product always reads fresh output. What is the mismatch?
MapReduce writes output files when tasks finish. It does not serve a point read under a latency budget, and it is not a database with an index of one id. A user request needs a store that can fetch a row. A batch job can build a file that a store later loads. The job itself is the wrong place to wait for one key.
Based on: MapReduce, OSDI 2004, Dean and Ghemawat.