Complexity in Distributed Systems
On a cluster, the step that dominates is moving a row between machines: the row is serialized, sent over the network, often spilled to disk, and read back. Two plans with the same O(n) CPU work can move very different numbers of rows, and that row count is the cost to minimize.
What a shuffle does
Input arrives split into partitions, each on a worker, in whatever order it was written. Grouping by user_id needs every row for a user on the same worker, so the engine computes a target worker from each key and sends the row there. That redistribution is a shuffle. Below, partition i starts on worker i, and the target is user_id % 3.
partitions = [[(101, 5), (102, 3), (101, 2)],
[(103, 6), (101, 4), (102, 1)]]
workers = {0: [], 1: [], 2: []}
moved = 0
for source, rows in enumerate(partitions):
for user_id, amount in rows:
target = user_id % 3
workers[target].append((user_id, amount))
if target != source:
moved += 1
for target, rows in workers.items():
totals = {}
for user_id, amount in rows:
totals[user_id] = totals.get(user_id, 0) + amount
print("worker", target, totals)
print("rows that crossed the network:", moved)worker 0 {102: 4}
worker 1 {103: 6}
worker 2 {101: 11}
rows that crossed the network: 4
Four of the 6 rows crossed the network. With W workers and evenly spread keys, a row's target is its current worker only 1 time in W, so about (W - 1)/W of all rows move: two thirds on 3 workers, 99% on 100. A shuffle is O(n) in rows, and each row moved costs far more than a CPU step.
Shrink the data before the shuffle
Sums, counts, mins and maxes split into 2 steps: each worker totals its own rows per key, and only those partial totals are shuffled for the final sum. The rows that cross drop from n to at most workers × distinct keys.
- Event partitions (source, raw events); parallelism: 200 partitions
- Partial sum (transform, per worker, per key)
- Hash shuffle (queue, key % workers)
- Final sum (transform, per key)
- Daily totals (storage, 1 row per key)
- Event partitions -> Partial sum
- Partial sum -> Hash shuffle
- Hash shuffle -> Final sum
- Final sum -> Daily totals
partitions = [
[("ana", 5), ("bo", 3), ("ana", 2), ("ana", 7)],
[("bo", 1), ("ana", 4), ("cy", 6), ("bo", 2)],
[("ana", 9), ("ana", 1), ("cy", 3), ("ana", 8)],
]
raw_rows = sum(len(rows) for rows in partitions)
partial_rows = 0
for rows in partitions:
partial = {}
for user, amount in rows:
partial[user] = partial.get(user, 0) + amount
partial_rows += len(partial)
print("rows shuffled without combine:", raw_rows)
print("rows shuffled with combine:", partial_rows)Output:
rows shuffled without combine: 12 rows shuffled with combine: 7
With 1 billion events on 200 workers and 50,000 product ids, at most 10 million partial rows cross, 1% of the input. The function must split exactly: a sum of partial sums is the full sum, and an average travels as a (sum, count) pair. A median cannot be rebuilt from partial medians, so it gets no combine step.
Broadcast the small side of a join
A join needs matching keys on the same worker too. A shuffle join hashes both tables by the join key and moves |A| + |B| rows. A broadcast join copies the small table B to all W workers, moving |B| × W rows, and leaves the large table in place. Joining 500,000,000 orders to a 250-row country table on 100 workers:
| Plan | Rows moved | Total |
|---|---|---|
| Shuffle both sides | 500,000,000 + 250 | 500,000,250 |
| Broadcast countries | 250 × 100 workers | 25,000 |
Broadcast wins when |B| × W stays well below |A| and B fits in every worker's memory. That is a comparison of 2 counts, so redo it with real counts for each join.
A join pairs 2,000,000,000 events with 40,000,000 user rows on 400 workers. Which plan moves fewer rows?
- Shuffle both sides: about 2.04 billion rows, against 16 billion to broadcast
- Broadcast users, because the smaller table is always the one to broadcast
- Broadcast users, since a broadcast leaves the 2 billion events in place
- Either plan, since both move a number of rows proportional to the input
Skew: 1 key, 1 slow worker
A stage finishes when its slowest partition finishes. If 1 key holds most rows, hashing sends them all to 1 worker while the rest idle. Salting splits the hot key: give each of its rows a salt from 0 to k - 1, aggregate per (key, salt), then add up the k partials for that key in a second, tiny aggregation.
keys = [7] * 12 + [8, 9, 10, 11]
def rows_per_worker(pairs, workers=4):
counts = [0] * workers
for key, salt in pairs:
counts[(key + salt) % workers] += 1
return counts
plain = [(key, 0) for key in keys]
salted = [(key, i % 4 if key == 7 else 0) for i, key in enumerate(keys)]
print("plain: ", rows_per_worker(plain))
print("salted:", rows_per_worker(salted))Output:
plain: [1, 1, 1, 13] salted: [4, 4, 4, 4]
The busiest worker drops from 13 rows to 4, so the stage finishes about 3 times sooner with the same total work, plus 1 small extra aggregation.
About this lesson
This section is part of the Complexity: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.