Sub topic 11
What distribution costs: shuffles, startup, and operations
This chapter assumes you can read a Spark DataFrame query, know what a group-by does, and have read the earlier chapters on execution models and the Spark profile. Spark spreads work across many machines. That buys capacity, and it adds costs that a single-node tool never pays.
Where the costs come from
A distributed engine splits data into partitions and sends them to executors, which are worker processes on different machines. Three kinds of cost follow from that.
| Cost | When you pay it | Typical effect |
|---|---|---|
| Startup | Every job or session | Seconds to minutes before work begins |
| Shuffle | Group-by, join, sort, distinct | Network and disk traffic between executors |
| Operations | Always | Cluster sizing, tuning, monitoring, upgrades |
Startup
Creating a Spark session starts a driver, requests executors, and loads the JVM (Java Virtual Machine) runtime. On a managed cluster, waiting for nodes can add minutes. A pandas, Polars, or DuckDB query starts in milliseconds because it runs in your existing Python process. For a job that takes two seconds of real work, Spark's startup can be most of the wall-clock time.
Shuffles
A shuffle redistributes rows across the network so that rows with the same key land on the same executor. A group-by on region is a good example. Each executor first computes partial sums on its own partitions. Spark then shuffles those partial results by key, and the final sums are computed after the shuffle.
A shuffle is expensive because it writes intermediate data to disk, sends it over the network, and waits for the slowest executor. Joins are worse, since both sides may be shuffled. Skew makes it worse again. If one key holds most of the rows, one executor does most of the work while the others sit idle.
You can see shuffles in the plan. Look for Exchange nodes in the output of explain():
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.appName("shuffle-demo").getOrCreate()
result = (
spark.read.parquet("sales.parquet")
.groupBy("region")
.agg(F.sum("revenue").alias("revenue"))
)
result.explain()Each Exchange line in the physical plan marks a shuffle. Fewer exchanges usually means a faster job.
Operations
A cluster is infrastructure you run. Someone chooses instance types, sets memory and partition counts, watches for failed or straggling tasks, and pays for idle nodes. Managed services reduce this work but do not remove it. Compare that with pip install duckdb, which needs no servers at all.
What you get in return
Distribution pays off when the data or the compute needs exceed one machine. Spark can process datasets far larger than one node's disk, and it recovers from failed executors by recomputing lost partitions. If your data fits on one machine, you pay these costs without receiving that benefit.
Exercise
A job aggregates 20 GB of Parquet on a machine with 64 GB of RAM. Name two costs Spark adds compared with DuckDB here, and say what Spark would give you in return.
Show answer
Spark adds startup time (driver, JVM, executors) and shuffle overhead for the group-by, plus the operational work of running a cluster. In return it gives horizontal scale and fault tolerance, which this job does not need because 20 GB fits comfortably on one machine.