Sub topic 11
Using spot instances safely
Spot instances are spare cloud capacity sold at a steep discount. The cloud provider can reclaim them with short notice. This chapter assumes you know Spark stages, tasks and shuffles, and the difference between on-demand and spot pricing. It also assumes you have read the earlier chapters on the driver and workers and on cost.
Why spot works for workers but not drivers
The driver holds the SparkContext, the query plan and the state of every running job. If the driver is reclaimed, the whole cluster fails and the job must restart. Workers are different. Spark can re-run lost tasks on other executors, so losing a worker costs time but not correctness.
The rule that follows is simple: keep the driver on on-demand capacity and put workers on spot.
Configuring spot with fallback
On AWS, the aws_attributes block controls this. first_on_demand sets how many nodes, counting from the driver, are on-demand. availability sets the type for the rest.
{
"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
}With first_on_demand set to 1, only the driver is on-demand. SPOT_WITH_FALLBACK requests spot workers and switches to on-demand capacity when no spot capacity is available. Plain SPOT has no fallback, so the cluster may not reach its target size.
What happens when a spot node is lost
The cost of a loss depends on what the node held. Running tasks restart elsewhere. Shuffle files stored on the lost node's local disk are gone, so Spark must recompute the stage that produced them. Jobs with long, wide shuffles lose more work per reclaim than short, narrow ones.
Common errors
- Driver on spot. Setting
first_on_demandto 0 puts the driver on spot. A single reclaim kills the job. - No fallback. Using
SPOTalone can leave a cluster smaller than its minimum, or unable to start, during a capacity shortage. - Spot for strict deadlines. Retries and recomputation make run time less predictable. Use on-demand when a job must finish by a fixed time.
- Spot for streaming. A long-running stream sees repeated disruptions and restarts. Prefer on-demand workers.
Combining spot with autoscaling
Autoscaling adds workers when load rises. With spot, those added workers are also discounted. Keep min_workers high enough that the job can make progress if spot capacity disappears, because fallback fills gaps only at on-demand prices.
Exercise
A nightly batch cluster uses "first_on_demand": 0 and "availability": "SPOT". It sometimes fails completely mid-run. What two changes make it more reliable, and why?
Show answer
Set first_on_demand to 1 so the driver runs on on-demand capacity, since losing the driver fails the whole job. Change availability to SPOT_WITH_FALLBACK so the cluster gets on-demand workers when spot capacity is unavailable.