Sub topic 7
Serverless compute for notebooks and jobs
This chapter assumes you know the driver and worker model of a Spark cluster, the difference between all-purpose and job clusters, and how to read a Jobs API definition. Serverless compute removes the cluster-sizing decisions you have made in the earlier chapters.
What serverless compute is
Serverless compute is compute that Databricks creates and manages in Databricks' own cloud account instead of in yours. You do not pick a node type, a runtime version, or a worker count. You attach a notebook to serverless compute, or you run a job task without naming a cluster, and Databricks supplies the resources.
Three things change compared with classic compute (all-purpose and job clusters running in your cloud account):
- Start time. Serverless compute is drawn from capacity that Databricks keeps ready, so it usually starts in seconds. Classic clusters must request virtual machines from your cloud provider first.
- Sizing. Databricks scales the resources automatically for the workload. There are no autoscaling bounds to set.
- Upgrades. Databricks manages the runtime version for you. You do not pin a
spark_version.
Serverless compute works only with Unity Catalog, so your data access is governed by Unity Catalog permissions. There is no access mode to choose.
Serverless for notebooks
In a notebook, open the compute selector and choose Serverless. The notebook connects without a startup wait. You pay only while the notebook is running work, and you do not manage auto termination. Serverless notebook compute shuts itself down after a period of inactivity.
Because you cannot change Spark settings freely, some things that work on a classic cluster are not available. Typical examples are custom cluster-level Spark configuration, init scripts, and some library installation methods. If your code depends on these, test it on serverless compute before you move to it.
Serverless for jobs
For a job, the difference is visible in the definition itself. Compare the job cluster definition from the previous chapters with this one:
{
"name": "nightly-etl-serverless",
"tasks": [
{
"task_key": "ingest",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}This definition has no job_clusters block and no job_cluster_key on the task. In a workspace where serverless compute for jobs is enabled, a notebook task that names no cluster runs on serverless compute. There is no node type, runtime version, or worker count to choose.
The run lifecycle is also simpler:
How serverless is billed
Serverless compute is billed in DBUs (Databricks Units) based on the work done, and the price already includes the underlying infrastructure. You do not receive a separate bill from your cloud provider for virtual machines. On classic compute you pay DBUs to Databricks plus infrastructure charges to your cloud provider. The cost chapter compares the two in detail.
This changes how you reason about cost:
- Idle time costs little, because resources are not held while nothing runs.
- The DBU rate per unit of work is usually higher than the classic DBU rate, because it covers infrastructure.
- Total cost can be lower or higher than a tuned classic cluster. Short, bursty, and unpredictable workloads tend to favor serverless. Long, steady, well-tuned jobs may be cheaper on a job cluster.
Common errors
- Assuming classic features exist. Init scripts, custom Spark configuration, and some libraries may not work. Run a test before migrating a production job.
- Expecting a cluster to tune. If a serverless run is slow, you cannot add workers. Look at the query itself, such as joins, file layout, and data volume.
- Forgetting the workspace setting. If a task with no cluster fails to start, check that serverless compute for jobs is enabled for the workspace.
- Mixing up billing. Serverless charges appear as Databricks usage only. Do not look for matching virtual machine charges in your cloud bill.
Exercise
A job definition has one notebook task and no job_clusters block, and the task has no cluster key. The workspace has serverless compute for jobs enabled. Where does the task run, and which charges appear for it?
Show answer
It runs on serverless compute, because a notebook task that names no cluster uses serverless in a workspace where it is enabled. The charges appear as DBU usage on the Databricks bill. There are no separate virtual machine charges in your cloud account, because the infrastructure is included in the serverless price.
Exercise
Your team has a long nightly job that runs for hours on a stable, well-tuned cluster and relies on an init script. Which compute type is the safer choice, and why?
Show answer
A job cluster is the safer choice. Init scripts may not be supported on serverless compute, and a steady, long-running, tuned workload often costs less on classic compute than at the serverless DBU rate. Test on serverless compute before deciding.