repartition() versus coalesce() in Spark
Spark partition control.
repartition does a full shuffle and can increase or balance partitions; coalesce avoids a full shuffle and only reduces them.
thinking coalesce can increase partitions or always beats repartition.
What's really being asked
Whether you grasp how Spark physically reorganizes data across partitions and the cost difference between a full shuffle and a narrow merge, which directly affects job runtime.
The full answer
repartition performs a full shuffle, redistributing rows across the cluster, and can either increase or decrease the number of partitions while producing roughly even partition sizes. That balance is valuable but the shuffle is expensive. coalesce instead merges existing partitions on the same executors to reduce the count, avoiding a full shuffle by combining adjacent partitions; it is much cheaper but can only decrease the partition count and may leave partitions unevenly sized. The decision hinges on direction and balance. If you need more partitions, or need to fix skew so work is evenly spread before a costly join or aggregation, use repartition despite the shuffle. If you simply need fewer partitions, for example to avoid writing thousands of tiny output files at the end of a job, use coalesce because it gets you there without the network cost.
The mistakes people make
Claiming coalesce can increase partitions, or that it is always preferable. Forgetting that coalesce can create skewed partitions that hurt a downstream join.
What usually comes next
What happens if you coalesce to one partition on a huge dataset? How does partition count relate to executor cores? When does repartitioning by a key column help a join?
A concrete example
A pipeline finishes with two thousand small partitions and writes two thousand tiny files to S3. The engineer calls coalesce to ten before writing, merging partitions without a shuffle for a cheap fix. Earlier, before a large skewed join, the same engineer uses repartition on the join key to spread data evenly and avoid stragglers.
Interview question
You need to reduce a DataFrame from 2000 partitions to 10 before writing output, minimizing network cost. Which is appropriate and why?
- a.repartition, because coalesce cannot reduce partition count
- b.Either one, since both perform identical work
- c.coalesce, because it merges partitions without a full shuffleCorrect
- d.repartition, because it avoids any shuffle when reducing
Why? this is the answer
coalesce merges existing partitions without a full shuffle, making it the cheaper way to reduce count. repartition would trigger a full shuffle, and coalesce can in fact reduce partitions.
Just read this? Test yourself on what you have been reading.
Read the original → bigdataperformance.substack.com
- #spark
- #repartition
- #coalesce
- #shuffle
- #partitioning
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles