Mapreduce Tutorial
Mapreduce Tutorial
on Large Clusters
A Tutorial Based on Google’s Seminal Paper
Google, Inc.
Image
Programming Model
Implementation
Refinements
Performance
Experience at Google
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
The Problem at Google (circa 2004)
Challenges:
Issues:
▶ Processing terabytes of data
▶ Input data is huge
▶ Hundreds of special-purpose
▶ Must distribute across
computations
hundreds/thousands of machines
▶ Examples:
▶ Complex code for:
▶ Crawled documents ▶ Parallelization
▶ Web request logs ▶ Data distribution
▶ Inverted indices ▶ Fault tolerance
▶ Graph structures ▶ Load balancing
▶ Page summaries
Core Problem
Simple computations obscured by large amounts of complex code dealing with
distribution and fault tolerance
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
The MapReduce Solution
Key Insight
Most computations involve:
1. Applying a map operation to each logical record
2. Applying a reduce operation to all values sharing the same key
Inspiration: Map and reduce primitives from functional programming (Lisp, etc.)
Benefits:
What MapReduce Provides:
▶ Hides messy details
▶ Simple programming interface
▶ Programmers focus on logic
▶ Automatic parallelization
▶ No distributed systems expertise
▶ Fault tolerance via re-execution
needed
▶ Load balancing
▶ Scales to thousands of machines
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Major Contributions
2. High-Performance Implementation
▶ Achieves high performance on large clusters of commodity PCs
▶ Processes many terabytes on thousands of machines
3. Proven at Scale
▶ Hundreds of MapReduce programs implemented at Google
▶ Thousands of jobs executed daily on Google’s clusters
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
MapReduce Programming Model
Core Concept
Computation takes a set of input key/value pairs and produces a set of output
key/value pairs
2. Reduce Function
▶ Accepts an intermediate key and a set of values for that key
▶ Merges values to form a possibly smaller set
▶ Typically produces zero or one output value per invocation
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Type Signatures
Conceptual Types
Even though implementations may use strings, conceptually:
Map: (k1 , v1 ) → list(k2 , v2 )
Important Notes:
▶ Input keys/values (k1 , v1 ) are from a different domain than output keys/values
▶ Intermediate keys/values (k2 , v2 ) are from the same domain as output keys/values
▶ Values supplied to Reduce via an iterator (handles large lists)
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Classic Example: Word Count
Problem: Count occurrences of each word in a large collection of documents
Output Files
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
More Examples
Distributed Grep
Map: Emit line if it matches pattern
Reduce: Identity function (copies data to output)
Inverted Index
Map: Parse document, emit ⟨word, document ID⟩
Reduce: Sort document IDs, emit ⟨word, list(document ID)⟩
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Google’s Computing Environment
Key Insight
Network bandwidth is a scarce resource → Optimize for locality!
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Execution Overview
User Program
(1) fork
Master
(2) assign
Output
Input files
(2) assign
worker output file 1
split 0 worker worker
split 1
(6) write
(3) read (4) local write Intermediate
(5) remote read worker output file 0
worker
split 2
split 3
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Execution Steps (1/2)
Step 1: Split Input
▶ MapReduce library splits input files into M pieces (typically 16-64 MB)
▶ Starts up many copies of the program on cluster machines
Location Propagation:
▶ Master is conduit for intermediate file regions
▶ For each completed map task, stores:
▶ Locations of R intermediate file regions
▶ Sizes of intermediate file regions
▶ Information pushed incrementally to workers with in-progress reduce tasks
Memory Overhead
O(M + R) scheduling decisions and O(M × R) state in memory
(Constant factors are small: ≈ 1 byte per map/reduce task pair)
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Fault Tolerance: Worker Failure
Detection:
▶ Master pings workers periodically
▶ No response in certain time → worker marked as failed
Recovery:
▶ Completed map tasks reset to idle state
▶ Map/reduce tasks in progress reset to idle
▶ Eligible for rescheduling on other workers
Resilience Example
Network maintenance caused 80 machines to become unreachable.
MapReduce simply re-executed work and continued to completion! . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Fault Tolerance: Master Failure & Semantics
Master Failure:
▶ Master writes periodic checkpoints
▶ New copy can be started from last checkpoint
▶ Current implementation: abort if master fails (clients can retry)
▶ Single master makes failure unlikely
Non-Deterministic Functions
Weaker but reasonable semantics:
▶ Output equivalent to some sequential execution . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Locality Optimization
Key Observation
Network bandwidth is a relatively scarce resource
Strategy:
▶ Input data stored on local disks via GFS (Google File System)
▶ GFS divides files into 64 MB blocks
▶ Stores 3 copies of each block on different machines
▶ Master takes location information into account
Scheduling Policy:
1. Best: Schedule map task on machine containing replica of input data
2. Next best: Schedule near a replica (e.g., same network switch)
Result
When running large MapReduce operations on significant fraction of workers:
Most input data read locally → consumes no network bandwidth . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Task Granularity
Subdividing:
▶ Map phase: M pieces
▶ Reduce phase: R pieces
Benefits:
▶ Improves dynamic load balancing
▶ Speeds up recovery when worker fails
▶ Completed tasks spread across all other workers
Practical Bounds:
▶ Master makes O(M + R) scheduling decisions
▶ Keeps O(M × R) state in memory
Causes of Stragglers:
▶ Bad disk (30 MB/s → 1 MB/s due to correctable errors)
▶ Cluster scheduler scheduled other tasks (competition for resources)
▶ Bug in machine initialization (e.g., processor caches disabled)
Impact
Sort program: 44% longer to complete when backup tasks disabled! . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Refinement 1: Partitioning Function
Default: Hash-based partitioning
▶ hash(key) mod R
▶ Results in fairly well-balanced partitions
Custom Partitioning:
▶ Sometimes useful to partition by other functions
▶ Example: Output keys are URLs
▶ Want all URLs from same host in same output file
▶ Solution: hash(Hostname(urlkey)) mod R
Benefits:
▶ Better data organization
▶ Enables efficient downstream processing
▶ Supports application-specific requirements
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Refinement 2: Ordering Guarantees
Guarantee
Within a given partition, intermediate key/value pairs processed in increasing key
order
Benefits:
▶ Easy to generate sorted output file per partition
▶ Useful when output format needs efficient random access by key
▶ Convenient for users who want sorted data
Use Cases:
▶ Building searchable indices
▶ Database-like access patterns
▶ Sorted data analysis
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Refinement 3: Combiner Function
Problem
Significant repetition in intermediate keys produced by map tasks
Example: Word counting - many ⟨the, 1⟩ records
Impact
Partial combining significantly speeds up certain classes of MapReduce operations
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Other Refinements
4. Input and Output Types
▶ Support for reading data in several formats (text, binary, database, etc.)
▶ Users can add custom input/output types via reader interface
6. Local Execution
▶ Alternative implementation for debugging
▶ Executes all work sequentially on local machine
▶ Facilitates debugging, profiling, small-scale testing
Network:
▶ Two-level tree-shaped switched network
▶ ≈ 100-200 Gbps aggregate bandwidth at root
▶ Round-trip time between any pair: < 1 millisecond
Conditions:
▶ Weekend afternoon execution
▶ CPUs, disks, network mostly idle
▶ 1-1.5GB reserved for other tasks .
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . .
. . . .
.
.
.
.
.
.
.
Benchmark 1: Grep
Task: Scan through 1010 100-byte records, search for rare three-character pattern
Configuration:
▶ Input: 1 TB, split into ≈ 64MB pieces (M = 15, 000)
▶ Output: One file (R = 1)
▶ Pattern occurs in 92,337 records
Results:
▶ Peak input rate: 30+ GB/s (1764 workers)
▶ Computation time: 150 seconds (including ≈ 60s startup overhead)
▶ Startup overhead due to:
▶ Program propagation to all workers
▶ GFS interactions to open 1000 input files
▶ Locality optimization information gathering
Key Insight
Input rate gradually increases as more workers assigned, peaks, then drops as tasks
complete . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Benchmark 2: Sort (1/2)
Task: Sort 1010 100-byte records (≈ 1 TB of data)
Configuration:
▶ Modeled after TeraSort benchmark
▶ Input split into 64MB pieces (M = 15, 000)
▶ Output: 4000 files (R = 4, 000)
▶ Output: 2 TB (2-way replicated GFS files)
Implementation:
▶ Map: Extract 10-byte sorting key, emit ⟨key, line⟩
▶ Reduce: Identity function (passes pairs unchanged)
▶ Less than 50 lines of user code!
Partitioning:
▶ Built-in knowledge of key distribution
▶ General sorting: pre-pass to sample keys and compute split-points
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Benchmark 2: Sort (2/2)
Normal Execution Results:
▶ Total time: 891 seconds
▶ Input rate peaks: ≈ 13 GB/s
▶ Shuffle starts as first map completes
▶ Shuffling done: ≈ 600 seconds
▶ Write rate: 2-4 GB/s
▶ All writes finish: ≈ 850 seconds
Observations:
▶ Input rate > shuffle rate > output rate
▶ Locality optimization: most data read locally
▶ Shuffle rate > output rate: 2 replicas written
▶ Comparable to TeraSort benchmark best result (1057 seconds)
What Happened:
▶ Underlying cluster scheduler immediately restarted processes
▶ Deaths show up as negative input rate
▶ Previously completed map work disappeared
▶ Re-execution happened relatively quickly
Results:
▶ Total time: 933 seconds
▶ Increase: Only 5% over normal execution (891s)
Conclusion
MapReduce is highly resilient to machine failures!
System gracefully handles worker deaths without significant performance degradation.
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
MapReduce at Google
Timeline:
▶ First version: February 2003
▶ Significant enhancements: August 2003
▶ Locality optimization, dynamic load balancing, etc.
Growth:
▶ 0 instances in early 2003
▶ Almost 900 separate instances by September 2004
▶ Used across wide range of domains
Applications:
Metric Value
Number of jobs 29,423
Average job completion time 634 secs
Machine days used 79,186 days
Input data read 3,288 TB
Intermediate data produced 758 TB
Output data written 193 TB
Average worker machines per job 157
Average worker deaths per job 1.2
Average map tasks per job 3,351
Average reduce tasks per job 55
Unique map implementations 395
Unique reduce implementations 269
Unique map/reduce combinations 426
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Large-Scale Indexing
Most Significant Use: Complete rewrite of production indexing system
2. Widely Applicable
▶ Large variety of problems expressible as MapReduce
▶ Used for web search, sorting, data mining, machine learning, and more
3. Scalable Implementation
▶ Scales to thousands of machines
▶ Efficient use of machine resources
▶ Suitable for Google’s large computational problems
Development Impact
Greatly speeds up development and prototyping cycle
Allows programmers to exploit large amounts of resources easily . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Key Lessons Learned
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Impact and Legacy
Historical Impact
This paper (OSDI 2004) became one of the most influential papers in distributed
systems and big data processing
Paradigm Shift:
▶ Democratized large-scale data processing
▶ Made distributed computing accessible to non-experts
▶ Enabled the ”Big Data” revolution
▶ Foundation for modern data engineering practices . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Summary
MapReduce in a Nutshell
A programming model and implementation for processing large datasets on clusters of
commodity machines
Key Characteristics:
▶ Simple programming interface (Map and Reduce functions)
▶ Automatic parallelization and distribution
▶ Fault tolerance via re-execution
▶ Locality optimization for performance
▶ Scalable to thousands of machines
Why It Matters:
▶ Hides complexity of distributed systems
▶ Allows programmers to focus on logic, not infrastructure
▶ Proven at massive scale at Google
▶ Influenced entire generation of big data systems . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
Questions to Consider
3. What are the trade-offs between simplicity and flexibility in the MapReduce
model?
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .