Deep Read · DDIA · Chapter 10

Batch Processing

Designing Data-Intensive Applications · Ch 10 · Martin Kleppmann · 2017

中文 →

What is this chapter about?

The "recommended for you" list on a shopping app, the feed you scroll on a video app, the results Google hands back—behind all of them sits a kind of computation that runs quietly, at its own pace, often overnight: it gathers a whole day's worth of everyone's behavior and, in one big pass, computes "who might like what" and "which word maps to which web pages." This chapter is about that gather-a-big-batch-and-crunch-it-all-at-once kind of work, called batch processing, and its most classic move—MapReduce.

An analogy first

Batch processing is like doing a big load of laundry: you don't wash one shirt the moment it's dirty (that's an "online service," where a click should get an instant reaction). You wait until you have a full basket, then run the machine once—not chasing speed on any single shirt, but chasing "the whole load, done efficiently." It doesn't care whether you wait a second or an hour; it only cares how long the whole batch takes and how much it can chew through per hour.

Why the old world was hard

The hard part is that the data is enormous—too big for one machine's disk to hold, too big for one CPU to ever finish. You have to split the work across thousands of machines working together. But the moment you have a crowd, new headaches appear: how do you hand out the work fairly? What if a machine dies halfway through? Where do the half-finished results go? These "coordinate a swarm of machines" chores are the real source of pain.

The core mechanism, intuitively

The key idea of MapReduce is simply "tally in parallel, then group and total." Picture hundreds of people together counting how many books each author has in a giant library. Step 1 (Map)—each person takes a stack and writes every book onto a little card, "author name → 1." Step 2 (grouping)—line all the cards up by author name and pile them together, so every card for the same author naturally ends up next to each other. Step 3 (Reduce)—each author has one pile; count it and you have their book total. That "line up by name, gather the same-named ones into one pile" step is the heart of the whole machine—it's what lets the same kind of data, scattered across thousands of machines, finally meet up and be totaled.

What it gives you / how to choose

With this "tally then total" trick, engineers can chew through vast data on a pile of cheap machines: build a search engine's index, compute a recommendation list, train a model. Better still, it isn't afraid of failure—the input is read-only, so if a machine botches its share, just recompute that piece on another machine; even human coding mistakes can be fixed and the whole thing re-run cleanly, no lingering damage. One honest cost: it's a slow-and-steady creature by nature—you must wait for the whole batch to finish before you get results, so it only suits offline work that can wait; anything real-time needs the "stream processing" covered next chapter.

Remember one thing

Batch processing = gather a big batch of data and crunch it all at once, optimizing throughput (how much per hour), not speed. The move is MapReduce: tally in parallel (Map) → line up by name to gather like with like → total (Reduce), using "sort-and-gather" to make data from thousands of machines meet. Inputs are read-only and failed pieces just get recomputed—which is why it's so wonderfully tough.

Want the actual mechanisms, join strategies, and diagrams? → Switch to the deep read