Databases / MapReduce

MapReduce

Count the words in a file too big for one computer. Eight words, three machines and three stages show how map, shuffle and reduce divide the work and survive a crash.

Intermediate7 steps~7 min
Pair — a key and a value, coloured by its key
Map worker — turns one split into pairs
Reduce worker — combines all values of a key
Coordinator — hands out tasks, watches for failures
// THE SHORT VERSION

In 30 seconds

MapReduce is a recipe for processing huge data on many ordinary machines. You write two small functions; the framework does the rest.

Map runs on every chunk of the input at once and emits key–value pairs. The shuffle brings all pairs with the same key together. Reduce combines each key’s values into a result.

Because tasks are independent, they run in parallel, and a task on a failed machine is simply run again elsewhere. Newer engines such as Spark kept these ideas but avoid writing to disk between every step.

MapReduce, step by step

STEPS01 / 07

Timings are invented to show the effect; real speed-ups are smaller because the shuffle and start-up take time. Spark also uses disk during shuffles and when data outgrows memory.

STEP 01 / 07 Too big for one machine
THE MATH
input: 8 words in 3 splits goal: count each word
  1. Step 1: Too big for one machine. The job: count how often each word appears in a file far too large for one computer. The file is already cut into splits stored on different machines. Our toy version has 8 words in 3 splits; picture billions of words on thousands of machines.
  2. Step 2: Map: one split at a time. You write a small map function. A copy runs on each split, ideally on the machine that already holds it, and all three run at the same time. Map does no counting: for every word it sees, it emits a pair (word, 1).
  3. Step 3: Shuffle: group by key. The framework, not your code, now moves the pairs. It sorts them by key and sends every pair with the same key to the same reduce worker. All four cat pairs end up together, whichever split they came from.
  4. Step 4: Reduce: one key at a time. You also write the reduce function. It receives one key with all of its values and boils them down to a result; here it adds up the ones. Two reduce workers run side by side, and their outputs together are the answer: bird 2, cat 4, dog 2.
  5. Step 5: Same code, more machines. Because map tasks never talk to each other, they can run on separate machines at once, and so can reduce tasks. Suppose a map task takes 2 minutes and a reduce task 1. One machine needs 8 minutes; three need 3. A coordinator hands out the tasks, and your two functions stay the same.
  6. Step 6: A worker dies: rerun its task. With thousands of machines, some fail during every large job. The coordinator pings each worker; when one goes quiet, it gives that worker’s task to another machine. The split is still stored, in several copies, and map only reads it, so the rerun produces exactly the same pairs.
  7. Step 7: Why newer engines took over. Real analyses need many steps, and each MapReduce job writes its result to disk for the next job to read back. Engines such as Apache Spark plan the whole chain at once, keep intermediate data in memory when it fits, and offer joins and SQL directly. They kept the ideas: split, shuffle, rerun what fails.
VALUES SO FAR
input8 wordsinput
splits3input
pairs····map
groups····shuffle
bird····result
cat····result
dog····result
time····parallel
on crash····rule

Spotted a mistake on this page?