Sub topic 2
How a cluster is built: driver, workers, and Spark
This chapter assumes you know what a Databricks notebook is, and that you have met Spark terms such as driver, executor, and job. It explains how those pieces map onto the virtual machines in a Databricks cluster. Later chapters build on this model.
The cluster as a set of machines
A Databricks cluster is a group of virtual machines (VMs) that run Apache Spark together. Databricks starts the VMs in your cloud account, installs the Databricks Runtime on them, and joins them into one Spark cluster. There are two roles:
- The driver node runs the Spark driver process. It holds the notebook's state, builds the query plan, splits work into tasks, and collects results.
- The worker nodes each run one Spark executor. Executors run the tasks the driver assigns and keep cached data and shuffle files.
The driver does not run tasks on your data in a multi-node cluster. It coordinates. A cluster with 4 workers has 5 VMs in total.
What each role needs
The two roles stress different resources, so it helps to know what loads them.
| Role | Does | Runs out of memory when |
|---|---|---|
| Driver | Plans queries, tracks tasks, holds results from `collect()` or `toPandas()` | You pull a large result into the notebook, or you keep many large Python objects |
| Worker | Reads data, runs transformations, shuffles, caches | Partitions are too large, or a join or aggregation spills past available memory |
By default the driver and workers use the same node type. You can choose a different driver node type when the driver needs more memory than the workers do. A common case is a job that returns large results to the driver.
Cores, tasks, and parallelism
Each worker VM has a number of CPU cores. Spark runs one task per core at a time. The total number of cores across workers is the cluster's maximum task parallelism. For example, 4 workers with 4 cores each can run 16 tasks at once.
If a stage has 200 tasks, Spark runs them in waves of 16. Adding workers raises the number of tasks that run at once. Adding workers does not help if a stage has only a few tasks or one very large partition. That case is called skew, and it leaves most cores idle.
Single node versus multi-node
A cluster can also be single node. In this mode there are no workers. The driver VM runs Spark in local mode and executes the tasks itself. Single node suits small data, library testing, and single-machine tools such as scikit-learn. It cannot scale out, so a large dataset will be limited by one machine.
Seeing the structure in a configuration
The cluster definition you will see in the Clusters API mirrors this model. The autoscale block sets how many workers the cluster may have, and the driver is added on top.
{
"cluster_name": "team-interactive",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 1,
"max_workers": 4
},
"autotermination_minutes": 60,
"data_security_mode": "USER_ISOLATION"
}This request body is for POST /api/2.1/clusters/create. Here is how it maps to the model:
node_type_idsets the VM type for the workers. The driver uses the same type because nodriver_node_type_idis given. The valuei3.xlargeis an AWS example, and Azure and GCP use different names.autoscaleallows 1 to 4 workers, so the cluster has between 2 and 5 VMs once the driver is counted.spark_versionselects the Databricks Runtime, which is covered in the next chapter.
Common mistakes
- Using
collect()on a large DataFrame. This moves all rows to the driver and can crash it. Uselimit(),display(), or write results to a table. - Adding workers to fix a slow single-threaded Python loop. Plain Python code runs on the driver only. Only Spark operations are spread across workers.
- Forgetting that the driver counts toward cost and capacity.
Exercise
A cluster has 3 workers, each with 8 cores, and one driver. How many VMs does it have, and how many tasks can Spark run at the same time? Why does a plain Python for loop in a notebook not use those tasks?
Show answer
It has 4 VMs (1 driver plus 3 workers). Spark can run 24 tasks at once (3 workers times 8 cores). A plain Python loop runs in the driver process, not as Spark tasks, so the workers stay idle. Only Spark operations such as DataFrame transformations are distributed to executors.