Why Spark exists
MapReduce materialised at every stage boundary, while Spark materialises only at shuffles, where data must cross the network.
Chaining MapReduce jobs makes every stage boundary a full stop: each job writes its output to disk before the next one reads a single record. Spark keeps intermediates in memory as RDDs and tracks lineage, so a lost partition is recomputed from its parents instead of read back. Narrow steps fuse into one pass, and only wide dependencies, which move data across the network, pay the full shuffle cost.
Consider an iterative algorithm that scans the same dataset many times. In a chain of MapReduce jobs, pass one writes its full output to disk, and pass two reads it back before doing any work. Every pass therefore pays for the whole dataset, and the next cannot start until the previous one has fully written. Spark instead holds each pass's result as an RDD in memory and records how it was derived, its lineage. A narrow step, such as a map over each record, fuses into the step before it. Only a wide dependency, where each output partition needs records from many input partitions, forces a shuffle, and the shuffle is the one point where data is written out before the downstream stage begins.
The abstraction hides that recovery is now computation. In MapReduce, a lost reducer's input is still on disk, so recovery is a re-read. In Spark, a lost partition is recomputed from its parents, and those parents may already be gone from memory. Consider a node that holds partitions on both sides of a shuffle and dies after the shuffle completes. Rebuilding the post-shuffle state means re-running every upstream parent partition that lived on that node. Recomputation is cheap for short lineage and expensive for long lineage. Holding intermediates in RAM means Spark needs more memory than the equivalent disk-based MapReduce run, and when RAM runs short the cache evicts and recovery gets expensive.
Count shuffles before anything else, because they matter more than operator count. A job with k wide dependencies pays k full materialisations and has k recompute boundaries, so k predicts both runtime and the cost of losing a node. Filter and pre-aggregate before a shuffle so less data crosses the network. Broadcast the small side of a join so that edge stays narrow. Persist only the intermediates you reuse, because each persisted RDD holds memory for the rest of the job. Check the memory budget before you choose a chain length, and expect spills to behave like MapReduce when it is short. When a job is slow, find the operator that forced a repartition, not the one that looks expensive in the code.
- MapReduce stage boundary
- one disk write per stage
- In-memory record step
- microseconds to milliseconds per record
- Lineage depth
- bounded by the DAG
- Spark materialisation
- one per wide dependency
Each boundary in a chained job is a full materialisation, so a chain of k stages pays k writes and k reads back.
The rough cost of processing one record already resident in memory, the baseline a disk write or network shuffle has to be weighed against.
The count of parent steps a lost partition may require to recompute. Deeper lineage means recovery touches more of the graph.
Spark writes to disk at shuffle boundaries only, so a job with k shuffles pays about k materialisations, not one per step.
Iterative algorithms over one dataset often ran as chains of MapReduce jobs, one job per pass over the same records. Each job wrote its complete output to disk, so every pass re-read and re-wrote the whole dataset before the next could begin.
The wall was that every stage boundary became a full materialisation. An iterative algorithm therefore paid for the entire dataset on every pass, and each extra iteration added another full write and read. The cost compounded because nothing carried state between passes except the files on disk.
The RDD work answered this by keeping intermediate results in memory and tracking lineage, the record of which parent partitions produced each one. A lost partition is recomputed from its parents rather than read back from disk, so stages no longer need persisting between them.
The lesson is that recovery strategy and execution strategy are one decision. Once a lost partition can be recomputed, intermediates need not be persisted between stages, and the chain can run as a single pass. Recomputation is cheap for short lineage and grows expensive as lineage deepens, so that depth is the tradeoff to measure.