Design an In-Process MapReduce
🗂️ What is an in-process MapReduce? MapReduce is a way to process a lot of data in parallel by writing just two small functions: map turns one input record into key-value pairs, and reduce combines all the values that share a key. The framework does the rest: it splits the input, runs the map function on many workers, groups the pairs by key, and runs the reduce function on many workers. Here we build that framework inside one process, with threads instead of machines.
This question comes in two forms. Some interviewers ask "count the words in a big set of documents using multiple threads". Others ask "build a mini MapReduce framework, then use it for word count". It is the same design: the multithreaded word count, done well, turns into split, map, shuffle and reduce, and once those four steps are in place, word count is just one pair of functions. We start from the word count, improve it until the framework appears, and then add a second job (an inverted index) to show that the framework is real.
Requirements
"We have many text documents. Count how often each word appears, using multiple threads. Design it so we can run other jobs like this too."
Clarifying Questions
You: What counts as a word?
Interviewer: Lowercase everything, then a word is a run of letters a to z. Anything else separates words.
You: How many threads should we use?
Interviewer: Make it configurable: some number of threads for the map step and some number for the reduce step.
You: Is it only word count, or should other jobs run on the same code?
Interviewer: Word count first. Then I would like an inverted index (which documents contain each word) without rewriting the threading code.
You: Does all the data fit in memory?
Interviewer: Yes, for now. One process, one machine.
You: In what order should results come back?
Interviewer: Sorted by word, so the output is deterministic. A "top K words" view would be nice too.
You: Can several callers run jobs on the same engine at the same time?
Interviewer: Yes. Two requests may run word counts at once.
The third answer is the one that turns a script into a design. If only word count existed, a few threads and a Counter would do. Because other jobs must reuse the threading code, the job-specific logic (how to split text into words, how to combine counts) has to move out of the engine and become something the caller passes in.
💡 Interview tip: Ask whether the parallel part should be reusable before you write any threads. "Count words with threads" and "build a framework" lead to the same final design, but you want to know which one you are being graded on before you choose where the word logic lives.
Final Requirements
- The engine is created with
Mmap threads andRreduce threads. - A job is two functions:
map(record) -> [(key, value)]andreduce(key, [values]) -> result. The engine runs any job and returns{key: result}. - The engine splits the input among the map threads, sends every pair with the same key to the same reduce thread, and runs the reduce threads in parallel.
- Word count: every word with its total count, sorted by word.
- Inverted index: every word with the sorted, de-duplicated list of documents that contain it.
- Top K: the
kmost frequent words, by count descending, ties alphabetical. - Several jobs may run at the same time on one engine and must not interfere.
Out of scope
- Data larger than memory (spilling to disk), and running on several machines.
- Failure recovery for crashed workers (we discuss retries in Extensibility).
- Streaming input that never ends.