Skip to the page

PySpark interview question · easy

What is the difference between `repartition` and `coalesce` in Spark?

The short answerIn Spark, repartition and coalesce are used to change the number of partitions in a DataFrame or RDD.

The full answer

In Spark, repartition and coalesce are used to change the number of partitions in a DataFrame or RDD. However, they serve different purposes and have distinct behaviors depending on whether you want to increase or decrease the number of partitions.

Repartition:

  • Used to increase or decrease the number of partitions.
  • Performs a full shuffle of the data across the cluster to evenly distribute it into the specified number of partitions.
  • Typically used when increasing the number of partitions for parallelism or balancing partition sizes after transformations like joins or aggregations.
  • Offers better performance for increasing partitions but is more expensive due to the shuffle.

Example:

df = df.repartition(100)  # Increase to 100 partitions

Coalesce:

  • Used to reduce the number of partitions.
  • Avoids a full shuffle by consolidating adjacent partitions without redistributing data across the cluster.
  • Best suited for optimizing partitions after filtering or when the data is already co-located.
  • More efficient for reducing partitions, but it may result in imbalanced partitions.

Example:

df = df.coalesce(10)  # Reduce to 10 partitions

Key Differences:

  • repartition performs a full shuffle and can increase or decrease partitions, whereas coalesce minimizes shuffling and is limited to reducing partitions.
  • coalesce is generally more efficient for reducing partitions, while repartition is better when a uniform distribution of data across partitions is required.

Code Example Comparing Both:

from pyspark.sql import SparkSession

# Initialize SparkSession
spark = SparkSession.builder.appName("Repartition vs Coalesce").getOrCreate()

# Sample DataFrame
data = [("Alice", 30), ("Bob", 35), ("Cathy", 29)]
columns = ["name", "age"]
df = spark.createDataFrame(data, columns)

# Repartition: Increase partitions to 10
repartitioned_df = df.repartition(10)

# Coalesce: Reduce partitions to 2
coalesced_df = repartitioned_df.coalesce(2)

repartitioned_df.show()
coalesced_df.show()

# Stop SparkSession
spark.stop()

Nuances to Consider:

  • Use repartition when you need a balanced distribution of data across partitions, especially after wide transformations like join or groupByKey.
  • Use coalesce when reducing partitions for performance optimization, such as before writing to a file or when data is already sorted.
  • Avoid excessive partition reduction with coalesce, as it may lead to skewed partitions and resource underutilization.

Tecyfy Takeaway:
repartition and coalesce are essential tools for managing partitions in Spark. repartition is versatile for both increasing and decreasing partitions but involves a full shuffle, while coalesce is efficient for reducing partitions without a shuffle. Choose based on your need for performance or balanced partitioning.

Last updated 24 January 2025