Apache Spark Execution Flow Explained
Published 2026-07-08 in PySpark
Apache Spark Execution Flow Explained One of the most common Spark questions is: "What happens internally when you run a Spark job?" Here's the complete execution flow: 1️⃣ Application Submission The user submits the application using "spark-submit", a Databricks Notebook, or a scheduled job. 2️⃣ Driver Initialization The Driver creates the "SparkSession" and "SparkContext", initializing the Spark application. 3️⃣ Resource Allocation The Cluster Manager allocates CPU, memory, and launches Executors on worker nodes. 4️⃣ Executor Registration The Executors start on worker nodes and register themselves with the Driver. 5️⃣ Logical Plan Creation The Spark SQL Parser converts the DataFrame/SQL code into an unresolved logical plan, and the Analyzer resolves table names, columns, and data types to produce a logical plan. 6️⃣ Query Optimization The Catalyst Optimizer applies optimization rules such as predicate pushdown, column pruning, constant folding, and join optimization to generate an optimized logical plan. 7️⃣ Physical Planning The Spark Planner generates one or more physical execution plans from the optimized logical plan, and Cost-Based Optimization (CBO) selects the most efficient one (when statistics are available). 8️⃣ DAG & Stage Creation The DAG Scheduler converts the physical plan into a DAG and splits it into stages at shuffle boundaries. 9️⃣ Task Creation The Task Scheduler divides each stage into tasks—typically one task per partition. 🔟 Task Scheduling The Task Scheduler assigns tasks to available Executors. 1️⃣1️⃣ Data Processing Each Executor reads only its assigned data partition from storage (ADLS, S3, HDFS, Blob Storage, etc.) into memory and executes the transformations. 1️⃣2️⃣ Shuffle (If Required) The Shuffle Manager redistributes data across Executors for operations like "groupBy", "join", "distinct", and "reduceByKey", ensuring records with the same key reach the same partition. 1️⃣3️⃣ Result Write The Executors write the final output back to the target storage. 1️⃣4️⃣ Job Completion The Driver marks the job as complete, and the Cluster Manager releases the allocated resources. 💡 Execution Flow: Application → Driver → Cluster Manager → Executors → SQL Parser → Analyzer → Catalyst Optimizer → Spark Planner → DAG Scheduler → Task Scheduler → Executors → Shuffle Manager (if needed) → Storage → Job Complete Understanding which component performs each responsibility is what separates someone who knows Spark APIs from someone who understands Spark internals.
More PySpark articles · All collections · Practice challenges
Practise what you just read: PySpark interview questions · online PySpark compiler · PySpark tutorials · PySpark cheat sheet.