Sub topic 1
How a Databricks cluster is built
A Databricks cluster is a set of virtual machines that run one Spark application together. Every sizing decision in later chapters depends on what each machine does and how its resources become units of parallel work. This chapter covers that structure.
Prerequisites
This chapter assumes you already know the following:
- Basic Spark concepts: jobs, stages, tasks, partitions, and shuffles.
- How to run a notebook or job in a Databricks workspace.
- What a cloud virtual machine instance type is, and how on-demand and spot pricing differ.
The two roles: driver and workers
A cluster has one driver node and zero or more worker nodes. All nodes are ordinary cloud instances that Databricks starts, configures with a Databricks Runtime image, and joins into a Spark cluster.
The driver node does the coordination work:
- It runs the Spark driver process, which holds the SparkSession and builds query plans.
- It splits each job into stages and tasks, and sends those tasks to executors.
- It hosts the notebook's interpreter state, including your Python variables and temporary views.
- It receives results from actions such as
collect()ortoPandas().
Worker nodes do the data processing. Each worker runs a Spark executor, which reads data, runs tasks, caches partitions, and writes shuffle files. In a multi-node cluster the driver does not run tasks, so its cores add no processing capacity.
From nodes to executors to task slots
Databricks normally runs one executor per worker node, and that executor can use the node's cores and most of its memory. This differs from some self-managed Spark setups, where you might run several smaller executors per machine.
The chain of resources is:
- A worker node has a number of vCPUs and an amount of RAM, set by its instance type.
- The executor on that node exposes one task slot per core. By default each task needs one core (
spark.task.cpusis 1). - A task slot runs one task at a time, and a task processes one partition.
The cluster's total task slots are therefore the sum of cores across all workers. Suppose you have 4 workers of 4 vCPUs each. The cluster then has 16 task slots, so at most 16 tasks run at the same moment.
Memory on a worker
A node's RAM is not all available to your tasks. Part of it goes to the operating system and Databricks services. Spark then divides the executor's share into regions:
- Execution memory holds data for joins, sorts, aggregations, and shuffles.
- Storage memory holds cached data and broadcast variables.
- Overhead memory covers off-heap use, such as Python worker processes in PySpark.
The execution and storage regions share a pool and can borrow from each other. Tasks running in parallel also split the execution memory. A node with 4 cores gives each running task roughly a quarter of it. Choosing an instance with more cores but the same RAM per core keeps the per-task memory unchanged. Choosing one with more cores and less RAM per core makes spills to disk more likely.
The driver is a single machine
Worker count can grow, but the cluster has only one driver. Several things can overload it:
- Collecting a large result into the driver with
collect()ortoPandas(). - Broadcasting a large table, because the driver first gathers it.
- Listing very large numbers of files in a table.
- Many users sharing one cluster, since all of their notebook state lives on the same driver.
- Running Python or pandas logic on the driver instead of in Spark.
Databricks lets you choose the driver's instance type separately from the workers' type. If the driver is under pressure and the workers are idle, the driver is the bottleneck, so a bigger driver fixes it. Adding workers does not.
If a driver fails, the whole Spark application ends and notebook state is lost. If a worker fails, Spark reschedules its tasks on other executors and recomputes lost shuffle data. A worker loss costs time. A driver loss costs the whole run.
A cluster specification
The Clusters API describes a cluster as JSON. This spec sets a fixed number of workers and a separate driver type:
{
"cluster_name": "etl-fixed",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"driver_node_type_id": "i3.2xlarge",
"num_workers": 4,
"autotermination_minutes": 30
}Here node_type_id sets the worker instance type. driver_node_type_id overrides the driver type. If you omit it, the driver uses the worker type. With num_workers set to 4 and i3.xlarge workers (4 vCPUs each), the cluster has 16 task slots. The later chapters replace num_workers with an autoscale range.
Single node and multi-node clusters
Setting num_workers to 0 gives a single node cluster. The driver then runs tasks itself, so its cores and memory are the only compute. This suits small data, exploration, and single-machine libraries. It cannot scale out, and one process handling everything means a heavy job competes with notebook state for memory.
A multi-node cluster has at least one worker. The driver and workers are separate machines, and the driver's cores stay out of the task slot count.
Checking what you actually have
You can check the size of a running cluster from a notebook. This cell prints the slot count and the memory settings the cluster reports:
total_slots = spark.sparkContext.defaultParallelism
conf = spark.sparkContext.getConf()
executor_memory = conf.get("spark.executor.memory", "not set")
driver_memory = conf.get("spark.driver.memory", "not set")
print(f"Default parallelism (approx. task slots): {total_slots}")
print(f"Executor memory: {executor_memory}")
print(f"Driver memory: {driver_memory}")On a multi-node cluster, defaultParallelism is usually the total core count across workers. On a single node cluster it reflects the driver's cores. Some access modes, including shared and serverless compute, do not expose sparkContext, so this cell fails there. In that case, read the cluster's configuration page or the Spark UI Executors tab.
Common mistakes
| Mistake | Effect | Better approach |
|---|---|---|
| Counting the driver's cores as capacity | Overestimates parallelism in a multi-node cluster | Count worker cores only |
| Adding workers when the driver is saturated | Cost rises and speed does not | Check driver load, then enlarge the driver |
| Treating a node's full RAM as task memory | Spills and out-of-memory errors | Divide executor memory by concurrent tasks |
| Using a single node cluster for large scans | One machine does all the reading | Use workers for distributed data |
What comes next
The next chapter links this structure to workload types. ETL, interactive analysis, machine learning, and BI queries stress the driver, the task slots, and the memory in different ways, so they call for different shapes of cluster.
Exercise
A multi-node cluster has 6 workers, each with 8 vCPUs and 64 GB of RAM, and a driver with 16 vCPUs. How many tasks can run at once, and why does adding more driver cores not change that number? A notebook then fails with an out-of-memory error while worker utilization stays near zero. Which node would you resize first?
Show answer
The cluster has 6 × 8 = 48 task slots, so up to 48 tasks run at once. The driver schedules tasks and holds notebook state but does not run tasks in a multi-node cluster, so its 16 cores add no slots. Low worker utilization together with an out-of-memory error points to the driver, for example from a large collect() or toPandas(). Resize the driver first, using driver_node_type_id, and consider reducing the data returned to it.
Exercise
Why can two clusters with the same total core count behave differently on a memory-heavy join?
Show answer
The memory available to each concurrent task depends on RAM per core. A cluster of nodes with less RAM per core gives each task less execution memory, so it spills to disk sooner. The two clusters can also differ in how many shuffle files are lost when a single node fails.