Architecture
Cluster
Section titled “Cluster”- A cluster consists of one or more nodes
- Consists of Cluster Manager, Executors
- An Executor is a dedicated JVM process running on a node. A node may run more than one JVM
- Instead of a single JVM in a large node, it’s often useful to run multiple JVMs for
- too many cores per executor lead to large GC pauses
- too few cores restrict shared memory (Java heap) amongst tasks
- typically 5 cores / executor is considered good value
- A Slot is logical execution unit, a single thread that can be used to run a task
- The number of available slots are determined by the number of cores available to the executor
Spark Application
Section titled “Spark Application”- A Spark Application runs one or more jobs (mostly) sequentially but some can be run concurrently
- A Job is a series of transformations and are triggered each time an action is called, such as
.save()or.collect(). - A Stage, is segment of job that runs without a Data Shuffle.
- Stages run sequentially
- A Task is smallest unit of work and unit of parallelism and needs a slot to run
SparkSessionis interface to the driver process- A script using DataFrame API may result in 1+ Spark jobs, whereas using RDD results in a single job
- 3 ways to run a Spark job
- local: both drivers and workers run locally
- client: driver runs locally and workers run on remote nodes
- cluster: both drivers and works run on remote nodes
- Unless when dynamic allocation is enabled (
spark.dynamicAllocation.enabled = true), an application requests number of executors (spark.executor.instances = <n>) and number of slots per executor (spark.executor.cores = <m>). The total slots requested are<n> * <m> - If number of slots requested is too high, the application runs in multiple Waves
Adaptive QUery Execution
Section titled “Adaptive QUery Execution”Modern Spark runtimes leverage Adaptive Query Execution (AQE).
- Post-Shuffle Partition Coalescing: You no longer need to guess how many shuffle partitions your query requires. Spark starts with a higher default, and AQE tracks data sizes mid-execution. If it realizes your shuffle data shrank, it automatically combines small partitions into a smaller, optimal number of tasks at runtime.
- Dynamic Joins: Spark dynamically changes join strategies (e.g., switching from a Sort-Merge Join to a Broadcast Hash Join) if it notices one side of a dataset is small enough to fit into memory, drastically changing how tasks are distributed across available executor slots.
| Resource Metric | Traditional Open-Source Spark | Modern Cloud / Databricks Setup |
|---|---|---|
| Executors | Hardcoded via spark.executor.instances | Managed dynamically by Enhanced Auto-scaling |
| Cores / Memory | Calculated manually (—executor-cores) | Abstracted via Serverless or Worker Instance Types |
| Parallelism / Tasks | Hardcoded via spark.sql.shuffle.partitions | Controlled natively via Adaptive Query Execution (AQE) |
Cloud-native Spark applications
Section titled “Cloud-native Spark applications”Modern cloud data platforms like Databricks don’t depend on application specified parallelism parameters
- Serverless Compute & Automated Sizing: control plane calculates your dataset’s volume and dynamically provisions the compute mid-query.
- High-Level Compute Sizing: When configuring managed applications or workflow environments, Databricks utilize predefined Instance Size Tiers
- Enhanced Auto-scaling & Node Boundaries: for managed data pipelines (such as Lakeflow Spark Pipelines), you specify boundaries rather than a fixed number of instances:
- Min and Max Workers: You provide a safe range (e.g., Min: 2, Max: 50).
- Aggressive Scale-Down: Modern Enhanced Auto-scaling monitors exact query stages. It eagerly shuts down underutilized machines to save costs as soon as a data-heavy stage completes.
- Scale to Zero: Modern pipelines can scale down to zero executors when idle, instantly pausing cloud spend. [4, 6, 7, 8]
DataFrame/RDD
Section titled “DataFrame/RDD”- DataFrame
- higher-level, newer API
- resides in
org.apache.spark.sql.*namespace - uses tungsten to optimize memory and CPU utilization at stage level to minimize overhead of JVM GC
RDD- low-level APIs
- resides in
org.apache.spark.*
- Data abstractions: DataFrame, Dataset, SQL Table and RDD (Resilient Distributed Data)
- are immutable
- DataFrame have types checked at run-time and are of type Row
- DataSet available in Scala as
case classor with JavaBeans - DataFrame and DataSet are converted to RDD during physical planning
- Operations consist of either transformations or actions
- Transformations can be narrow (doesn’t require shuffle) or wide (requires shuffle)
- Actions, spawn jobs, jobs consists of stages, stages spawn tasks
- Tasks = Slots = Cores
- Data Types
- Data can be Row object or can be represented by Encoders (available only as Scala case class or Java beans)
- Data locality:
PROCESS_LOCAL>NODE_LOCAL>RACK_LOCAL>NO_PREF- affects wait times depending on how local the data is. Increase wait times for far-off data
- From Catalyst optimizer pov, RDDs are schema-less, and hence can’t be optimized
- From Scala’s pov, DataFrame is untyped, whereas RDD and DataSet are typed
Dataframes Execution
Section titled “Dataframes Execution”- Dataframes get optimized by Catalyst to create a Physical Plan
- Tungsten uses Whole-Stage Code Generation to
- translate generic Scala/Python object into compact raw bytecode
- use off-heap binary memory to store data
- Compiled bytecode is distributed across cluster as RDD, but unlike regular RDDs,
- aren’t used for manipulating data using traditional transformations (
map,filteretc) - are used to define execution DAG and split data into partitions
- in the UI they appear as RDDs with internal names such as (
WholeStageCodegen,Exchange,MapPartitionsRDD)
- aren’t used for manipulating data using traditional transformations (
flowchart TD
DataFrame --> ULP[Unresolved Logical Plan]
SQL[SQL Query] --> ULP
ULP --Ananlysis--> LP[Logical Plan]
Catalog --Ananlysis--> LP
LP --Logical Optimization--> OLP[Optimized Logical Plan]
OLP --Physical Planning--> PP@{ shape: st-rect, label: Physical Plans }
PP --> CM[Cost Model]
CM --> SPL[Selected Physical Plan]
SPL --Code Generation--> RDD