Sub topic 7
Serverless compute for notebooks and jobs
This chapter assumes you know the Databricks workspace and notebooks, basic Spark concepts (driver, executors, jobs), and how pay-as-you-go cloud billing works. You should also have read the earlier chapters on all-purpose and job clusters, because serverless is easiest to understand as a contrast with them.
What serverless compute is
Serverless compute is compute that Databricks creates, sizes, and manages in Databricks' own cloud account. You do not pick a node type, a runtime version, a worker count, or an autoscaling range. You attach a notebook or a job task to serverless compute, and Databricks supplies the resources.
Classic compute (all-purpose and job clusters) runs in virtual machines in your cloud account. You choose the configuration, wait for the machines to start, and pay the cloud provider for them in addition to the Databricks charge. Serverless moves that infrastructure to Databricks, so you pay a single usage-based charge.
| Aspect | Classic compute | Serverless compute |
|---|---|---|
| Where VMs run | Your cloud account | Databricks-managed account |
| Configuration you choose | Node type, runtime, workers, access mode, policies | Mostly none; some workload settings such as environment and performance mode |
| Start time | Minutes | Seconds in typical cases |
| Idle management | You set auto termination | Databricks manages capacity |
| Billing | DBUs plus cloud infrastructure | Serverless DBUs only |
The exact feature set, supported languages, and defaults change over time and differ by cloud and region. Check the current Databricks documentation for what your workspace supports.
Serverless for notebooks
For interactive work, you open a notebook and choose serverless from the compute selector instead of choosing a cluster. Typical behavior:
- The notebook connects in seconds, without waiting for VMs to boot.
- The compute is tied to your notebook session. It is not a shared, named resource that several people attach to.
- Databricks picks the runtime. You select an environment version instead of a Databricks Runtime version, and you can add Python libraries through the notebook's environment settings.
- Data access is governed by Unity Catalog. Serverless compute behaves like standard access mode: queries run with the identity of the user, and Unity Catalog permissions apply.
Because there is no cluster to configure, there are also no cluster-level Spark settings to tune. Only a limited set of Spark configuration properties can be changed. Code that relies on setting arbitrary Spark configs, on installing init scripts, or on low-level access to the driver machine may not work.
Serverless for jobs
For scheduled and automated work, serverless is the default shape of a task when you specify no cluster. In a workspace where serverless compute for jobs is enabled, a notebook task that names no cluster runs on serverless compute. The definition is shorter than a job-cluster definition, because there is nothing to declare.
{
"name": "nightly-etl-serverless",
"tasks": [
{
"task_key": "ingest",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}Compare this with the job-cluster version of the same task from the previous chapter:
{
"name": "nightly-etl",
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2,
"data_security_mode": "USER_ISOLATION"
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}The second definition has a job_clusters block, a runtime string, a node type, and a worker count. Each of those is a decision you own, and each can be wrong for the workload. The serverless definition removes all of them.
Two details are worth noting. First, the node type in the classic example (i3.xlarge) is an AWS example, and it differs on Azure and GCP. A serverless definition has no such cloud-specific field. Second, the "no cluster means serverless" behavior depends on the workspace setting. If serverless compute for jobs is not enabled, a task with no compute specified is not valid in the same way, so check your workspace before relying on it.
How the lifecycle differs
Classic compute has an explicit lifecycle you manage: create, start, run, idle, terminate. Serverless hides most of it.
The diagram below compares the two paths a job run takes.
The main practical effect is start-up latency. A job cluster commonly spends several minutes acquiring and booting VMs before the first line of your code runs. For a short task that runs every few minutes, that overhead can exceed the useful work. Serverless starts much faster, so short and frequent tasks benefit most.
Autoscaling and sizing without configuration
On classic compute you set min_workers and max_workers, and you set autotermination_minutes on all-purpose clusters. Serverless has no equivalent fields in the notebook or job definition. Databricks scales resources automatically while the workload runs and releases them when it finishes.
This removes two common mistakes: leaving an all-purpose cluster running overnight, and setting a maximum worker count too low for a heavy run. It also removes your direct control. If a query needs more memory than serverless provides, you cannot choose a bigger node type. You change the code, or you move that workload to classic compute.
Cost model
Serverless billing has one component: serverless DBUs, a Databricks unit of processing usage. The cloud infrastructure cost is bundled into the serverless DBU rate, so you do not receive a separate bill from your cloud provider for these VMs.
Classic compute has two components:
- DBUs, billed by Databricks according to the compute type and the node's DBU rate per hour.
- Cloud infrastructure, billed by your cloud provider for VMs, disks, and related resources.
The serverless DBU rate is higher than the classic DBU rate for the same kind of work, because it includes the infrastructure. A fair comparison therefore needs the full classic cost, not just the DBU line.
Billing for serverless is based on the compute actually used while your work runs. Classic compute bills for the whole time the VMs are up, including start-up time and idle time. This leads to a useful rule of thumb:
- If a classic cluster is busy most of the time it is running, its cost per unit of work can be lower than serverless.
- If it spends a large fraction of its time starting, waiting, or idle, serverless often costs less, because you stop paying for that time.
Treat this as a hypothesis to test, not a guarantee. Measure both options on a real workload using the billing usage system table or the account usage page, and compare cost for the same amount of work.
Cost attribution works differently as well. On classic compute you tag clusters, and the tags flow to the cloud bill. On serverless you use budget policies and usage tags in Databricks, which appear in the billable usage data.
Governance and security
Serverless compute is built to work with Unity Catalog. Table and volume permissions apply, and the user or job identity determines access. Network behavior is different from classic compute, because the compute runs in Databricks' account rather than inside your VPC or virtual network. If your data sources sit behind private networking, you configure serverless network connectivity (for example, private endpoints or network policies, depending on your cloud) rather than adjusting a subnet you own.
Cluster policies, which constrain classic cluster configuration, do not apply in the same way, since there is little to constrain. Control spending with budget policies and workspace-level settings instead.
Limitations and edge cases
Serverless trades flexibility for convenience. Before you migrate a workload, check these points:
- Spark configuration. Only a small set of Spark properties can be set. Jobs that depend on custom configuration may fail or behave differently.
- Init scripts and custom containers. These are cluster features and are not available on serverless.
- Libraries. You manage dependencies through the environment definition, not through cluster library installation. Large or unusual native dependencies can be difficult.
- Languages and APIs. Support for languages and for lower-level APIs (such as direct RDD access) is more limited than on classic compute. Verify your code uses supported Spark Connect-compatible APIs.
- Caching assumptions. Do not assume that local disk state or cached data persists between runs. Treat each run as starting clean.
- Availability. Serverless is not available in every region or workspace type, and individual features roll out gradually.
- Debugging access. You cannot SSH to nodes or inspect the driver machine. You rely on the Spark UI where available, query history, and logs exposed by Databricks.
A common error is migrating a notebook that sets a Spark configuration or uses an unsupported API, and then reading the failure as a platform fault. Read the error message first. It usually names the unsupported setting or operation.
Mixing compute types
A single job can use different compute for different tasks. Tasks that need custom libraries, init scripts, or a specific runtime can reference a job cluster by job_cluster_key, while lightweight orchestration or SQL tasks run on serverless. This lets you move workloads gradually and keep classic compute only where it is needed.
Summary of when serverless fits
- Interactive exploration where waiting for a cluster slows people down.
- Scheduled jobs that are short, frequent, or irregular in size.
- Teams that want less operational work and no cluster policies to maintain.
It fits less well when you need custom Spark configuration, init scripts, custom containers, specific hardware, or very tight cost control on long steady workloads.
Exercise
A nightly job runs for 12 minutes of real processing. On a job cluster, start-up takes about 6 minutes and the cluster terminates immediately after the run. A teammate wants to move the job to serverless. Name two reasons it could be a good fit, and one check you should do before moving it.
Show answer
Good fit: (1) serverless starts in seconds, removing most of the 6 minutes of start-up, which is a third of the total time you pay for on classic compute; (2) you stop choosing and maintaining node type, runtime, and worker count, and billing covers only the compute used while the work runs. Check before moving: confirm the notebook does not depend on unsupported features such as custom Spark configuration, init scripts, or unsupported APIs, and compare measured cost for the same run on both compute types using billing usage data.
Exercise
A job definition contains a notebook task with no cluster settings and no job_clusters block. The run fails because no compute is available. What is the most likely cause?
Show answer
Serverless compute for jobs is probably not enabled in the workspace. A task with no compute specified runs on serverless only when that workspace setting is on. Either enable serverless for jobs (if your administrator allows it) or add a job cluster and reference it with jobclusterkey.