Error fixData Engineering · 3 min read · Updated 6 Dec 2026

Spark Data Skew: Why One Task Is Slow and How to Fix It

Fix Spark data skew where one task runs for hours: detect it in the Spark UI, then use AQE skew joins, broadcast joins, salting or filtering hot keys.

QUICK ANSWERData skew means a few keys hold far more rows than the rest, so after a join or groupBy one task gets most of the data and runs much longer (or fails with out of memory) while others finish in seconds. Detect it in the Spark UI by comparing max and median task times. Fix it with Adaptive Query Execution skew join handling, a broadcast join when one side is small, salting the hot key, or processing hot keys separately.

Learn this hands-on: covered in Week 4: Apache Spark of our 12-week Data Engineering course, with live labs and a trainer to help. See the full syllabus →

Spark data skew: causes and fixes
Spark data skew: causes and fixes

How to spot skew

  1. Spark UI → Stages → the slow stage → Summary Metrics.
  2. Compare Max with Median for duration and shuffle read size; a max 10x or more above the median is skew.
  3. Find the hot keys: df.groupBy("customer_id").count().orderBy(F.desc("count")).show(10).
  4. Common culprits: null or empty keys, a "default" or "unknown" value, one very large customer or store.

Fixes, from easiest

FixWhenHow
AQE skew joinSpark 3+ sort-merge joinsspark.sql.adaptive.enabled and spark.sql.adaptive.skewJoin.enabled = true (defaults in recent versions)
Filter or handle nullsSkew comes from null keysFilter them out or process them separately
Broadcast joinOne side fits in memory (tens to a few hundred MB)df.join(F.broadcast(small), "key")
SaltingHuge hot keys on both sidesAdd a random 0..N suffix to the hot key, replicate the other side N times
Two-stage aggregationSkewed groupByAggregate by (key, salt), then by key

Salting example

from pyspark.sql import functions as F
N = 16
big = big.withColumn("salt", (F.rand() * N).cast("int"))
small = small.crossJoin(spark.range(N).withColumnRenamed("id", "salt"))
out = big.join(small, ["customer_id", "salt"]).drop("salt")

Each hot key is now spread across 16 tasks instead of one. Salt only when AQE and broadcast joins are not enough, because it multiplies the smaller side.

Prevent it

  • Check key distributions when you design a pipeline, not after it fails.
  • Avoid partitioning output by a column with a few huge values; use date plus a better key, or liquid clustering on Delta tables.
  • Keep AQE on and use recent Spark/Databricks runtimes.

Questions

Is skew the same as too few partitions?

No. With too few partitions every task is big. With skew, most tasks are small and a few are huge.

Does Databricks fix skew automatically?

AQE handles many skewed joins automatically, but extreme skew, skewed aggregations and null keys can still need manual fixes.