Partitions and Shuffles: repartition vs coalesce
Databricks Spark Developer Associate · Lesson 5 of 18 · 26:13
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: partitions, the partitions of file reads, shuffle partitions, what a shuffle costs, and repartition versus coalesce. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Two hundred tiny tasks
Coralpeak's nightly job aggregates a small table of a few thousand rows, and the Spark UI shows a stage of two hundred tasks that each finish in milliseconds. Another job ends with one huge partition, and a single task works on it for an hour. This lesson covers partitions, shuffle partitions, the cost of a shuffle, and how to change the number of partitions.
Partitions
A partition is a chunk of the rows of a DataFrame, and each task processes one partition, so the number of partitions sets how much parallel work Spark can do. When Spark reads files, it packs them into partitions of up to one hundred twenty eight megabytes by default, a setting called max partition bytes. So a ten gigabyte folder of events gives roughly eighty partitions, while many tiny files are packed together. And you can check the count with getNumPartitions, on the underlying RDD of the DataFrame.
Shuffle partitions
After a shuffle, for a join or an aggregation, the number of partitions comes from another setting: spark.sql.shuffle.partitions, which is two hundred by default. Two hundred suits medium data, but small data ends up in two hundred tiny tasks, and very large data in huge tasks that spill to disk. You can change it for the session, or let Adaptive Query Execution merge small shuffle partitions at run time, which it does by default. So the right number depends on the size of the data after the shuffle, not on the number of input files.
What a shuffle costs
A shuffle redistributes rows by key, so that all the rows with the same key end up in the same partition. Each task of the first stage writes its rows to shuffle files on local disk, sorted by target partition, and the tasks of the next stage fetch their part over the network. So a shuffle costs disk reads and writes, serialization, and network traffic, and when the data does not fit in memory, it spills to disk. The Stages tab of the Spark UI shows the shuffle read and shuffle write sizes, and the spill, for every stage.
repartition and coalesce
repartition returns a DataFrame with exactly the number of partitions you ask for, more or fewer, through a full shuffle that spreads the rows evenly. Given columns, it uses hash partitioning on them, so that rows with the same key land together, for example before a write partitioned by date. coalesce only reduces the number of partitions, and it avoids a full shuffle by merging existing partitions, so it is cheaper. Asking coalesce for more partitions than there are leaves the count unchanged. But a drastic coalesce, down to one partition, puts all the work of that stage on one task, so repartition can be better when the work upstream is heavy.
Recap
Let's recap. A partition is a chunk of rows, and each task processes one, so partitions set the parallelism. File reads pack the data into partitions of up to one hundred twenty eight megabytes. After a shuffle, the partitions come from the shuffle partitions setting, two hundred by default, which AQE can merge at run time. A shuffle moves rows by key through local disk and the network, and it can spill. And repartition shuffles to any number of partitions, while coalesce only reduces them, without a full shuffle.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Coralpeak reads event files that add up to about 12 GB, with the default settings. About how many partitions does the DataFrame get?
- 200, the default number of partitions
- About 96, one for each 128 MB
- One for each executor
- 12, one for each gigabyte
Show the answer
About 96, one for each 128 MB
When it reads files, Spark packs them into partitions of up to 100 twenty eight megabytes, so twelve gigabytes give about ninety six partitions. “200, the default number of partitions” is wrong, since two hundred is the default for shuffles, not for reading files. “One for each executor” is wrong, since the number of executors does not set the partitions of a read. And “12, one for each gigabyte” is wrong, since the default split size is 100 twenty eight megabytes, not one gigabyte.
An aggregation of a few thousand rows runs 200 tasks after its shuffle, each with almost no data, and AQE is turned off. What explains the count?
- The input folder holds 200 files
- The cluster has 200 cores
- The data was cached in 200 blocks
- The shuffle partitions setting, 200 by default
Show the answer
The shuffle partitions setting, 200 by default
After a shuffle, the number of partitions comes from the shuffle partitions setting, two hundred by default, and with AQE turned off, nothing merges them. “The input folder holds 200 files” is wrong, since input files set the partitions of the read, not those after the shuffle. “The cluster has 200 cores” is wrong, since the number of cores sets how many tasks run at once, not how many exist. And “The data was cached in 200 blocks” is wrong, since the data was not cached, and caching does not set the shuffle partitions anyway.
A job filters a large table down to a few hundred MB over 2,000 partitions, and must write about ten files as cheaply as possible. Which call fits?
- df.coalesce(10)
- df.repartition(10)
- df.repartition(10000)
- df.coalesce(4000)
Show the answer
df.coalesce(10)
Coalesce merges the existing partitions down to ten without a full shuffle, which is the cheapest way to reduce their number. “df.repartition(10)” is wrong, since repartition would also give ten partitions, but through a full shuffle. “df.repartition(10000)” is wrong, since it would create even more partitions, and more files. And “df.coalesce(4000)” is wrong, since coalesce cannot increase the number of partitions, so it would keep all two thousand.