Spark Architecture and Execution Model
Learn how Spark runs: driver and executors, lazy evaluation, jobs, stages and tasks, the Catalyst optimizer and how to read the Spark UI.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Distributed Processing Concepts, Production-Grade Python
Apache Spark is the most widely used engine for large-scale batch processing and appears in most Indian data engineering job descriptions, often through Databricks, AWS EMR or GCP Dataproc.
The pieces
- Driver: runs your program, builds the plan and schedules work.
- Executors: JVM processes on worker nodes that run tasks and cache data.
- Cluster manager: YARN, Kubernetes or Spark standalone allocates resources. On Databricks this is managed for you.
- SparkSession: your entry point in PySpark.
Lazy evaluation
Transformations (filter, select, join) only build a plan. Nothing runs until an action (count, show, write, collect). This lets Spark optimise the whole pipeline at once.
Jobs, stages, tasks
- Each action triggers a job.
- A job is split into stages at shuffle boundaries.
- Each stage has one task per partition.
The Spark UI (port 4040 locally) shows all of this, plus shuffle sizes, task durations and spills. Learning to read it is essential.
Catalyst and Tungsten
The DataFrame API and Spark SQL go through the Catalyst optimizer (predicate pushdown, column pruning, join reordering) and Tungsten (efficient memory and code generation). This is why DataFrames are much faster than the old RDD API. Adaptive Query Execution (AQE) re-optimises at runtime using real statistics.
Pitfalls
- Calling
collect()on a large DataFrame and crashing the driver. - Using RDDs and Python UDFs where built-in DataFrame functions exist.
- Reading the code and assuming it runs line by line; it does not.
You are ready to move on when you can run a PySpark job locally, open the Spark UI, and explain how many jobs, stages and tasks it produced and why.
Key concepts
- Driver, executors, cluster manager
- SparkSession
- Transformations vs actions
- Jobs, stages, tasks
- Spark UI
- Catalyst optimizer
- Adaptive Query Execution
- RDD API: Mostly legacy; know it exists
Mini project
Install PySpark locally (or use Databricks Free Edition or Google Colab). Run a job that reads Parquet, filters, groups and writes output. Screenshot the Spark UI and annotate the stages, shuffle sizes and slowest task.
Check yourself
You write ten transformations and then call count(). How many jobs run, and when does Spark actually read the data?
Free resources
- Apache Spark documentation (Apache Spark)
- Spark cluster mode overview (Apache Spark)
- Spark web UI (Apache Spark)