Sub topic 11
Using spot instances safely
This chapter assumes you know Spark stages, tasks, and shuffles. It also assumes you know the difference between on-demand and spot pricing in a cloud provider, and that you can read a Databricks cluster specification in JSON. By the end you should be able to decide which clusters can use spot workers, configure them, and recognize the failures spot capacity causes.
What spot instances are
A spot instance is a virtual machine that the cloud provider sells from spare capacity at a steep discount. The discount varies by instance type, region, and time, and is often large. The trade-off is that the provider can take the machine back, usually with a short warning (about two minutes on AWS), when it needs the capacity or when the spot price rises above your bid.
In a Databricks cluster this means any node on spot capacity can disappear in the middle of a job. Whether that is acceptable depends on which node it is and what Spark does when it happens.
Why workers can use spot and drivers should not
The driver holds the SparkContext, the query plan, the scheduler state, and the notebook session. If the driver is lost, the whole application fails. Spark cannot recover from it, and a notebook loses its state.
Workers are different. Spark is designed to tolerate executor loss:
- Tasks running on the lost executor are rescheduled on other executors.
- Cached partitions on the lost node are recomputed from lineage when needed.
- Shuffle output files stored on the lost node are gone. Spark must rerun the map tasks that produced them.
So the standard pattern is an on-demand driver with spot workers. Databricks supports this through first_on_demand, which counts nodes starting with the driver.
{
"cluster_name": "etl-spot",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"aws_attributes": {
"first_on_demand": 1,
"availability": "SPOT_WITH_FALLBACK"
},
"autotermination_minutes": 30
}Here first_on_demand: 1 keeps the first node, the driver, on-demand. The remaining nodes are requested as spot. SPOT_WITH_FALLBACK tells Databricks to request an on-demand instance if spot capacity is unavailable, so the cluster still starts.
The availability settings
On AWS the availability field takes three values.
| Value | Behavior | Typical use |
|---|---|---|
| `ON_DEMAND` | All nodes are on-demand. | Production jobs with strict deadlines, streaming, interactive clusters |
| `SPOT` | Nodes past `first_on_demand` are spot only. If capacity is missing, the cluster gets fewer nodes or fails to scale. | Cost-sensitive work that tolerates a smaller cluster |
| `SPOT_WITH_FALLBACK` | Spot first, on-demand if spot is unavailable. | Most batch ETL |
Azure and Google Cloud have their own attribute blocks (azure_attributes, gcp_attributes) with equivalent settings. The names differ, so check the Clusters API reference for your cloud.
The fallback has a cost implication. If spot capacity is scarce, a SPOT_WITH_FALLBACK cluster quietly becomes an on-demand cluster, and your bill rises toward the full price. Check actual usage if the savings look smaller than expected.
What happens when a spot node is reclaimed
The job usually continues, but with a cost in time. Three effects matter.
- Task retries. Tasks that were running on the lost node start over on another executor. A task is retried up to
spark.task.maxFailurestimes (default 4), but losing an executor because its node was reclaimed is generally not counted against that limit. - Recomputed shuffle data. If a later stage needs shuffle files that lived on the lost node, Spark reruns the earlier stage's tasks to regenerate them. In a job with several wide stages, one reclaimed node can trigger a chain of reruns.
- Lost cached data. Cached DataFrames lose the partitions on that node. Spark recomputes them from the source when they are next used, which can be slow for expensive lineage.
A job on spot workers therefore has a longer, less predictable runtime. The savings are real only if the extra runtime costs less than the discount.
Databricks also supports decommissioning of workers on spot reclaim. When this is enabled, Spark tries to migrate shuffle and cached blocks to other nodes during the warning period. The settings are spark.decommission.enabled, spark.storage.decommission.enabled, spark.storage.decommission.shuffleBlocks.enabled, and spark.storage.decommission.rddBlocks.enabled. Check the current Databricks documentation for your runtime before relying on them. Two minutes is not always enough to move a large amount of shuffle data.
When spot is a good fit
Spot works best when losing a node costs little.
- Batch ETL with idempotent steps. If a retry or a full rerun produces the same output, a failure is only a delay. Delta Lake transactions help here, because a partially finished write is never visible.
- Short jobs. The shorter the job, the smaller the chance a reclaim hits it, and the less work is lost.
- Development and testing clusters. An occasional interruption is tolerable.
- Scale-out portions of an autoscaling cluster. Keep a small on-demand base and let burst capacity be spot.
When to avoid spot
- Interactive clusters used by many people. A reclaimed node can interrupt several users' queries and drop cached tables.
- Streaming jobs with latency targets. A reclaim causes a pause while tasks recompute and the cluster replaces nodes. Streaming queries can recover from checkpoints, but the delay may breach a service level.
- Long jobs with large shuffles. The longer a job runs, the more reclaims it sees, and the more expensive each recompute is.
- Jobs with hard deadlines. If a missed deadline costs more than the discount saves, use on-demand.
Mixing on-demand and spot
You do not have to choose one or the other. first_on_demand can be larger than 1. With a value of 3, the driver and the first two workers are on-demand, and the rest are spot. The on-demand workers form a stable core that always survives, so a reclaim removes only part of the cluster. This is also a hedge against the case where spot capacity disappears entirely: the job runs more slowly, but it still finishes.
With autoscaling, the nodes that exist at the minimum are the first ones requested. Setting first_on_demand to cover the driver plus min_workers gives you an on-demand floor, and workers added during scale-up are spot.
Enforcing spot settings with policies
Spot settings are easy to forget or to override. A cluster policy can fix them for everyone who uses it. This keeps expensive on-demand clusters from appearing by accident, and it can also stop users from putting the driver on spot capacity.
{
"aws_attributes.availability": {
"type": "fixed",
"value": "SPOT_WITH_FALLBACK"
},
"aws_attributes.first_on_demand": {
"type": "range",
"minValue": 1,
"defaultValue": 1
},
"autoscale.max_workers": {
"type": "range",
"maxValue": 8,
"defaultValue": 4
}
}The minValue of 1 on first_on_demand guarantees the driver is always on-demand. Policies are covered in more detail in a later chapter.
Common mistakes
- Using spot with
first_on_demand: 0. This puts the driver on spot capacity. Failures look random and are hard to diagnose. - Choosing
SPOTwithout fallback for a job that must finish. If capacity is short, the cluster may start small or not at all. - Assuming the discount is fixed. Spot savings vary by instance type and time. Compare the current spot price to on-demand before committing, and consider several instance types if your cloud supports flexible fleets.
- Ignoring DBU cost. Spot reduces the cloud infrastructure charge only. Databricks Units (DBUs) are billed at the normal rate for the same node, so total savings are smaller than the headline spot discount.
- Judging success by price alone. A job that runs 40 percent longer because of recomputation may save little once the extra DBUs are counted.
Checking whether spot is working
Look at three things after running a job on spot capacity:
- Executor events in the Spark UI. The Executors tab and the event log show removed executors. Repeated removals mean frequent reclaims.
- Stage retries. Stages that appear multiple times, or tasks with "ExecutorLostFailure", point to recomputed shuffle data.
- Runtime and cost compared with an on-demand baseline. Run the same job both ways a few times. If the spot run is not clearly cheaper, the discount is being lost to reruns.
Exercise
A nightly ETL job runs on a cluster with "first_on_demand": 0 and "availability": "SPOT_WITH_FALLBACK". Twice this month the whole job failed with no executor-level errors, and a rerun succeeded. What is the most likely cause, and what change fixes it?
Show answer
With first_on_demand set to 0, the driver is on spot capacity too. When the provider reclaimed the driver node, the whole Spark application ended, which looks like a sudden failure that a rerun clears. Set first_on_demand to 1 so the driver is on-demand, and keep SPOT_WITH_FALLBACK for the workers. A cluster policy with a minValue of 1 on first_on_demand stops the setting from being changed back.
Exercise
A streaming query with a 10-second latency target runs on 8 spot workers. Should you keep it on spot? Give one reason and one possible compromise.
Show answer
Usually not. A spot reclaim stalls the query while tasks are rescheduled and the cluster replaces the node, which can break the latency target. A compromise is to use on-demand for a core set of workers by raising first_on_demand, or to use on-demand for the whole cluster if the target is strict, and to keep spot for batch jobs that tolerate delay.