Algorithms · Huge scale

MapReduce

MapReduce: how Google counted the whole web. Split a giant job into tiny ones, run them everywhere, then add up the answers.

The problem

Picture a library with more pages than any person could read in a lifetime, and you need to know how often every word appears. Google faced this kind of job: huge datasets, far too big for one computer.

In 2004, Jeffrey Dean and Sanjay Ghemawat described how Google handled it in "MapReduce: Simplified Data Processing on Large Clusters". Their answer used thousands of machines working together.

The obvious way

Put one computer in charge and have it read every page, keeping a tally of each word. It works on 12 pages. On a giant dataset it breaks twice: the data won't fit, and reading it all one page at a time takes forever. Buying a faster computer only delays the wall. Using many computers helps, but then you must split the work, collect the results, and cope with machines that die mid-job.

The clever idea

Imagine 1,000 students counting words in a pile of books. You hand each student a few pages. Each writes a slip for every word they see: "cat, 1". That's the map step. Then slips are sorted into boxes, one box per word. That's the shuffle. Finally one person per box adds up the slips, which is the reduce step.

The framework does the boring, hard parts: splitting the input, scheduling work onto machines, and retrying work when a machine fails. You only write the map and reduce functions.

How the code does it

Map turns "the cat sat" into three pairs: (the,1), (cat,1), (sat,1). The obvious way runs it on all 12 pages in one place. The clever way gives each of 4 machines 3 pages and has each map only its own chunk.

Then shuffle gathers the 1s into one pile per word and reduce sums each pile. The bar chart shows the win: one machine does 65 steps, while each of the four does at most 17. Both ways give the same counts, and "the" tops the table.

Trade-offs

Splitting work isn't free. The shuffle moves data between machines, and the framework has to schedule and retry work. If your data fits comfortably on one computer, a plain loop is simpler and often faster. MapReduce also fits best when the job can be phrased as map, group by key, reduce.

Where else you’ll see it

Hadoop is a well-known open-source implementation of the same idea. The map-then-combine shape also shows up everywhere in everyday code, such as Python's list comprehensions and sum.

What does the shuffle step do?

  • Adds up the counts for each word
  • Groups all pairs that share the same key
  • Splits the pages across machines
Show the answer

Groups all pairs that share the same key. Shuffle gathers every (word, 1) pair for the same word together. Reduce then does the adding, and the framework does the splitting.

Change it and see
  • Change range(4) and the 3 to 6 machines
  • Add a page with "cat" five times
  • Change [:5] to [:8]

The code

Python, with what each line means beside it. It compares the clever way with one computer reading everything.

pages = [Start the list of twelve tiny web pages
"the cat sat on the mat", "the dog sat on the log",Pages one and two
"the cat saw the dog", "a dog and a cat met",Pages three and four
"the mat is on the log", "a cat sat on a log",Pages five and six
"the dog saw the cat", "the log and the mat",Pages seven and eight
"a cat and a dog sat", "the dog is on the mat",Pages nine and ten
"the cat is on the log", "a dog saw a mat",Pages eleven and twelve
]
def map_page(page):Define map: turn one page into pairs
return [(word, 1) for word in page.split()]Every word becomes a pair with count one
def shuffle(pairs):Define shuffle: gather pairs by word
groups = {}Start with empty groups
for word, one in pairs:Look at each pair one by one
groups.setdefault(word, []).append(one)Drop the 1 into that word's pile
return groupsHand back all the piles
def reduce_word(word, ones):Define reduce: combine one word's pile
return sum(ones)Add up the ones
# Obvious way: one machine maps every pageNote: the obvious single-machine way
pairs = [p for page in pages for p in map_page(page)]Map every page, one after another
one_machine = len(pairs)Remember how many steps that took
# Clever way: 4 machines, 3 pages each, mapped separatelyNote: the clever four-machine way
work, merged = [], []Prepare a work tally and merged pile
for m in range(4):Visit each of four machines
chunk = pages[m * 3:(m + 1) * 3]Give this machine its three pages
out = [p for page in chunk for p in map_page(page)]It maps only its own pages
work.append(len(out))Record how much work it did
merged += outSend its pairs to the shared pile
counts = {w: reduce_word(w, ones) for w, ones in shuffle(merged).items()}Shuffle the pairs, then reduce each word
top = dict(sorted(counts.items(), key=lambda kv: -kv[1])[:5])Keep only the five biggest counts
show(top, "top 5 words")Draw the top words as a table
show([one_machine] + work, "steps: 1 machine vs each of 4")Draw the work as bars
print("same answer:", counts == {w: sum(o) for w, o in shuffle(pairs).items()})Check both ways give the same answer

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on huge scale