In this tutorial" I will show you what is the difference of Spark" Repartition Vs Coalesce. When we work with Spark" very often we have to change the partitions amount of the DataFrame" or DataSet". The repartition() and coalesce() will do this for us, but with ONE Major Difference which is very important from performance perspective.
Table of Contents
Introduction
Apache Spark
Apache Spark" is a fast and powerful open-source data processing engine. It is designed to be easy to use and provides a range of tools and libraries for data processing, including support for SQL", machine learning", and stream processing.
Spark is highly scalable and can run on a single machine or on a cluster of hundreds of machines, making it an ideal choice for big data" processing tasks. It is also flexible and interactive, allowing you to easily develop and deploy distributed applications.
Spark is widely used in a variety of industries and applications, including data analytics, machine learning", and real-time stream processing. It is also well-suited for a range of data processing tasks, such as ETL" (extract, transform, and load), data aggregation, and data transformation.
Apache Spark DataFrame / DataSet Partitions
In Apache Spark", a DataFrame or DataSet" is a distributed collection of data that is organized into rows and columns. It is similar to a table in a relational database or a data frame in R or Python".
a DataFrame or DataSet" can be partitioned into smaller chunks called partitions, which can be processed independently and in parallel. By default", Spark tries to create a partition for every block of the file being read (assuming the file is splittable).
The number of partitions in a DataFrame or DataSet" can have a significant impact on the performance of Spark applications. Having too few partitions can cause tasks to be slow, while having too many partitions can cause tasks to be too fine-grained and result in too much overhead.
To optimize the number of partitions in a DataFrame or DataSet", you can use the repartition
or coalesce
methods. The repartition
method allows you to specify the desired number of partitions, while the coalesce
method allows you to decrease the number of partitions while preserving the existing partitioning.
Spark Data Shuffle
In Apache Spark", a data shuffle is the process of moving data between executors and nodes in a cluster. It is a common operation that is performed when executing Spark jobs, and it is used to distribute data and compute tasks across the cluster.
A data shuffle occurs when a Spark job requires data that is not available on the current executor or node, or when the data needs to be redistributed to balance the load across the cluster. For example, a data shuffle might be triggered when:
- A
groupByKey
operation is performed on a pair RDD" - A
join
operation is performed on two RDDs - A
repartition
orcoalesce
operation is performed on a DataFrame or DataSet"
A data shuffle can have a significant impact on the performance of a Spark job. It can be an expensive operation, particularly for large datasets, because it requires data to be transferred over the network and written to disk. To optimize the performance of Spark jobs, it is generally a good idea" to minimize the amount of data shuffling that is required.
Spark Repartition Vs Coalesce
In Apache Spark", repartition
and coalesce
are methods that can be used to change the number of partitions in a DataFrame or DataSet".
The repartition
method allows you to specify the desired number of partitions and will shuffle the data to create a new partitioning scheme. This can be useful if you want to increase the number of partitions to allow for parallel processing, or if you want to change the current partitioning scheme. However, repartition
can be expensive because it requires data shuffling, which can be time-consuming for large datasets.
Spark Coalesce
The coalesce
method, on the other hand, allows you to decrease the number of partitions while preserving the existing partitioning. It does this by combining existing partitions, rather than shuffling the data to create a new partitioning scheme. This can be more efficient than repartition
when you want to decrease the number of partitions, but it can only be used to decrease the number of partitions and not increase it.
As I mentioned these both methods will change the DataFrame or DataSet" partitions, but the Coalesce() will do this better!
1st Difference – Why Coalesce() Is Better Than Repartition()?
The answer is: PERFORMANCE! When the DataFrame or DataSet" is spread across the Nodes when we execute the Coalesce() method the Spark will limit the data shuffle between data nodes.
As we know the Exchange (shuffle) is one of the most time consuming operation, due to fact that data must be transferred between nodes and it causes the network traffic, which is bad and unwanted.
When you will go to the Spark code the coalesce is just the repartition with shuffle = false as default". Let’s the code: https://github.com/apache/spark/blob/v3.3.0/core/src/main/scala/org/apache/spark/rdd/RDD.scala#L500
def coalesce(numPartitions: Int, shuffle: Boolean = false, partitionCoalescer: Option[PartitionCoalescer] = Option.empty) (implicit ord: Ordering[T] = null)
Spark Repartition Vs Coalesce – Shuffle
Let’s assume we have data spread across the node in the following way as on below diagram.
When we execute coalesce() the data for partitions from Node 1 and Node 3" will be kept locally and only data from Node 2 and Node 4 will be shuffled, so it will limit the network traffic across the data nodes in you cluster.

2nd Difference – Partitions Amount
When you call .coalesce(10) on the DataFrame/DataSet which already has lower amount of partitions nothing will happen. To make it you need to run .repartition(10) instead.
Method | Increase | Decrease |
---|---|---|
repartition | Yes | Yes |
coalesce | No | Yes |
Save DataFrame As Single File
Based on the above knowledge to save the DataFrame as single file you must use the .repartition(1) instead of .coalesce(1).
df .repartition(1) .write .format("com.databricks.spark.csv") .option("header", "true") .save("all_data_in_one_file.csv")
PySpark Coalesce / PySpark Repartition
The presented principles and actions are also valid for PySpark" (Python" Spark API"), because the core mechanisms are the same for Spark with the Scala API or Java as for Python".
Also enjoy the benefits of Spark regardless of what API you use. Spark will take care of everything underneath.
Summary
Spark Repartition Vs Coalesce – In this post you learned what are the differences between repartition and coalesce and how to use them using Spark with Scala and PySpark".
There are several ways to minimize data shuffling in Spark, such as using partitioning functions that preserve the partitioning of the input data, using broadcast variables to send large datasets to all nodes in the cluster, and using cached data to avoid recomputing expensive transformations.
In general, you should use repartition
if you want to increase the number of partitions or change the current partitioning scheme, and use coalesce
if you want to decrease the number of partitions while preserving the existing partitioning.
Could You Please Share This Post?
I appreciate It And Thank YOU! :)
Have A Nice Day!