Skip to content
SD-21 · Back to the write-up

Design a Sorting Large Data Sets

Sample keys to pick even ranges, sort blocks into runs per range, send each range to its owner, and merge each range into one part file.

Follow a request

Input
Plan
Sort and merge
Output
Job clientSubmits
Input files100 TB
CoordinatorTasks, retries
Key sampler249 split points
Task stateDurable
Map workersSort runs per range
Local disksSorted runs
Merge workersOne per range
Sorted outputParts + manifest
1coord → map · map task 81,204A worker gets a map task: one 256 MB block of one input file.1 / 4

The numbers, at a glance

Each worker’s share……is twice the memory it can sort
200 GB fits in memory400 GB share
Time for 100 TBMinutes, by design choice
Disk is most of the timePer GB: 4 s of disk passes, 1 s of network
So the best lever is fewer bytes: compression halves both.

One range, in order

Shown for range 17; all 250 ranges run side by side.

Coordinator
Map worker
Local disk
Merge worker
Output
1 map task (256 MB block)
2 sorted runs per range
3 done, piece sizes
4 merge range 17
5 fetch piece 17
6 1.6 GB sorted piece
7 part-00017.attempt-1
8 done, count, checksum
9 rename, manifest last
RequestResponseAsync, does not block

How to split the work

The decision this question is really about.

Split by input
One bottleneck
250 sorted files
one merger
27+ hours
+ No shuffle.− A final merge of 100 TB on one machine.
Hash partition
Wrong order
hash(key) mod 250
each part sorted
parts overlap
+ Even shares.− Ranges overlap.
Sampled ranges
Our pick
sample → 249 splits
map runs per rangeshuffle
250-way merge per range
+ Parts concatenate in order.− Needs a sample pass.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 100 GBMake it work
Input
Sort process
Output
One machine sorts in memory: one pass.
Stage 2 · ≈ 1 TBSpill to disk
Input
Sort processLocal disk runs
Output
Sorted runs on local disk, then a heap merge: two passes.
Stage 3 · ≈ 100 TBSplit by range
CoordinatorKey sampler
Map workersLocal disk runs
Merge workers
Output
Sampled split points over 250 workers; shuffle each range to its owner.
Stage 4 · 100 TB in ≈ 23 minFast and sturdy
CoordinatorKey samplerTask state
Map workersCompressed runs
Merge workers
Output + manifest
Compression, saved task state, retries, atomic commit.
Was this walkthrough useful?Your vote tells us what to improve next.