MapReduce: Simplified Data Processing on Large Clusters
Shoeb Ilyas - October 1, 2026 - 1901 Words
In the world of software engineering Google, Amazon and other software platforms often need to process huge amounts of datasets. These input data sets can vary from few KBs to multi-TBs. Processing these datasets is usually a straightforward computation but we have limited time and a lot of processing to do considering the size of these datasets. The problem we encounter is that how do we ensure fast and efficient processing of such huge datasets within a given time slot.
The MapReduce is a programming model introduced by Google in 2003 and it deals with the exact problem of processing and generating huge multi-TB datasets. This programming model allows us to parallelize our data processing over hundreds or thousands of machines to perform computations within a reasonable amount of time.
In the next section, we will look into the fundamentals of this programming model. In Section 3, we will go through the execution flow covering as many edge cases as possible to give the reader a solid mental model after which they can delve directly either into reading this research paper themselves or writing a custom implementation themselves. Then in the following sections we discuss more about partioning function, combiner function, ordering guarantee and other aspects of this programming model. Let's dive right in!
Programming Model
The MapReduce programming model introduced by Google, hides all the complexities of partitioning our data, handling machine failures, inter-machine communications etc, from the user. The user only needs to provide the map and the reduce functions and the input data. The rest is handled by MapReduce itself. While the fundamental idea remains the same there can be various implementations of MapReduce depending on the user's requirements. Let's quickly go through what Map and Reduce are:
- Map: is a user-defined function that takes in a set of input key/value pairs and generates a set of intermediate key/value pairs. The intermediate key/value pairs are the grouped by values belonging to similar keys and then this grouped data is passed to the reduce function.
- Reduce is a user-defined function that takes in the intermediate key/value pairs and aggregates the values belonging to the same key and generates a final set of output key/value pairs. There is usually zero to one key/value pair generated per intermediate key per reduce invocation. The reduce function receives the intermediate values as an iterator to optimize for huge datasets.
The official paper explains map reduce with the following pseudo-code:
map(String key, String value):
// key: document name
// value: document contents
for each word w in value:
EmitIntermediate(w, "1");
reduce(String key, Iterator values):
// key: a word
// values: a list of counts
int result = 0;
for each v in values:
result += ParseInt(v);
Emit(AsString(result));Well, that's it! That's all you need to know about the basic MapReduce programming model. Let's go through an execution flow while covering as many edge cases as possible to form a solid mental model of the novel implementation of MapReduce that Google introduces us to.
Execution Flow
Let's assume you are implementing MapReduce to compute the access frequency of each URL in your website. In summary, the MapReduce will go through each log in your input log data and generate a set of intermediate key/value pair - ("URL", 1). These intermediate key/value pairs will then be passed on to the reduceer function that will generate a final set of key/value pairs in the format - ("URL", Total_Access_Count) written to an output file. This was a very filtered down explanation of how MapReduce will compute the access frequency of each URL in your website.
Now, let's deep dive into the execution flow and understand in-detail how exactly a MapReduce implementation will work. This is quite a generic implementation and can be easily adapted to your own specific requirements. If at all you get confused about the execution flow, try to run this same flow by using the example in the paragraph above:
We have M map workers. R reduce workers. The number of reduce workers define the number of output files. Therefore, we have R output files.
- The input data is partitioned into M splits.
- The MapReduce spawns multiple copies of the program across the cluster of machines.
- One of the programs assigned to be the master. The Master takes on the responsibility of orchestrating the entire MapReduce operation across M map workers and R reduce workers.
- The Master picks up the idle workers and assigns each one of them either a map task or a reduce task.
- The map worker consumes it's assigned split and consumes the input data. It generates a set of intermediate key/value pairs buffered in memory.
- Periodically, this set of input/value pair is flushed to the disk into one of the R partition regions. Read carefully now. We have R intermediate files per map worker. For each intermediate key/value pair, we compute the has for the key and assign it to one of R regions - R0, R1, R2...RR. The hash function is or must be deterministic so that all the values for a key land in the same R region otherwise multiple key/value pairs associated with the same key might land in different R reduce workers therefore leading to partial computations.
- Whenever a map worker completes it's task, it forwards the location of these workers to the Master.
- The Master forwards these locations to the reduce workers.
- When reduce workers receive the location of the files, make procedere calls to fetch those files from the associated map worker.
- The reduce worker first sorts and groups the data by intermediate key. It then passes it through an iterator to the Reduce function and the final result is written to an output file.
- When all tasks are completed, the control is returned back to the user.
- The final result is persisted in the R output files.
The following sections are important. I would recommend the user to go through these sub-sections specially, if they intend to implement MapReduce by themselves.
Master maintains various data structures. Master maintains the state of the tasks and their worker machines. It also keeps the location of the output intermediate files generated by M map workers. It also forwards the location of the intermediate files to the reduce workers.
Every master schedules O(M+R) tasks and also keep O(M *R) state in memory as described above.
Concept of a Straggler
A straggler is an operation that takes unusually long time to complete the last few map or reduce tasks. This problem can arise due to a host of reasons. The scheduling system may have scheduled other tasks on a worker machine which makes it slow to execute the Map or Reduce task because of competition for CPU, network, disk usage and other parameters.
In order to mitigate this, the MapReduce system schedules additional back-up tasks for in-progress tasks whenever a MapReduce operation is close to completion. The task is marked as completed if either the primary or the back-up task executes successfully.
Worker Failures
The master pings the worker machines periodically. If a worker fails to respond within a specific interval of time, the worker is marked as failed. If the worker is a map worker irrespective of whether the task was completed or not, the data associated with that worker is also assumed to be lost. The master re-schedules that map task on an idle worker. If a reducer worker with a completed reduce task fails, the task for that reduce worker is not re-scheduled, since we persist the output in a global file.
Let's say a map task is re-executed on worker B after it's initial worker A failed. The master informs all the reduce workers so that if a reduce task hasn't read data from worker A, it reads the data from worker B. The paper doesn't explain how it handles the case where a reduce worker has already read some data from worker A and now needs to reach out to worker B for remaining data. How do we resolve it?
For handling master failures, it's best to just restart the entire MapReduce operation as the data lost is not critical and always be recomputed. It's possible to store checkpoints at specific intervals of time so that if a master fails, we can re-start at nearly exactly the same point of execution. However, this depends on the programmer's choice.
Good To Know
Partitioning Function
The user specifies the desired number of output files R. The data is partitioned across these tasks using the partition function on the intermediate key. The system uses a partition function by default which usually leads to a well-balanced partition. However, there might cases when we need custom partition logic in which case the user can provide their own partition function. For example, we might want data related to any host URL in a single file associated with each host.
Ordering Guarantee
Within a parition, the system ensures that the intermediate key/value pairs are sorted in increasing key order. This makes it efficient for random access lookups by key or sort order as it generates a sorted output file per partition.
Combiner Function
MapReduce also has the support for combiner functions. The combiner function is run after a map task and performs partial merging of the data before it's is sent across the network. This is helpful in cases where map task produces hundreds or thousands of values for the same intermediate key. One example is counting the word frequency in huge document dataset. It's possible that for a word like 'The', there would be huge amounts of <'The', 1> pair generated. The difference between a reduce function and a combiner function is that the output of reduce function is written to a global output file and the output of a combiner function is written to an intermediate file in the disk locally on the worker machine.
Input and Output Types
The MapReduce provides a set of pre-defined input and output types. The system already knows how to partition the data based on the input type. The user can also define their own input type by providing an implementation of the reader interface provided by MapReduce.
Skipping Bad Records
It's possible for our Map or Reduce task to fail on certain records. In such cases, it's okay to keep track of those records and ignore them during re-execution to make progress. This is specially useful when handling large amounts of data sets. For example, calculating analytical data for your website access.
Status Page
The master runs an internal HTTP server and serves a status page for any MapReduce operation. The status page displays the status of workers. It also contains the standard error and standard output files that the user can review to examine why a MapReduce operation is slower than expected in case of machine slowdown. The status page acts as a diagnose tool for our MapReduce operation.
Conclusion
This paper was first written in 2003. Since then it's possible that it has been enhanced and improved overtime. Consider this MapReduce paper to provide you a foundational mental model and not as a final iteration of this model.
References
- Google's official paper - MapReduce
- Helped me understand MapReduce - Breakdown by Jordan