Sub topic 7
Apache Spark: distributed processing on clusters
This chapter assumes you know basic Python, SQL, and the pandas workflow, and that you have read the earlier chapters on execution models and columnar formats. It covers what Spark is good at, how it runs a query, and where it costs more than it returns.
What Spark is
Apache Spark is a distributed engine. A driver process plans the work. Many executors, which are worker processes spread across machines, run it. The data is split into partitions, and each executor processes some of them in parallel.
Spark is lazy. Transformations such as groupBy and filter only build a plan. Nothing runs until an action such as show(), count(), or a write asks for a result.
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.appName("sales").getOrCreate()
result = (
spark.read.parquet("sales.parquet")
.groupBy("region")
.agg(F.sum("revenue").alias("revenue"))
)
result.show()The first call that does real work is show(). Before it runs, Spark's optimizer (Catalyst) prunes unneeded columns and pushes filters down to the Parquet reader.
How a query runs
Spark cuts the plan into stages at points where data must move between machines. That movement is a shuffle. A group-by needs all rows for one region on the same executor, so Spark first aggregates within each partition, then shuffles the partial results, then combines them.
placeholderStrengths
- Data beyond one machine. Capacity grows by adding executors, so a table of many terabytes is a normal input.
- Fault tolerance. If an executor fails, Spark reruns the lost tasks from the plan.
- Data platform integration. Spark connects to object storage, table formats such as Delta Lake and Iceberg, streaming sources, and managed services such as Databricks, EMR, and Dataproc.
- One API across scales. The same DataFrame and SQL code runs on a laptop in local mode or on a large cluster.
Limits
- Startup cost. Starting a JVM and a session takes seconds. A cluster can take minutes to provision. A query that DuckDB answers in a fraction of a second can take far longer in Spark.
- Shuffle cost. Joins and group-bys move data over the network and often spill to disk.
- Operational work. Someone must size the cluster, tune partition counts, and watch for skewed keys, where one partition holds most of the rows and one task runs long after the others finish.
- Small data is slower. With a few gigabytes, coordination overhead usually outweighs any parallel gain.
Common mistakes
- Using Spark because the data "might grow," when it fits on one machine today.
- Calling
toPandas()on a large result. It collects everything onto the driver and can run it out of memory. - Leaving the default of 200 shuffle partitions on tiny or huge data without checking whether it fits.
Exercise
A team has a 15 GB Parquet dataset and a laptop with 32 GB of RAM. They propose Spark because it is "the scalable option." Give two costs they would take on, and say what you would try first.
Show answer
They would pay Spark's startup time and its shuffle and tuning overhead, and they would need to run or maintain a Spark environment. The data fits on one machine, so try DuckDB or Polars first, reading only the needed columns from Parquet. Move to Spark only if data growth or shared cluster infrastructure makes a single node insufficient.