Lesson 3 of 5 Data Quality

Detecting and Fixing Data Skew

This lesson explains data skew, where a few keys hold most of the data or traffic so that a few tasks, slices, shards or partitions do most of the work, and how to detect and fix it.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify where skew appears in each service.
  • Detect skew from task durations and metrics.
  • Spread hot keys with salting.

Where skew appears

Skew means some keys carry far more data or traffic than others, and it appears in several services. In Spark, on Glue or EMR, the tasks that receive hot keys become stragglers that hold up the whole job, or run out of memory. Fixes include salting the key, adding a random suffix so its rows spread across tasks, adaptive query execution, broadcast joins for small tables, and repartitioning.

In Redshift, a distribution key with uneven values overloads some slices, so queries run as slowly as the busiest slice; a better key, or EVEN or AUTO distribution, fixes it. In Kinesis and DynamoDB, a popular partition key creates hot shards or partitions that throttle, fixed with higher-cardinality keys. The following table summarises the cases.

WhereEffectFix
Spark (Glue, EMR)Tasks for hot keys become stragglers or run out of memorySalting, adaptive query execution, broadcast joins, repartitioning
RedshiftA skewed distribution key overloads some slicesA better distribution key, or EVEN or AUTO
Kinesis, DynamoDBHot shards and partitions throttleHigher-cardinality partition keys
Match the pairs

Skew by service

Exam skills 3.4.5

Match each skew symptom with its fix.

Log in to try this activity Interactive activities are free with an account.

Detecting skew

Data skew means that work is unevenly distributed: a few partitions or keys hold far more data than the rest, so the tasks that process them run much longer while others sit idle. Skew is usually visible in monitoring before it causes failures.

In the Spark UI, a skewed stage shows a few tasks that take far longer, or process far more data, than the median task. AWS Glue observability metrics report skew in job stages directly. In Amazon Redshift, SVV_TABLE_INFO reports row skew across slices, which slows every query on the table. In Amazon DynamoDB, CloudWatch Contributor Insights shows the most frequently accessed keys, revealing hot partitions.

The following diagram shows the task durations in a Spark stage slowed by a skewed join.

The skewed join. 999 tasks finish in about a minute, but one task processes 30% of all rows, the orders with a NULL customer ID from guest checkouts, which all hash to the same partition, and takes 40 minutes. Handling guest orders separately removes the giant group from the join.
Only a sample of the short tasks is drawn.
Match the pairs

Detecting skew

Exam skills 3.4.5 · 3.3.4

Match each service with where skew becomes visible.

Log in to try this activity Interactive activities are free with an account.

Salting

Salting appends a random suffix to a hot key, so its rows spread over many partitions; results are then aggregated again without the suffix. In joins, the other side is replicated once for each suffix. Null keys, such as guest orders without a customer ID, are a frequent cause of skew and can often simply be handled separately.

The following diagram shows a hot key before and after salting.

Salting a hot key. Without salting, every row for key A goes to one partition and one task. Appending a random suffix from 0 to 3 spreads key A's rows over four partitions, and a second aggregation without the suffix combines the partial results. In a join, the other side is replicated once for each suffix.
The row counts are illustrative.
Worked example

One slow task out of a thousand

A Spark join of orders to customers finishes 999 tasks in a minute, then waits 40 minutes for the last one. The Spark UI shows that task processed 30% of all rows: orders with a NULL customer ID (guest checkouts) all hash to the same partition. The following table lists the possible fixes.

FixHow it helps
Filter out or handle NULL keys separatelyRemoves the giant group from the join
Salt the skewed key (append a random suffix)Spreads one key over many partitions
Broadcast the smaller tableAvoids shuffling the large table at all
Enable adaptive query execution skew join handlingSpark splits skewed partitions automatically

Handling guest orders separately is the simplest fix here, because they have no customer to join to.

Check yourself

How is skew across Redshift slices fixed?

Show answer

By changing the distribution key, or using EVEN or AUTO distribution.

Exam Tips

Remember for the exam

  • Spark straggler tasks on one hot key are fixed with salting or adaptive query execution.
  • Imbalanced Redshift slices are fixed by changing the distribution key, or using EVEN or AUTO.
  • A hot Kinesis shard or DynamoDB partition is fixed with a higher-cardinality key.
  • Broadcast the smaller table to avoid shuffling a skewed large table.
  • Handle null join keys separately when they cause skew.