🔍 Ctrl+K
🟢 Easy

What are Partitions?

Spark divides a dataset into partitions so that data can be processed in parallel. Each partition is a logical chunk of data that is processed by a single task.

Dataset Partitioning Overview
Dataset
Partition 1
Partition 2
Partition 3
Partition 4

Key relationships:

  • 1 Partition → 1 Task — Each partition is processed by exactly one task.
  • 1 Executor Core → 1 Task at a time — A core can process one partition concurrently.
  • More partitions = more parallelism (but also more overhead).

Default partition count: When you read a file, Spark creates one partition per HDFS block (default 128 MB) or one partition per file. When using DataFrames, the default number of partitions is determined by spark.sql.shuffle.partitions (default 200).

💡 Interview Tip
The number of partitions determines the level of parallelism. Too few partitions underutilize resources; too many create overhead.
🟢 Easy

Horizontal Partitioning

Divides rows across partitions. Each partition contains a subset of the total rows.

Horizontal Partitioning
Original Dataset
Row 1 Row 2 Row 3 Row 4 Row 5 Row 6
After Horizontal Partitioning
Partition 1
Row 1 Row 2
Partition 2
Row 3 Row 4
Partition 3
Row 5 Row 6

Use case: Used by default in Spark for parallel processing of large datasets.

🟢 Easy

Vertical Partitioning

Divides columns across partitions. Each partition contains a subset of the total columns.

Vertical Partitioning
Original Dataset
ID Name Age Salary Dept
After Vertical Partitioning
Partition 1
ID Name Age
Partition 2
ID Salary Dept

Use case: Useful when you frequently access only a subset of columns.

🟠 Intermediate

Hash Partitioning

Determines partition assignment by applying a hash function to the partition key.

partition = hash(key) % number_of_partitions
Hash Partitioning Flow
Customer ID
Hash Function
Hash Value
Modulo Partitions
Partition 0
Partition 1
Partition 2
Partition 3

Records with the same key are sent to the same partition, ensuring co-location of related data.

Python
from pyspark.sql.functions import hash, col

df = spark.createDataFrame([
    ("Alice", 100), ("Bob", 200), ("Charlie", 300)
], ["name", "amount"])

# Hash partition by name
df.repartition(4, "name")

Use case: Commonly used for joins and aggregations to co-locate related data.

💡 Interview Tip
Hash partitioning ensures records with the same key end up in the same partition, which is crucial for efficient joins.
🟠 Intermediate

Range Partitioning

Divides data according to value ranges. Each partition covers a specific range of values.

Range Partitioning
Partition 1
0 – 100
Partition 2
101 – 200
Partition 3
201 – 300
Python
from pyspark.sql.functions import col

# Range partition by age
df.repartitionByRange(3, col("age"))

Use case: Useful for range-based queries and maintaining sorted order.

💡 Interview Tip
Range partitioning is useful when you need data sorted within partitions for efficient range queries.
🟢 Easy

Round-Robin Partitioning

Records are distributed sequentially across partitions in a circular manner.

Round-Robin Distribution
Records
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Partitions
P1: R1
P2: R2
P3: R3
P1: R4
P2: R5
P3: R6
Python
# Round-robin distribution (default repartition behavior)
df = df.repartition(4)

Use case: Used when there is no specific partitioning key and you want even distribution.

💡 Interview Tip
Round-robin ensures even data distribution but doesn't preserve key-based grouping.
🔴 Advanced

Custom Partitioning

Custom partitioning allows developers to define their own partitioning logic according to application requirements.

Python
from pyspark.sql.functions import udf
from pyspark.sql.types import IntegerType

def custom_partitioner(key):
    # Custom logic to determine partition
    if key.startswith("A"):
        return 0
    elif key.startswith("B"):
        return 1
    else:
        return 2

Use case: When standard partitioning strategies don't meet your specific data distribution needs.

🟠 Intermediate

Repartition vs Coalesce

Feature Repartition Coalesce
OperationFull shufflePartial shuffle
Increase PartitionsYesNo
Decrease PartitionsYesYes
Data DistributionEvenMay be uneven
PerformanceSlowerFaster
Use CaseIncrease parallelismReduce partitions
Python
# Repartition - full shuffle, creates new partitions
df = df.repartition(10)

# Coalesce - no shuffle, merges existing partitions
df = df.coalesce(2)
💡 Interview Tip
Use coalesce to reduce partitions (faster, no shuffle) and repartition to increase partitions (full shuffle, even distribution).