MapReduce: Simplified Data Processing on Large Clusters
2004 · OSDI · ~12 min read
Read the original paperRuns a two-function program over terabytes on thousands of failing machines. Read it for the fault-tolerance reasoning: which work is re-run, which is not, and what the programmer must promise.
The MapReduce paper lets a programmer process terabytes on thousands of machines by writing two small functions. The library takes care of splitting the input, scheduling work, moving data between machines and redoing work when machines die. Reading the paper today is worth it less for the API than for the reasoning about failure: why some work is re-run and some is not, and what the programmer has to promise for that to be correct.
The situation in 2003 was this. Google’s engineers had hundreds of special-purpose programs that computed things over crawled documents and request logs: inverted indexes, link graphs, the most frequent queries of the day. Each computation was simple in itself. Each also had to run on hundreds of cheap Linux machines with two to four gigabytes of memory, on a network where some machine was always failing. So each program carried its own code to partition input, ship it, handle a worker that disappeared, and stitch output back together. Jeff Dean and Sanjay Ghemawat noticed that these programs had the same shape and that the scaffolding could be written once.
The shape that kept repeating
The programmer writes a map function, which takes one input record and emits any number of intermediate key and value pairs, and a reduce function, which takes one intermediate key with all the values emitted for it and produces output. Word count is the paper’s own first example, and it will be ours.
map(line):
for word in line.split():
emit(word, 1)
reduce(word, counts):
emit(word, sum(counts))Our input is three lines, each treated as one split: “the quick brown fox jumps over the lazy dog”, “the dog sleeps and the fox runs”, and “a quick fox and a quick dog”. Map turns the first line into nine pairs, the second into seven and the third into seven: 23 pairs in all. Reduce for the key “the” receives [1, 1, 1, 1] and emits 4. Reduce for “quick” receives three ones, one from the first line and two from the third, and emits 3.
The restriction is what matters. Map looks at one record and nothing else. Reduce looks at one key and nothing else. Because neither function depends on what is happening elsewhere, the library is free to run any map on any machine, in any order, and to run the same map twice if the first attempt is lost. A programmer who could write arbitrary distributed code would take that freedom away.
Following one job through the cluster
The paper describes a run as a fixed sequence of steps. A single master process splits the input into M pieces, typically 16 to 64 megabytes each, and assigns map tasks to idle workers. It prefers a worker that already stores a replica of that piece in the Google File System, or failing that one on the same network switch, so that most input is read from local disk. In our job M is 3 and we choose R = 2 reduce tasks.
A map worker parses its split and runs the user’s map function. It buffers the pairs in memory and periodically writes them to its own local disk, divided into R regions by a partitioning function, by default hash(key) mod R. With R = 2, every pair for “the” goes to region 0 or region 1 on every map worker, the same region each time, so all of them will meet at the same reducer. The worker reports the locations of its regions to the master.
When a reduce worker is told where its region lives on each map worker, it fetches the data over the network with remote reads. This is the shuffle, and it is usually the most expensive network step in the job. The reducer sorts what it fetched by key, so all pairs for one word sit together, then calls reduce once per key and appends the output to a file in GFS. A job with R = 2 produces two output files.
One refinement changes the cost noticeably. A combiner is a reduce-like function the library runs on the map worker before writing, when the reduce operation is associative and commutative, as addition is. With a combiner, the first line emits (“the”, 2) once instead of (“the”, 1) twice, and the third line emits (“quick”, 2) and (“a”, 2). Our 23 pairs shrink to 19, one per distinct word per line. That is a small saving on three lines. On real text, where a handful of words make up a large share of all tokens, the combiner removes most of the shuffle traffic. Drag R in the simulation and watch the partitions change while each word still lands in exactly one of them.
The partitioning function is the other refinement worth knowing, because it decides what ends up together. The default hash spreads keys evenly but puts no two related keys in the same output file on purpose. The paper’s example is a job whose keys are URLs, where the user wants all pages from one host in the same output file; partitioning by a hash of the hostname, rather than of the whole URL, does that. The library also guarantees that within a partition, keys reach reduce in sorted order, so each output file is sorted and supports lookups by key without a further pass. A badly chosen partitioning function has the opposite effect: if one key is far more common than the rest, the reducer that owns it does far more work than its peers and becomes a straggler of the job’s own making.
Each line of the text is an input split handed to its own map worker. Change the text, pick a number of reducers, and step through the phases.
(the, 1) (quick, 1) (brown, 1) (fox, 1) (jumps, 1) (over, 1) (the, 1) (lazy, 1) (dog, 1)
(the, 1) (dog, 1) (sleeps, 1) (and, 1) (the, 1) (fox, 1) (runs, 1)
(a, 1) (quick, 1) (fox, 1) (and, 1) (a, 1) (quick, 1) (dog, 1)
quick · runs · quick · quick
the · brown · the · dog · the · dog · the · a · a · dog
fox · jumps · over · lazy · sleeps · and · fox · fox · and
quick: 3 runs: 1
a: 2 brown: 1 dog: 3 the: 4
and: 2 fox: 3 jumps: 1 lazy: 1 over: 1 sleeps: 1
You wrote two functions. map turns a line into (word, 1) pairs; reduce sums the values for one word. Everything else on this screen is the framework.
The paper’s typical production job used M = 200,000 and R = 5,000 on 2,000 machines. Many more tasks than machines is deliberate. A worker that finishes early picks up another small task, which balances load, and when a worker dies its many small tasks spread across the whole cluster instead of landing on one replacement. The cost is on the master, which must make on the order of M + R scheduling decisions and keep state for every map and reduce pair; the paper notes this is about a byte per pair, which sets a practical ceiling on how finely a job can be cut.
Midway through a job, a worker dies. It had completed three map tasks and one reduce task, and was halfway through a fourth map task. Which of those does the master schedule again?
Show the worked answer
All four map tasks, and not the reduce task. The in-progress map obviously has to run again. The three completed maps also have to run again, because their output lived on the local disk of the machine that just died, and reducers that have not yet fetched it can no longer reach it. The completed reduce task is safe: its output was written to GFS, which keeps replicas on other machines.
Why a dead worker costs only re-execution
The master keeps a small record for each task: idle, in progress or completed, and which worker has it. It pings every worker periodically. A worker that stops answering is marked failed, and its tasks go back to idle as described in the answer above. When a map task is re-run on a new worker, every reducer is told; any reducer that has not yet read the old copy reads from the new one.
Re-running work is only safe if a task’s output appears exactly once, even if two copies of the task run. The paper handles this with atomic commits. A map task writes to private temporary files and sends their names to the master in its completion message; the master ignores a completion for a task it has already recorded as done. A reduce task writes to a temporary file and, when finished, renames it to the final name, relying on the file system’s atomic rename. If two copies of the same reduce finish, the renames race and the final file holds the output of exactly one of them.
This gives a strong promise, with a condition attached. If the user’s map and reduce functions are deterministic, the output of a distributed run is the same as a sequential run of the program with no failures. Change one variable to see what the condition is protecting. Suppose our map function samples one line in a thousand at random to build a preview. Map task 2 runs, reducer 0 fetches its output, then the worker dies before reducer 1 fetches. The master re-runs map task 2, which samples different lines, and reducer 1 reads from the new run. Now reducer 0’s output reflects one execution and reducer 1’s another. Each output file is the result of some sequential run, but not the same run. The paper states this weaker guarantee for non-deterministic programs and leaves the decision to the user.
And the master itself? The paper says the implementation simply aborts the job if the master fails, and the client can retry. Checkpointing the master was easy to add and they chose not to, because with one master among thousands of workers its failure was rare. That is a decision written down with its reason, and it held for years.
The last few tasks decide the finish time
A machine that is slow rather than dead causes more trouble than one that crashes. A failing disk that corrects errors, a machine shared with other jobs, or a misconfiguration that disabled processor caches (an example the paper gives) can make one task take many times longer than its peers. The job cannot finish until the slowest task does. The paper calls these stragglers.
The fix is blunt. When a job is close to finishing, the master schedules backup copies of the tasks still in progress, and marks each task done when either copy finishes. The paper says this typically raises the resources used by no more than a few per cent. On its sort benchmark of about a terabyte, the normal run took 891 seconds; with backup tasks disabled the same sort took 1,283 seconds, 44 per cent longer, because a few stragglers held up the end. In another run they killed 200 of the roughly 1,800 workers part way through, and the job finished in 933 seconds, about 5 per cent slower than normal. Those two figures, compared, are the paper’s argument: crashes were cheap to absorb, slowness was not.
The paper’s other benchmark, a grep that scanned ten billion 100-byte records for a rare pattern, finished in about 150 seconds, and the authors point out that about a minute of that was start-up: propagating the program to workers and opening the input files. That overhead is invisible on a long job and dominant on a short one, and it is one reason MapReduce never suited interactive queries.
The same idea later moved from batch tasks to individual requests. Dean and Barroso’s “The Tail at Scale” (2013) applies it as hedged requests: send a second copy of a slow RPC to another replica and use whichever answers first.
What the paper chose not to do
Every choice that made MapReduce simple also has a cost, and the next decade of systems is largely a list of those costs being removed.
| choice | what it bought | what it cost | what later systems changed |
|---|---|---|---|
| map output on local disk | cheap writes, simple recovery by re-running | a dead worker loses finished work | Spark keeps lineage and recomputes only lost partitions |
| job output to replicated GFS | finished reduces never re-run | multi-step pipelines write every stage to disk three times | Spark and Dataflow keep intermediate data in memory between stages |
| exactly two stages | one model to schedule and reason about | real pipelines became chains of jobs glued by hand | FlumeJava, Spark and Dataflow plan a graph of stages |
| sort every reduce input | grouping by key for free, sorted output | sorting work even when grouping by hash would do | engines choose hash or sort aggregation per query |
| one master, abort on its failure | simple, consistent scheduling state | a single point of failure for the job | YARN and later schedulers replicate or restart the coordinator |
Iterative work exposed the costs first. A machine-learning algorithm that makes twenty passes over the same data runs as twenty MapReduce jobs, each reading its input from the file system and writing its output back. Spark, from Matei Zaharia’s group at Berkeley, kept the functional model but held datasets in memory and recorded how each was derived, so a lost partition could be recomputed from its parents rather than reloaded from disk. Inside Google, FlumeJava (2010) let programmers describe a pipeline and compiled it into an optimised sequence of MapReduce jobs, and the Dataflow model (2015) merged batch and streaming. By 2014 Google said publicly that it had largely moved new work off MapReduce.
The database community objected early. In 2008 David DeWitt and Michael Stonebraker published a critique arguing that MapReduce ignored decades of work on schemas, indexes and query optimisation. Much of that critique was later absorbed: Hive and Spark SQL put SQL back on top of the same engines. What survived intact is the execution shape. A GROUP BY in BigQuery, Snowflake, Trino or Spark is still partition, apply, shuffle by key, aggregate; the query plan will show you the exchange step where the shuffle happens. And re-execution of idempotent tasks remains the default way batch systems handle failure.
When is the model the wrong fit? When the computation needs shared mutable state across records, when it is iterative and latency matters, or when results are needed within seconds of the data arriving. The paper does not claim otherwise; it claims that a large class of Google’s jobs fit, and the growth its experience section reports, from nothing to hundreds of separate MapReduce programs in about a year and a half, supports that claim.
How to read the original
Read Section 2 (the programming model) and Section 3 (implementation) in full; they are eight pages and contain nearly everything above. Section 3.3 on fault tolerance is the heart of the paper, including the paragraph on semantics in the presence of failures, which most summaries leave out. Skim Section 4 on refinements, but stop at the combiner, the skipping of bad records (the library can skip input records that deterministically crash user code), and the counters. In Section 5, look at the three data-transfer graphs for the sort benchmark side by side: normal, without backup tasks, and with machines killed. The shape of the tail in each is more instructive than the totals. Section 6 is short and shows how quickly the library spread inside Google, including the rewrite of the production indexing system, where one phase shrank from about 3,800 lines of C++ to about 700.
MapReduce could re-run work because its inputs and outputs lived in a file system that never lost acknowledged data. The next paper, Dynamo, removes that comfort. It is about data that is being written to constantly, by customers, while parts of the system are unreachable.
The original. Read it after this page; the explainer above is a map, not a substitute.
The opening lecture of the best distributed-systems course on the web walks through this exact paper.
Lab 1 has you implement MapReduce yourself; nothing teaches the paper better.