Joins in Batch Processing
How a batch job joins two datasets that live on many machines: shuffle both sides, copy the small side everywhere, or partition both the same way in advance, and what to do about hot keys.
Overview
A join matches records from two datasets on a shared key, for example page views and user profiles on user_id. On one machine a database does this with an index. In a batch job both inputs are large files split across many machines, and there is no index. The records that must meet usually sit on different machines.
So every distributed join answers one question: how do the records with the same key end up in the same place? There are three answers, and they differ in what crosses the network.
- Sort-merge join (shuffle both sides): both datasets move over the network, once. It needs nothing special and works at any size.
- Broadcast hash join: only the small dataset moves, copied to every worker. It needs the small side to fit in each worker's memory.
- Partitioned join (map-side): nothing moves at join time. It needs both inputs to be already split the same way on the join key.
The rest of this chapter takes them in that order, then looks at the problem they all share (skew) and at how an engine chooses between them.
Sort-merge join: shuffle both sides
This is the default when both sides are large. It is also called a reduce-side join or repartition join. Each mapper reads a piece of either input and emits the join key with the record, tagged with the side it came from. The framework then partitions the output by key, so every record with the same key goes to the same reducer. The MapReduce paper describes the default partition function as hash(key) mod R, where R is the number of reducers, and guarantees that each reducer sees its keys in sorted order.
The reducer's job is simple because the keys arrive sorted: it takes all records for one key, pairs every profile with every view, and moves to the next key. One detail matters for memory. If the reducer must hold all records for a key before pairing them, a key with millions of views can exhaust memory. The fix is a secondary sort: order records so that the small side (the one profile) arrives first for each key. The reducer then keeps only that one record in memory and streams the views past it. Blanas and others call this the improved repartition join.
The cost is that both datasets cross the network and are sorted. Shipping the data is linear in its size; sorting adds a log factor on each machine.
Key concepts
- Sort-merge join
- Broadcast hash join
- Partitioned (map-side) join
- Shuffle
- Secondary sort
- Join skew
- Salting
Interview questions
- How does Spark decide which join strategy to use?
- Walk me through a sort-merge join in MapReduce. Where does the cost go?
- One task in your join stage runs for an hour while the other 199 finish in two minutes. What is going on and how do you fix it?
- Why do batch jobs join against a copy of a database table instead of querying the database?
- When is it worth storing tables pre-partitioned (bucketed) on a join key?
Sign up free to read the full topic
More DDIA topics
- Fan-Out and the Twitter Timeline Problem
- Fault Tolerance in Stream Processing
- Faults and Partial Failures
- Graph Data Models
- Implementing Serializability
- Isolation Levels
- Joins in Stream Processing
- Lambda and Kappa Architectures
- Leaderless Replication (Dynamo-Style)
- Linearizability
- LSM-Trees and SSTables
- MapReduce
Start free on Cariara · Coding problems · System design studio