MapReduce 00:00:01 Count words across three pieces of a book Three machines read separate passages. We need a count for every word in the whole book, although no mapper reads the whole book. The trick is to make the word the key carried between machines. Three lines are three splits, so that they can be read in parallel. Each map task emits a pair for each word: seven pairs in all. None of the three input splits contains a repeated word here, so a local combiner saves nothing. A split containing many repeats can emit a smaller set of partial counts. Suppose data, hello and mapreduce belong to reduce task 1, and of and world to reduce task 2. Task 1: data 1, hello 2, mapreduce 1. Task 2: of 1, world 2. Each reduce task writes one file to replicated storage. Together they hold five words and seven occurrences. Each reducer produces part of the answer, and the result is the collection of those output files. A word's contributions must reach the same logical reduction. 00:01:07 Where do the equal keys meet? Mappers finish on different machines with their own runs of key-value pairs. A reducer cannot add the counts for a word until every relevant partition of those mapper outputs reaches it. Map 1 emitted a, b, a. Map 2 emitted b, c, a. Each map task sorts its own output on its own disk. Suppose a and c belong to reduce 1, and b to reduce 2. Each fetches its part from both maps. After merging sorted runs, equal keys sit together within their assigned reducer. In this example that gives three contributions for A, one for C and two for B. The shuffle is physical data movement. Sorting makes equal keys adjacent within a reducer's input. Neither operation means every byte in the job must first pass through one central machine. 00:01:59 The wrong way to average an average Try combining values two and four on one worker, and nine on another. The two local groups are different sizes. Would averaging their two averages give the average of all three observations? Map 1 has values 2 and 4. Map 2 has 9. The true average is 15 divided by 3 equals 5. Map 1 sends 3 and map 2 sends 9. Reduce averages these and gets 6. Map 1 sends (6, 2). Map 2 sends (9, 1). Reduce adds to (15, 3) and divides: 5. Carry a sum and count together, combine each component, then divide once. The combiner is an optional optimization, so correctness has to survive it running zero, one or several times. 00:02:47 A finished mapper disappears In this execution model the workers' local disks hold intermediate map output. A worker can disappear after reporting success but before a reducer has fetched its partition. The input splits are still available elsewhere. All three map tasks have finished. Their output is on their own disks. The machine of map task 2 is lost before reduce fetched from it. Split 2 has other copies, so a free machine runs map task 2 again. Reduce task 2 is at 10% while task 1 is finished. A bad disk makes it slow. A second copy starts elsewhere and finishes first. The slow one is stopped. Re-execution or speculative copies can repeat a task. Keep external side effects outside the task or make them safe to repeat. A second copy should never mean a second real-world payment. 00:03:40 One popular word holds up everybody A reducer assigned a very common word gets far more records than its peers. Adding reducers does little for that one key if every occurrence must land at the same reducer in the same stage. Reducer 1 gets the key the with 900 records. The others get 50 each. With six reducers the key the is still on one. Nothing is faster. Make three composite keys. Each keeps the original word and attaches salt zero, salt one or salt two. About three hundred records then land on each of three reducers. A small second job removes the salt and adds three partial counts to 900. Split the hot key into partial groups, count them, then combine their totals. The same principle solved the book and its busiest word. Move compact partial answers where the mathematics permits.