CASE STUDIES LIST/ HOME
← Case #13#14 / 25Case #15 →
CASE STUDY #14Phase 4: Spark & Distributed ProcessingCLASSIFICATION: EXPERIMENT

I Added More Partitions and Made the Pipeline Slower

Task Scheduling Overhead and Driver Bottlenecks in Distributed Spark Jobs

Apache SparkPySparkSpark UI

1. The Problem

Increasing Spark partitions from 200 to 5,000 on a 2 GB dataset caused pipeline execution time to increase from 45 seconds to 4.2 minutes.

2. What I Initially Thought

"I thought more partitions meant higher parallelism and faster execution across cluster worker cores."

3. What I Learned

Every Spark partition creates an individual JVM task sent by the Driver to executors. Creating 5,000 tasks for a small dataset meant task scheduling overhead exceeded actual processing time.

Task Scheduling OverheadPartition GranularityDriver BottleneckTarget Partition Size

4. What I Built

Dynamic partition sizing configuration based on dataset volume (`target_partitions = max(1, total_bytes / 128MB)`).

# Calculate optimal partition count dynamically
total_bytes = fs.getContentSummary(path).getLength()
target_partitions = max(4, int(total_bytes / (128 * 1024 * 1024)))
df = spark.read.parquet(path).repartition(target_partitions)

5. The Experiment

BEFORE

2 GB dataset partitioned into 5,000 partitions. Execution duration: 4.2 minutes (Driver overhead: 85%).

CHANGE APPLIED

Calculated optimal target partitions (16 partitions of ~128 MB each).

AFTER RESULT

Execution duration dropped from 4.2 minutes to 22 seconds; Driver scheduling overhead eliminated.

6. What Went Wrong

Confused Spark's `spark.sql.shuffle.partitions` default (200) with input file partitions, causing small datasets to spawn 200 empty shuffle tasks.

7. Engineering Decision & Trade-offs

Configured `spark.sql.adaptive.enabled=true` (AQE) to allow Spark to dynamically coalesce shuffle partitions at runtime.

8. What I Would Do Differently in Production

Enable Adaptive Query Execution (AQE) in Spark 3.x to automatically coalesce small shuffle partitions without hardcoding static partition counts.

Questions I Can Now Answer Confidently in an Interview:

  • Why does creating too many Spark partitions degrade pipeline execution speed?
  • How does Spark's Adaptive Query Execution (AQE) coalesce shuffle partitions dynamically?
  • What is the recommended target partition data size for optimal Spark processing?

Expected / Verified Evidence

•Spark UI Task execution timeline graph (5000 vs 16 tasks)
•AQE configuration script (pipelines/config/spark_session.py)
•Runtime benchmark metrics table
BACK TO ALL CASE STUDIESNEXT: CASE #15 (Replacing Network Shuffle With Broadcast Joins)