When Not to Join in Spark
Spark's join cost follows the bytes it reads. A sort-merge or broadcast join reads both sides in full, less whatever partition pruning removes, which suits work where most rows on both sides reach the output. When the work is fetching a few thousand rows by key from a table of billions, an index or a key-value store reads those rows and nothing else, and its cost follows the number of keys.
Same question, 2 cost curves
A support tool flags 5,000 trips today and needs each rider's history from a table of 2 billion rows at about 200 bytes each.
| Measure | Spark join | Key-value lookups |
|---|---|---|
| Rows read | 2 billion, the whole history | 5,000 |
| Bytes read | 400GB | about 1MB |
| Cost grows with | table size | keys requested |
| Smallest possible latency | job startup plus a full scan | one round trip per key or batch of keys |
The Spark job can broadcast the 5,000 keys and skip the big shuffle, and it still scans 400GB to find 5,000 rows. Unless the history's layout lets pruning skip files for those keys, every file is opened. The crossover sits where the keys you need cover a large share of the table: fetching 500 million rows by key would mean 500 million round trips to read 100GB that one scan reads sequentially.
Signs the join belongs in a store
- The driving side is tiny next to the lookup side: thousands of keys against billions of rows
- The answer is needed per request or per event, within milliseconds of a user acting
- The same keyed lookup runs many times a day against a table that changes slowly
- Each run reads the big side whole and keeps a tiny fraction of it
Spark builds, the store serves
Most of these cases still need Spark for the half that touches every row. A nightly job aggregates all trips per rider, a sort-merge join plus a groupBy over the full history, and writes one row per rider_id into a key-value store. The fare service and the support tool then read single keys. The join that reads everything runs once in batch, and the lookups that read one row run in the store.
- Trip log (source, event log)
- Nightly rider build (transform, Spark); parallelism: 200 partitions
- rider_features (storage, key-value store); freshness: 24 hours
- Fare service (consumer, API)
- Support tool (consumer, web app)
- Trip log -> Nightly rider build
- Nightly rider build -> rider_features
- rider_features -> Fare service
- rider_features -> Support tool
The reverse mistake
The opposite error is calling the store from inside a Spark transformation, one request per row. Enriching 48 million trips that way makes 48 million round trips from the executors, capped by what the store can serve, and a retried task repeats its calls. When most keys are needed, export a snapshot of the store's table and join it in Spark, which reads it once in bulk.
A fraud check must attach a card's 30-day spend to each incoming payment within 50 milliseconds. Spend comes from a 3-billion-row payment history. How should the work be split?
- A Spark job runs per payment and joins it to the history on card_id, returning the sum.
- Broadcast the payment history to the fraud service, which joins each payment against it.
- The key-value store holds raw payments and sums a card's last 30 days on each request.
- Spark computes 30-day spend per card in batch and writes it keyed by card; the check reads one key.
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.