# 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. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/data-engineer/spark-architecture
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/data-engineer/distributed-processing), [Production-Grade Python](https://www.internhack.in/roadmaps/tracks/data-engineer/python-production)

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](https://spark.apache.org/docs/latest/) (Apache Spark)
- [Spark cluster mode overview](https://spark.apache.org/docs/latest/cluster-overview.html) (Apache Spark)
- [Spark web UI](https://spark.apache.org/docs/latest/web-ui.html) (Apache Spark)

Previous: [Distributed Processing Concepts](https://www.internhack.in/roadmaps/tracks/data-engineer/distributed-processing) · Next: [PySpark DataFrames](https://www.internhack.in/roadmaps/tracks/data-engineer/pyspark-dataframes)

## More in Batch Processing with Apache Spark

- [Distributed Processing Concepts](https://www.internhack.in/roadmaps/tracks/data-engineer/distributed-processing)
- [Spark Architecture and Execution Model](https://www.internhack.in/roadmaps/tracks/data-engineer/spark-architecture)
- [PySpark DataFrames](https://www.internhack.in/roadmaps/tracks/data-engineer/pyspark-dataframes)
- [Joins and Shuffles in Spark](https://www.internhack.in/roadmaps/tracks/data-engineer/spark-joins)
- [Spark Performance Tuning and Operations](https://www.internhack.in/roadmaps/tracks/data-engineer/spark-tuning)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Data Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/data-engineer)
