Sub topic 4
Autoscaling clusters and auto stop
This chapter covers the two settings that decide how many compute resources a SQL warehouse uses over time. Autoscaling changes the number of clusters while the warehouse runs. Auto stop shuts the warehouse down when it is idle. It assumes you know the warehouse types and sizes from the earlier chapters, and that you understand DBU billing, which charges for compute time while a warehouse runs.
Two different dimensions of capacity
Size and scaling are separate settings, and they solve different problems.
- Size (cluster size) sets how much compute each cluster has. A larger size helps one heavy query finish sooner.
- Scaling sets how many clusters of that size run side by side. More clusters help when many queries arrive at once.
A warehouse has a minimum and a maximum number of clusters. With a minimum of 1 and a maximum of 1, the warehouse never scales out. With a minimum of 1 and a maximum of 4, it starts with one cluster and can grow to four.
| Symptom | Likely cause | Setting to change |
|---|---|---|
| One large query is slow | Not enough compute per cluster | Increase size |
| Many users, queries wait in a queue | Not enough clusters | Raise the maximum cluster count |
| Warehouse costs money overnight with no users | Idle time before shutdown | Lower auto stop |
| First query each morning waits a long time | Cold start | Use serverless, or raise auto stop |
Adding clusters does not speed up a single query. Each query runs on one cluster, so scaling out only reduces waiting.
How autoscaling works
Each cluster can run a limited number of queries at the same time. A common rule of thumb in Databricks guidance is about 10 concurrent queries per cluster. When more queries arrive than the running clusters can handle, the extra queries wait in a queue.
On pro and classic warehouses, the warehouse watches the queue. If queries have been waiting, it starts another cluster, up to the maximum. When the load drops, it removes clusters, down to the minimum. Scale-down is deliberately slow, so a short lull does not trigger a removal followed by an immediate, expensive restart.
Serverless warehouses use Intelligent Workload Management. It estimates how demanding each incoming query is and adds capacity more quickly. Serverless also has much faster cluster startup, so scale-out reaches users in seconds instead of minutes.
The exact thresholds and timings change between releases, so treat any number here as approximate and check the current documentation before you depend on one.
Choosing minimum and maximum
- Set the minimum to the number of clusters you need for normal load. Every minimum cluster is billed while the warehouse runs, so a minimum of 3 costs about three times a minimum of 1.
- Set the maximum to the most you are willing to pay for during a spike. It is a cost cap as well as a capacity limit.
- Leave the minimum at 1 for most interactive workloads, then raise the maximum if you see queueing.
If you set the maximum too low, queries queue during peaks. If you set it far too high, one runaway dashboard refresh or a batch of scheduled queries can quietly multiply your bill.
Auto stop
Auto stop shuts the warehouse down after it has had no running queries for a set number of minutes. Nothing is billed for a stopped warehouse. If someone runs a query against it later, the warehouse starts again automatically.
Typical defaults:
- Serverless warehouses default to 10 minutes. The console allows shorter values, and the API allows even shorter ones for serverless.
- Pro and classic warehouses default to 45 minutes. The console minimum is 5 minutes, and you can disable auto stop entirely by setting 0.
The trade-off is cost against responsiveness:
- A short auto stop saves money but means more cold starts.
- A long auto stop keeps the warehouse warm but bills you for idle minutes.
Cold start time depends on the type. Pro and classic warehouses take several minutes to start. Serverless warehouses usually start in a few seconds, which is why a short auto stop works well there. On a pro or classic warehouse, a 5-minute auto stop can leave users waiting minutes for the first query after each break.
Stopping a warehouse also discards its local caches, so the first queries after a restart read more data from storage and run slower than they did before the stop. The caching chapter covers this in more detail.
The warehouse lifecycle
Configuring scaling and auto stop through the API
You can set all of this in the warehouse settings page. The same settings are available in the Warehouses API, which is useful for repeatable setups. This request creates a pro warehouse with one to three clusters and a 10-minute auto stop.
curl -X POST "$DATABRICKS_HOST/api/2.0/sql/warehouses" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "analytics-pro",
"cluster_size": "Small",
"min_num_clusters": 1,
"max_num_clusters": 3,
"auto_stop_mins": 10,
"warehouse_type": "PRO",
"enable_serverless_compute": false
}'For a serverless warehouse, set "warehouse_type": "PRO" and "enable_serverless_compute": true. Serverless is built on the pro feature set.
The field names min_num_clusters, max_num_clusters, and auto_stop_mins map directly to the settings in the console. The call returns the new warehouse ID, which you pass as warehouse_id when you run statements, as shown in the programmatic access chapter.
Observing scaling behavior
Do not guess whether your settings fit. Look at what the warehouse did. If system tables are enabled in your account, the warehouse events table records starts, stops, and scaling changes.
SELECT
event_time,
event_type,
cluster_count
FROM system.compute.warehouse_events
WHERE warehouse_id = '<your-warehouse-id>'
ORDER BY event_time DESC
LIMIT 50;Read the output like this:
- Frequent
SCALED_UPevents followed bySCALED_DOWNevents suggest bursty load. Consider a higher minimum if users complain about waiting. - A
cluster_countthat stays pinned at your maximum suggests the maximum is too low, or that the queries are too heavy for the size. - Many
STARTINGandSTOPPINGevents in one day suggest an auto stop that is too short for the usage pattern.
The warehouse's monitoring tab shows the same story as a chart of running clusters and queued queries. A line of queued queries that sits above zero for long stretches is the clearest sign that you need more clusters.
Common errors
- Raising the maximum to fix a slow query. Extra clusters do not help a single query. Check the query profile, or increase the size.
- Setting a high minimum "just in case". You pay for those clusters whenever the warehouse is running, even with no queries.
- Turning off auto stop on a pro or classic warehouse. The warehouse then bills around the clock until someone stops it.
- Using a very short auto stop on a pro or classic warehouse. Users hit multi-minute cold starts and lose the warm cache.
- Judging scale-down by a stopwatch. Clusters leave slowly by design, so a cluster that is still running a few minutes after load drops is normal.
Putting it together
A practical way to set a new warehouse:
- Pick the type. Choose serverless if available, because of the fast start and scaling.
- Pick the size from the slowest realistic single query.
- Set the minimum clusters to 1 and the maximum to the peak number of concurrent users divided by roughly 10, rounded up.
- Set auto stop to match how bursty usage is and how long a cold start takes for that type.
- Review the monitoring tab and the events table after real usage, and adjust.
Exercise
A team shares a pro warehouse (Medium, min 1, max 2, auto stop 5 minutes). Users report that the first query after lunch takes about four minutes, and during the 10:00 meeting rush, queries queue for a minute. Which two changes address these problems, and what does each cost?
Show answer
For the slow first query, the cause is a cold start after the warehouse auto stopped. Either switch to a serverless warehouse, which starts in seconds, or raise auto stop so it does not shut down over lunch. The cost is idle billing time if you raise auto stop, or serverless pricing if you switch. For the meeting rush, the cause is queueing, so raise the maximum cluster count (for example from 2 to 4). The cost is that during the rush you pay for up to twice as many clusters, but only while they run. Increasing the size would not fix queueing, because the problem is concurrency, not the speed of one query.