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.

MeasureSpark joinKey-value lookups
Rows read2 billion, the whole history5,000
Bytes read400GBabout 1MB
Cost grows withtable sizekeys requested
Smallest possible latencyjob startup plus a full scanone 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

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.

Spark reads every trip once a night and writes one row per rider; each request reads a single key.
  • 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?

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.