---
source_url: "https://www.cloudthat.com/resources/blog/handling-data-skew-in-spark-the-power-of-salting"
title: "Handling Data Skew in Spark: The Power of Salting - CloudThat Resources"
mirrored_at: 2026-08-11T15:03:04.839Z
host: www.cloudthat.com
cited_in_42a: true
mirror_canonical: "https://index.42a.ai/www.cloudthat.com/resources/blog/handling-data-skew-in-spark-the-power-of-salting"
---

> **Original source:** https://www.cloudthat.com/resources/blog/handling-data-skew-in-spark-the-power-of-salting

## Understand Dataskew

In Apache Spark, data skew occurs when certain keys in your dataset are significantly more frequent than others.  
For efficient usage of Spark, the data needs to go into each executor of similar size. But consider the following scenario where we are joining two datasets. Suppose you’re joining two datasets on customer\_id. If customer\_id = 123 occurs in 80% of the records, all those records are sent to the same partition. So here, one executor gets overloaded, while others remain idle.

**Impact of skewing:**

-   Straggler tasks that run much longer than others.
-   Executors suffering from out-of-memory (OOM) errors.
-   Overall poor Spark job performance, even if cluster resources look sufficient.

### Start Learning In-Demand Tech Skills with Expert-Led Training

-   Industry-Authorized Curriculum
-   Expert-led Training

[Enroll Now](https://www.cloudthat.com/training/?utm_source=blog-page&utm_medium=website)

## Techniques to Overcome Data Skew

Spark provides multiple ways to deal with data skew:

1.  Repartitioning / Coalesce

– Distribute data more evenly by increasing or reducing partitions.

2.  Broadcast Joins

– Send a smaller dataset to all executors to avoid shuffles.

3.  Skew Join Hints

– Spark 3.x introduced /\*+ SKEW \*/ hints to optimize skewed joins automatically.

4.  Salting

– Artificially split hot keys into multiple sub-keys so that executors share the load.

Each of these helps, but salting in Spark is the most direct way to fix extreme key skew.

 **Salting Technique with Example**

Let’s understand skewing and salting with the below example

## The Problem

Imagine two datasets:

-   df1 → very skewed on customer\_id = 123.
-   df2 → balanced data.

Usual  join is

df1.join(df2, “customer\_id”)

So, all customer\_id=123 records will end up in one executor, creating skew.

## The Solution: Salting

We add a **salt column** to the skewed dataset (df1) to distribute heavily repeated keys across multiple partitions. At the same time, we expand the smaller dataset (df2) by duplicating each row for every possible salt value. This ensures that when the join is performed on (customer\_id, salt), the salted keys from df1 still have corresponding matches in df2, while the workload is evenly balanced across executors.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<strong\><em\>from pyspark.sql.functions import col, lit, rand, floor, explode, array</em\></strong\>

<strong\><em\>\# Add salt to skewed dataset (df1)</em></strong>

<strong\><em\>df1\_salted \= df1.withColumn("salt", floor(rand()\*5)) </em\></strong\>

<strong\><em\>Here 5 buckets are created for distributing the data into more partitions</em\></strong\>

<strong\><em\> </em\></strong\>

<strong\><em\>\# Expand df2 to replicate rows with all salt values</em></strong>

<strong\><em\>salt\_values \= \[lit(i) for i in range(5)\]</em\></strong\>

<strong\><em\>df2\_expanded \= df2.withColumn("salt", explode(array(\*salt\_values)))</em\></strong\>

<strong\><em\> </em\></strong\>

<strong\><em\>\# Join on (key + salt)</em></strong>

<strong\><em\>     df\_joined \= df1\_salted.join(df2\_expanded, \["customer\_id", "salt"\])</em\></strong\>

## How This Works

-   df1 → adds a random salt (0–4) to each row.
-   df2 → replicates each row across all salt values, so that salted keys still match.
-   Join happens on (customer\_id, salt) → spreading skewed keys across **5 executors instead of 1**.
-   After join, you can drop the salt column if not needed.

## Executor-Level Behavior

To have clarity on the salting techniques and how it works within the executors. Consider there were 5 executors for the above example.

-   **Without salting:**
    -   Executor-1: 100 records of customer\_id=123
    -   Executors 2–5: almost idle
-   **With salting (5 buckets):**
    -   Executor-1: 20 records (123\_0)
    -   Executor-2: 20 records (123\_1)
    -   Executor-3: 20 records (123\_2)
    -   Executor-4: 20 records (123\_3)
    -   Executor-5: 20 records (123\_4)

Load is evenly balanced, all executors work in parallel, job finishes faster.

## Conclusion

Data skew is one of the most common performance bottlenecks in Spark jobs. While Spark provides multiple tools—broadcast joins, repartitioning, and skew hints—salting in Spark remains a powerful, hands-on technique for balancing workloads when a few keys dominate your dataset.

By understanding how salting in Spark works at the executor level, data engineers can make their pipelines faster, more efficient, and more reliable—whether on open-source Spark or platforms like Azure Databricks.

Interested to learn on spark and Azure Databricks, please visit CloudThat Website for our customized trainings [https://www.cloudthat.com/training/databricks](https://www.cloudthat.com/training/databricks) .

### Upskill Your Teams with Enterprise-Ready Tech Training Programs

-   Team-wide Customizable Programs
-   Measurable Business Outcomes

[Learn More](https://www.cloudthat.com/corporate-training/?utm_source=blog-page&utm_medium=website)

## About CloudThat

[CloudThat](https://www.cloudthat.com/) is an award-winning company and the first in India to offer [cloud training](https://www.cloudthat.com/) and [consulting services](https://www.cloudthat.com/consulting/) worldwide. As an AWS Premier Tier Services Partner, AWS Advanced Training Partner, Microsoft Solutions Partner, and Google Cloud Platform Partner, CloudThat has empowered over 1.1 million professionals through 1000+ cloud certifications, winning global recognition for its training excellence, including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 14 awards in the last 9 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, Security, IoT, and advanced technologies like Gen AI & AI/ML. It has delivered over 750 consulting projects for 850+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.