According to Learning Spark
Keep in mind that repartitioning your data is a fairly expensive operation. Spark also has an optimized version of
repartition
- it's recommended to use it while increasing the number of partitions, because it involve shuffling of all the data.
coalesce
- it's is recommended to use it while reducing the number of partitions. For example if you have 3 partitions and you want to reduce it to 2, coalesce
will move the 3rd partition data to partition 1 and 2. Partition 1 and 2 will remains in the same container.
On the other hand, repartition
will shuffle data in all the partitions, therefore the network usage between the executors will be high and it will impacts the performance.
coalesce
performs better than repartition
while reducing the number of partitions.
But also you should make sure that, the data which is coming coalesce nodes should have highly configured, if you are dealing with huge data. Because all the data will be loaded to those nodes, may lead memory exception. Though reparation is costly, i prefer to use it. Since it shuffles and distribute the data equally.
Be wise to select between coalesce and repartition.