Sub topic 1
How a Databricks Cluster Is Built
Prerequisites
This chapter assumes you know basic Spark concepts (jobs, stages, tasks, partitions, shuffles). It also assumes you can run notebooks or jobs in a Databricks workspace and know what a cloud virtual machine instance type is.
The driver and the workers
A Databricks cluster is a set of cloud virtual machines that work together. One machine is the driver. The others are workers.
- The driver runs your notebook or job code, builds the Spark execution plan, and splits each stage into tasks. It also holds results that you collect back to it.
- Workers run the tasks. Each worker hosts one Spark executor, which is a process that holds data in memory and runs tasks in parallel.
The driver does not run tasks when the cluster has workers. A cluster with zero workers is called single node. There the driver does everything, and that mode suits only small jobs.
From cores to task slots
Each executor offers one task slot per CPU core. A task slot runs one task at a time. So total parallelism is roughly the number of worker cores. Three workers with 4 cores each give 12 task slots.
Memory is shared the same way. An executor's memory is split among its running tasks. More cores per node means more tasks share the same memory, so a memory-heavy job may need fewer cores per gigabyte, not more.
You can check what your cluster offers from a notebook:
total_cores = spark.sparkContext.defaultParallelism
shuffle_partitions = spark.conf.get("spark.sql.shuffle.partitions")
aqe_enabled = spark.conf.get("spark.sql.adaptive.enabled")
print(f"Default parallelism (approx. total cores): {total_cores}")
print(f"Shuffle partitions: {shuffle_partitions}")
print(f"Adaptive query execution enabled: {aqe_enabled}")If a stage has 200 partitions and the cluster has 12 slots, Spark runs the tasks in about 17 rounds. More slots means fewer rounds, up to the point where slots outnumber partitions and the extra cores sit idle.
What you configure
Two choices define the shape of a cluster: the node type and the worker count. The node type sets cores, memory, and local storage per machine. The worker count can be fixed or can scale between a minimum and a maximum. The driver and workers may use different node types, which helps when the driver needs more memory to collect large results.
This is a minimal specification in the Clusters API format:
{
"cluster_name": "etl-autoscale",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"autotermination_minutes": 30
}Here autoscale replaces a fixed num_workers, and the cluster shuts down after 30 idle minutes. Later chapters cover each of these settings in detail.
Exercise
A cluster has 1 driver and 5 workers. Each worker has 8 cores. How many task slots does it offer, and how many rounds does a stage with 200 tasks need at minimum?
Show answer
The 5 workers give 5 x 8 = 40 task slots. The driver adds none. A stage with 200 tasks needs 200 / 40 = 5 rounds.