Paper Map Reduce
Paper Map Reduce
Abstract
MapReduce is a programming model and an associated implementation
for processing and generating large data sets. Users specify a map
function that processes a key/value pair to generate a set of intermediate
key/value pairs, and a reduce function that merges all intermediate
values associated with the same intermediate key. Many real-world tasks
are expressible in this model, as shown in the paper.
Programs written in this functional style are automatically parallelized and
executed on a large cluster of commodity machines. The run-time
system takes care of the details of partitioning the input data, scheduling
the program's execution across a set of machines, handling machine
failures, and managing the required inter-machine communication. This
allows programmers without any experience with parallel and distributed
systems to easily utilize the resources of a large distributed system.
Our implementation of MapReduce runs on a large cluster of commodity
machines and is highly scalable: a typical MapReduce computation
processes many terabytes of data on thousands of machines.
Programmers find the system easy to use: hundreds of MapReduce
programs have been implemented, and upwards of one thousand
MapReduce jobs are executed on Google's clusters every day.
1 Introduction
Over the past five years, the authors and many others at Google have
implemented hundreds of special-purpose computations that process
large amounts of raw data, such as crawled documents, web request logs,
etc., to compute various kinds of derived data, such as inverted indices,
various representations of the graph structure of web documents,
summaries of the number of pages crawled per host, the set of most
frequent queries in a given day, etc. Most such computations are
conceptually straightforward. However, the input data is usually large,
and the computations have to be distributed across hundreds or
thousands of machines to finish in a reasonable amount of time. The
issues of how to parallelize the computation, distribute the data, and
handle failures conspire to obscure the original simple computation with
large amounts of complex code to deal with these issues.
As a reaction to this complexity, we designed a new abstraction that
allows us to express the simple computations we were trying to perform
but hides the messy details of parallelization, fault-tolerance, data
distribution, and load balancing in a library. Our abstraction is inspired
by the map and reduce primitives present in Lisp and many other
functional languages. We realized that most of our computations involved
applying a map operation to each logical "record" in our input to compute
a set of intermediate key/value pairs and then applying a reduce
operation to all the values that shared the same key to combine the
derived data appropriately. Our use of a functional model with user-
specified map and reduce operations allows us to parallelize large
computations easily and to use re-execution as the primary mechanism
for fault tolerance.
The major contributions of this work are a simple and powerful
interface that enables automatic parallelization and distribution of large-
scale computations, combined with an implementation of this interface
that achieves high performance on large clusters of commodity Pcs.
2 Programming Model
The computation takes a set of input key/value pairs and produces a set
of output key/value pairs. The user of the MapReduce library expresses
the computation as two functions: Map and Reduce.
• Map, written by the user, takes an input pair and produces a set of
intermediate key/value pairs. The MapReduce library groups
together all intermediate values associated with the same
intermediate key I and passes them to the Reduce function.
• Reduce, also written by the user, accepts an intermediate key I and
a set of values for that key. It merges together these values to form a
possibly smaller set of values. Typically, just zero or one output value
is produced per Reduce invocation. The intermediate values are
supplied to the user's reduce function via an iterator. This allows us
to handle lists of values that are too large to fit in memory.
Example
Consider the problem of counting the number of occurrences of each word
in a large collection of documents. The user would write code similar to
the following pseudo-code:
Types
Even though the previous pseudo-code is written in terms of string inputs
and outputs, conceptually the map and reduce functions supplied by the
user have associated types:
map (k1,v1) → list(k2,v2)
reduce (k2, list(v2)) → list(v2)
i.e., the input keys and values are drawn from a different domain than the
output keys and values. Furthermore, the intermediate keys and values
are from the same domain as the output keys and values.
Our C++ implementation passes strings to and from the user-defined
functions and leaves it to the user code to convert between strings and
appropriate types.
More Examples
Here are a few simple examples of interesting programs that can be easily
expressed as MapReduce computations.
1. Distributed Grep: The map function emits a line if it matches a
supplied pattern. The reduce function is an identity function that
just copies the supplied intermediate data to the output.
2. Count of URL Access Frequency: The map function processes
logs of web page requests and outputs ⟨URL, 1⟩. The reduce
function adds together all values for the same URL and emits a ⟨URL,
total count⟩ pair.
3. Reverse Web-Link Graph: The map function outputs ⟨target,
source⟩ pairs for each link to a target URL found in a page named
source. The reduce function concatenates the list of all source URLs
associated with a given target URL and emits the pair: ⟨target,
list(source)⟩.
4. Term-Vector per Host: A term vector summarizes the most
important words that occur in a document or a set of documents as a
list of ⟨word, frequency⟩ pairs. The map function emits a ⟨hostname,
term vector⟩ pair for each input document (where the hostname is
extracted from the URL of the document). The reduce function is
passed all per-document term vectors for a given host. It adds these
term vectors together, throwing away infrequent terms, and then
emits a final ⟨hostname, term vector⟩ pair.
5. Inverted Index: The map function parses each document and
emits a sequence of ⟨word, document ID⟩ pairs. The reduce function
accepts all pairs for a given word, sorts the corresponding document
IDs, and emits a ⟨word, list(document ID)⟩ pair. The set of all output
pairs forms a simple inverted index. It is easy to augment this
computation to keep track of word positions.
6. Distributed Sort: The map function extracts the key from each
record and emits a ⟨key, record⟩ pair. The reduce function emits all
pairs unchanged. This computation depends on the partitioning
facilities described in Section 4.1 and the ordering properties
described in Section 4.2.
3 Implementation
Many different implementations of the MapReduce interface are
possible. The right choice depends on the environment. For example, one
implementation may be suitable for a small shared-memory machine,
another for a large NUMA multi-processor, and yet another for an even
larger collection of networked machines.
This section describes an implementation targeted to the computing
environment in wide use at Google: large clusters of commodity PCs
connected together with switched Ethernet [4]. In our environment:
1.Machines are typically dual-processor x86 processors running Linux,
with 2-4 GB of memory per machine.
2.Commodity networking hardware is used—typically either 100
megabits/second or 1 gigabit/second at the machine level, but
averaging considerably less in overall bisection bandwidth.
3.A cluster consists of hundreds or thousands of machines, and
therefore machine failures are common.
4.Storage is provided by inexpensive IDE disks attached directly to
individual machines. A distributed file system [8] developed in-house
is used to manage the data stored on these disks. The file system
uses replication to provide availability and reliability on top of
unreliable hardware.
5.Users submit jobs to a scheduling system. Each job consists of a set
of tasks and is mapped by the scheduler to a set of available
machines within a cluster.
Execution Overview
The Map invocations are distributed across multiple machines by
automatically partitioning the input data into a set of M splits. The input
splits can be processed in parallel by different machines. Reduce
invocations are distributed by partitioning the intermediate key space into
R pieces using a partitioning function (e.g., hash(key) mod R). The
number of partitions (R) and the partitioning function are specified by the
user.
Figure 1 shows the overall flow of a MapReduce operation in our
implementation. When the user program calls the MapReduce function,
the following sequence of actions occurs (the numbered labels in Figure 1
correspond to the numbers in the list below):
1. The MapReduce library in the user program first splits the input
files into M pieces of typically 16 megabytes to 64 megabytes (MB)
per piece (controllable by the user via an optional parameter). It then
starts up many copies of the program on a cluster of machines.
2. One of the copies of the program is special—the master. The rest
are workers that are assigned work by the master. There are M map
tasks and R reduce tasks to assign. The master picks idle workers
and assigns each one a map task or a reduce task.
3. A worker who is assigned a map task reads the contents of the
corresponding input split. It parses key/value pairs out of the input
data and passes each pair to the user-defined Map function. The
intermediate key/value pairs produced by the Map function are
buffered in memory.
4. Periodically, the buffered pairs are written to local disk, partitioned
into R regions by the partitioning function. The locations of these
buffered pairs on the local disk are passed back to the master, who is
responsible for forwarding these locations to the reduce workers.
5.When a reduce worker is notified by the master about these
locations, it uses remote procedure calls to read the buffered data
from the local disks of the map workers. When a reduce worker has
read all intermediate data, it sorts it by the intermediate keys so that
all occurrences of the same key are grouped together. The sorting is
needed because typically many different keys map to the same
reduce task. If the amount of intermediate data is too large to fit in
memory, an external sort is used.
6. The reduce worker iterates over the sorted intermediate data and for
each unique intermediate key encountered, it passes the key and the
corresponding set of intermediate values to the user's Reduce
function. The output of the Reduce function is appended to a final
output file for this reduce partition.
7. When all map tasks and reduce tasks have been completed, the
master wakes up the user program. At this point, the MapReduce
call in the user program returns back to the user code.
After successful completion, the output of the MapReduce execution is
available in the R output files (one per reduce task, with file names as
specified by the user). Typically, users do not need to combine these R
output files into one file—they often pass these files as input to another
MapReduce call or use them from another distributed application that is
able to deal with input that is partitioned into multiple files.
Fault Tolerance
Since the MapReduce library is designed to help process very large
amounts of data using hundreds or thousands of machines, the library
must tolerate machine failures gracefully.
Worker Failure
The master pings every worker periodically. If no response is received
from a worker in a certain amount of time, the master marks the worker
as failed. Any map tasks completed by the worker are reset back to their
initial idle state and therefore become eligible for scheduling on other
workers. Similarly, any map task or reduce task in progress on a failed
worker is also reset to idle and becomes eligible for rescheduling.
Completed map tasks are re-executed on a failure because their output is
stored on the local disk(s) of the failed machine and is therefore
inaccessible. Completed reduce tasks do not need to be re-executed since
their output is stored in a global file system.
When a map task is executed first by worker A and then later executed by
worker B (because A failed), all workers executing reduce tasks are
notified of the re-execution. Any reduce task that has not already read the
data from worker A will read the data from worker B.
MapReduce is resilient to large-scale worker failures. For example,
during one MapReduce operation, network maintenance on a running
cluster was causing groups of 80 machines at a time to become
unreachable for several minutes. The MapReduce master simply re-
executed the work done by the unreachable worker machines and
continued to make forward progress, eventually completing the
MapReduce operation.
Master Failure
It is easy to make the master write periodic checkpoints of the master
data structures described above. If the master task dies, a new copy can
be started from the last checkpointed state. However, given that there is
only a single master, its failure is unlikely; therefore, our current
implementation aborts the MapReduce computation if the master fails.
Clients can check for this condition and retry the MapReduce operation if
they desire.
Locality
Network bandwidth is a relatively scarce resource in our computing
environment. We conserve network bandwidth by taking advantage of the
fact that the input data (managed by GFS [8]) is stored on the local disks
of the machines that make up our cluster. GFS divides each file into 64
MB blocks and stores several copies of each block (typically 3 copies) on
different machines. The MapReduce master takes the location
information of the input files into account and attempts to schedule a map
task on a machine that contains a replica of the corresponding input data.
Failing that, it attempts to schedule a map task near a replica of that
task's input data (e.g., on a worker machine that is on the same network
switch as the machine containing the data). When running large
MapReduce operations on a significant fraction of the workers in a
cluster, most input data is read locally and consumes no network
bandwidth.
Task Granularity
We subdivide the map phase into M pieces and the reduce phase into R
pieces, as described above. Ideally, M and R should be much larger than
the number of worker machines. Having each worker perform many
different tasks improves dynamic load balancing and also speeds up
recovery when a worker fails: the many map tasks it has completed can
be spread out across all the other worker machines.
There are practical bounds on how large M and R can be in our
implementation, since the master must make O(M+R) scheduling
decisions and keeps O(M*R) state in memory as described above. (The
constant factors for memory usage are small, however: the O(M*R) piece
of the state consists of approximately one byte of data per map
task/reduce task pair.)
Furthermore, R is often constrained by users because the output of each
reduce task ends up in a separate output file. In practice, we tend to
choose M so that each individual task is roughly
16 MB to 64 MB of input data (so that the locality optimization
described above is most effective), and we make R a small multiple of the
number of worker machines we expect to use. We often perform
MapReduce computations with M = 200,000 and R = 5,000, using
2,000 worker machines.
Backup Tasks
One of the common causes that lengthens the total time taken for a
MapReduce operation is a "straggler": a machine that takes an
unusually long time to complete one of the last few map or reduce tasks
in the computation. Stragglers can arise for a whole host of reasons. For
example, a machine with a bad disk may experience frequent correctable
errors that slow its read performance from 30 MB/s to 1 MB/s. The cluster
scheduling system may have scheduled other tasks on the machine,
causing it to execute the MapReduce code more slowly due to
competition for CPU, memory, local disk, or network bandwidth. A recent
problem we experienced was a bug in machine initialization code that
caused processor caches to be disabled: computations on affected
machines slowed down by over a factor of one hundred.
We have a general mechanism to alleviate the problem of stragglers.
When a MapReduce operation is close to completion, the master
schedules backup executions of the remaining in-progress tasks. The
task is marked as completed whenever either the primary or the backup
execution completes. We have tuned this mechanism so that it typically
increases the computational resources used by the operation by no more
than a few percent. We have found that this significantly reduces the time
to complete large MapReduce operations. As an example, the sort
program described in Section 5.3 takes 44% longer to complete when the
backup task mechanism is disabled.
4 Refinements
Although the basic functionality provided by simply writing Map and
Reduce functions is sufficient for most needs, we have found a few
extensions useful. These are described in this section.
Partitioning Function
The users of MapReduce specify the number of reduce tasks/output files
that they desire (R). Data gets partitioned across these tasks using a
partitioning function on the intermediate key. A default partitioning
function is provided that uses hashing (e.g., hash(key) mod R). This
tends to result in fairly well-balanced partitions. In some cases, however,
it is useful to partition data by some other function of the key. For
example, sometimes the output keys are URLs, and we want all entries for
a single host to end up in the same output file. To support situations like
this, the user of the MapReduce library can provide a special partitioning
function. For example, using hash(Hostname(urlkey)) mod R as the
partitioning function causes all URLs from the same host to end up in the
same output file.
Ordering Guarantees
We guarantee that within a given partition, the intermediate key/value
pairs are processed in increasing key order. This ordering guarantee
makes it easy to generate a sorted output file per partition, which is
useful when the output file format needs to support efficient random
access lookups by key, or users of the output find it convenient to have
the data sorted.
Combiner Function
In some cases, there is significant repetition in the intermediate keys
produced by each map task, and the user-specified Reduce function is
commutative and associative. A good example of this is the word-
counting example in Section 2.1. Since word frequencies tend to follow a
Zipf distribution, each map task will produce hundreds or thousands of
records of the form ⟨the, 1⟩. All of these counts will be sent over the
network to a single reduce task and then added together by the Reduce
function to produce one number. We allow the user to specify an optional
Combiner function that does partial merging of this data before it is sent
over the network.
The Combiner function is executed on each machine that performs a
map task. Typically, the same code is used to implement both the
Combiner and the Reduce functions. The only difference between a
Reduce function and a Combiner function is how the MapReduce
library handles the output of the function. The output of a Reduce
function is written to the final output file. The output of a Combiner
function is written to an intermediate file that will be sent to a reduce
task.
Partial combining significantly speeds up certain classes of MapReduce
operations. Appendix A contains an example that uses a Combiner.
Side-effects
In some cases, users of MapReduce have found it convenient to produce
auxiliary files as additional outputs from their map and/or reduce
operators. We rely on the application writer to make such side-effects
atomic and idempotent. Typically, the application writes to a temporary
file and atomically renames this file once it has been fully generated.
We do not provide support for atomic two-phase commits of multiple
output files produced by a single task. Therefore, tasks that produce
multiple output files with cross-file consistency requirements should be
deterministic. This restriction has never been an issue in practice.
Local Execution
Debugging problems in Map or Reduce functions can be tricky since the
actual computation happens in a distributed system, often on several
thousand machines, with work assignment decisions made dynamically by
the master. To help facilitate debugging, profiling, and small-scale testing,
we have developed an alternative implementation of the MapReduce
library that sequentially executes all of the work for a MapReduce
operation on the local machine. Controls are provided to the user so that
the computation can be limited to particular map tasks. Users invoke their
program with a special flag and can then easily use any debugging or
testing tools they find useful (e.g., gdb).
Status Information
The master runs an internal HTTP server and exports a set of status
pages for human consumption. The status pages show the progress of the
computation, such as how many tasks have been completed, how many
are in progress, bytes of input, bytes of intermediate data, bytes of
output, processing rates, etc. The pages also contain links to the standard
error and standard output files generated by each task. The user can use
this data to predict how long the computation will take and whether or not
more resources should be added to the computation. These pages can
also be used to figure out when the computation is much slower than
expected.
In addition, the top-level status page shows which workers have failed
and which map and reduce tasks they were processing when they failed.
This information is useful when attempting to diagnose bugs in the user
code.
Counters
The MapReduce library provides a counter facility to count
occurrences of various events. For example, user code may want to count
the total number of words processed or the number of German
documents indexed, etc.
To use this facility, user code creates a named counter object and then
increments the counter appropriately in the Map and/or Reduce function.
For example:
Counter* uppercase;
uppercase = GetCounter("uppercase");