Shuffle-Hash Join

A shuffled hash join keeps the 2 shuffles of sort-merge and replaces its 2 sorts with a hash table. Each reduce task fetches its partition of both sides and loads the smaller slice into a hash table keyed on the join key. It then streams the larger slice through that table, probing once per row. Nothing waits for a sort, and no key order is needed.

Sort-merge join, per reduce task
  • Fetches its slice of both sides
  • Sorts both slices by key
  • Merges them in one forward pass
  • Can write sorted runs to disk when memory runs short
  • The planner's default for large equi-joins
Shuffled hash join, per reduce task
  • Fetches its slice of both sides
  • Builds a hash table from the smaller slice
  • Probes it once per row of the larger slice
  • Needs the whole build slice in memory at once
  • Chosen by a hint or by changing a default

How it gets into a plan

Spark plans a shuffled hash join in 2 cases

The 2,000MB test averages out to 10MB per reducer, and that average assumes the build side's keys spread evenly over 200 partitions. Hash partitioning sends every row of a key to one task, so the estimate says nothing about the largest slice. To confirm the choice, look for ShuffledHashJoin [driver_id], [driver_id], Inner, BuildRight in the plan, with an Exchange under each side and no Sort above either.

priced = trips.join(drivers.hint("shuffle_hash"), "driver_id")
priced.explain()

When dropping the sort wins

Take 60GB of trips joined on driver_id to a 600MB drivers table with one row per driver, over 200 partitions. Each reduce task receives around 300MB of trips and 3MB of drivers. Sort-merge sorts both slices before its first match. The hash join builds a 3MB table and streams the 300MB through it unsorted. Shuffle bytes are the same either way, so the saving is the sort of 60GB of rows, spread over 200 tasks, on every run.

The memory bet

The saving holds only if every task's build slice, as a hash table, fits in the memory the task can use. An executor with 4 cores runs 4 tasks at once, and they share its memory. Sort-merge degrades when memory is short, writing sorted runs to disk and reading them back. The hash table in this operator has no disk fallback, so a slice that does not fit fails the task with an out-of-memory error.

Build sideRows per keyAverage sliceLargest sliceResult
`drivers`, 600MB13MBabout 3MBFaster than sort-merge
`city_fares`, 1.6GBone city holds 25%8MB400MB or moreThe task holding that city fails

In the second row the bet is lost. Every row of the large city hashes to one partition, so one task must build a hash table of at least 400MB while the other 199 build around 8MB each. A retry reads the same partition and fails the same way. After spark.task.maxFailures attempts, 4 by default, the job fails. Without the hint, sort-merge would have finished that task slowly, sorting the slice partly on disk.

A join hinted shuffle_hash on city_fares (1.6GB, 200 shuffle partitions) fails: task 137 runs out of memory on all 4 attempts while the other 199 tasks succeed. What is the cause, and what fixes it?

Spark reads 4 join hints, one per strategy: broadcast plans a broadcast hash join, merge a sort-merge join, shuffle_hash a shuffled hash join, and shuffle_replicate_nl a CartesianProduct. For broadcast and shuffled hash, the hinted side is the one built into a hash table. When the 2 sides carry different hints, broadcast wins over merge, merge over shuffle_hash, and shuffle_hash over shuffle_replicate_nl. A hint the join cannot honour, such as shuffle_hash on a condition with no equality, is dropped with a warning in the driver log.

About this lesson

This section is part of the Joins: 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.