Sub topic 9
Choosing workload size and scale to zero
This chapter covers the two compute settings you choose when you create a Model Serving endpoint: workload size and scale to zero. It assumes you can already create an endpoint with the Databricks SDK, as in the previous chapter. It also assumes you know what a REST request is and that you have a registered model version to serve.
What these two settings control
An endpoint runs your model on managed compute. Two settings on each served entity decide how much compute you pay for and how it behaves:
workload_sizesets how many requests the endpoint can process at the same time.scale_to_zero_enabledsets whether the endpoint can shut its compute down when nobody is calling it.
Together they set a trade-off between cost, latency, and availability. A larger workload handles more traffic but costs more while it runs. Scale to zero costs nothing while idle, but the first request after an idle period has to wait for compute to start.
Concurrency, not requests per second
Workload size is defined by provisioned concurrency. This is the number of requests the endpoint can be processing at the same moment. It is not requests per second.
The two are linked by the latency of one prediction:
concurrency needed ≈ requests per second × seconds per request
For example, suppose clients send 40 requests per second and each prediction takes 0.1 seconds. At any moment about 4 requests are in flight. A model that takes 1 second per prediction would need about 40 concurrent slots for the same traffic.
This is why a fast model on a small endpoint can handle surprising traffic, and a slow model can saturate a large one. Measure your model's latency before you pick a size.
The available sizes
Databricks offers three CPU workload sizes. Each maps to a range of concurrency the endpoint can scale within.
| Workload size | Approximate concurrency | Typical use |
|---|---|---|
| Small | 0 to 4 | Development, testing, low-volume batch-style calls |
| Medium | 8 to 16 | Moderate production traffic |
| Large | 16 to 64 | High-volume production traffic |
The ranges and the available options can change, and GPU workload types exist for larger models. Check the current Databricks documentation before you commit to a production design. For the small scikit-learn model in this tutorial, Small is enough.
Within the range for its size, the endpoint adds and removes capacity as traffic changes. A Medium endpoint is not always running 16 slots. It scales between its lower and upper bound as concurrent demand rises and falls.
Setting the size in code
The size is a field on ServedEntityInput. This example creates an endpoint with Medium compute that never scales to zero:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput, ServedEntityInput
w = WorkspaceClient()
w.serving_endpoints.create_and_wait(
name="iris-endpoint-prod",
config=EndpointCoreConfigInput(
served_entities=[
ServedEntityInput(
entity_name="main.default.iris_classifier",
entity_version="1",
workload_size="Medium",
scale_to_zero_enabled=False,
)
]
),
)The only differences from the earlier example are workload_size="Medium" and scale_to_zero_enabled=False. The endpoint name must be unique in the workspace, so this example uses a different name.
How scale to zero works
When scale_to_zero_enabled is True, the endpoint follows a cycle:
- The endpoint serves requests normally.
- If it receives no traffic for a period of time, it releases its compute. Databricks documents this idle period as about 30 minutes.
- The endpoint stays registered, and its URL, permissions, and configuration are unchanged. It simply has no running compute.
- The next request triggers a cold start. Databricks provisions compute, loads the model, and then processes the request.
The cold start can take from tens of seconds to several minutes, depending on the model's size and its dependencies. That is long enough for an HTTP client with a short timeout to give up before the endpoint is ready.
Scale to zero is a development convenience
Databricks does not guarantee availability or latency for endpoints that scale to zero, and recommends against it for production workloads. Use it when:
- the endpoint is for development, demos, or tests
- calls are rare and the callers can tolerate a delay
- cost matters more than first-request latency
Turn it off when:
- a user or another service waits on the response
- you have a latency target
- a monitoring probe or health check calls the endpoint and would keep failing during a cold start
Handling cold starts in a client
If you must call a scale-to-zero endpoint from code, retry with a growing delay and a long overall timeout. This script does that with the requests library and the token authentication from the earlier chapter:
import os
import time
import requests
HOST = os.environ["DATABRICKS_HOST"].rstrip("/") # for example https://my-workspace.cloud.databricks.com
TOKEN = os.environ["DATABRICKS_TOKEN"]
URL = f"{HOST}/serving-endpoints/iris-endpoint/invocations"
payload = {
"dataframe_split": {
"columns": [
"sepal length (cm)",
"sepal width (cm)",
"petal length (cm)",
"petal width (cm)",
],
"data": [[5.1, 3.5, 1.4, 0.2]],
}
}
def score(payload, attempts=6, first_delay=5):
delay = first_delay
for attempt in range(1, attempts + 1):
try:
resp = requests.post(
URL,
headers={"Authorization": f"Bearer {TOKEN}"},
json=payload,
timeout=120,
)
if resp.status_code == 200:
return resp.json()
if resp.status_code not in (429, 500, 502, 503, 504):
resp.raise_for_status() # a 4xx such as 400 or 403 will not fix itself
except (requests.Timeout, requests.ConnectionError):
pass
print(f"Attempt {attempt} failed, retrying in {delay}s")
time.sleep(delay)
delay *= 2
raise RuntimeError("Endpoint did not become ready in time")
print(score(payload))The script retries only errors that can be temporary: timeouts, connection errors, and 429 or 5xx responses. A 400 means the payload does not match the model signature, and a 403 means a permission problem. Retrying those wastes time and hides the real error.
Changing the settings on an existing endpoint
You can change workload size or scale to zero without recreating the endpoint. Send an updated configuration with update_config_and_wait:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ServedEntityInput
w = WorkspaceClient()
w.serving_endpoints.update_config_and_wait(
name="iris-endpoint",
served_entities=[
ServedEntityInput(
entity_name="main.default.iris_classifier",
entity_version="1",
workload_size="Medium",
scale_to_zero_enabled=False,
)
],
)The update replaces the endpoint's served entities, so repeat every field you want to keep, including the entity name and version. While the update is in progress, the previous configuration keeps serving traffic. The endpoint switches over once the new compute is ready. The next chapter covers this rollout behavior in more detail.
Choosing a configuration
Use this sequence to decide.
- Estimate concurrency. Multiply expected peak requests per second by the model's latency in seconds. Measure latency on a test endpoint instead of guessing.
- Pick the smallest size whose range covers that number, with some headroom for bursts. Start small and move up when metrics show queueing.
- Decide on scale to zero. Enable it for development and low-stakes endpoints. Disable it for anything with a latency target.
- Revisit after launch. Real traffic usually differs from the estimate.
| Situation | Suggested settings |
|---|---|
| Notebook experiments and a demo | Small, scale to zero on |
| Internal tool used a few times a day | Small, scale to zero on, client with retries |
| Customer-facing API with steady traffic | Medium or Large, scale to zero off |
| Spiky traffic with a latency target | Size for the peak, scale to zero off |
Common mistakes
- Sizing by requests per second alone. A 2 second model at 10 requests per second needs about 20 concurrent slots, which does not fit in Small.
- Leaving scale to zero on after a demo. The endpoint then fails its first request on a Monday morning.
- Over-sizing to fix slow predictions. More concurrency lets more requests run at once. It does not make each prediction faster. If a single prediction is slow, look at the model or its feature preparation.
- Retrying every error. Only temporary failures benefit from retries.
- Omitting fields in an update. Because an update replaces the served entities, a missing field can reset to its default.
Exercise
An endpoint serves a model that takes about 0.5 seconds per prediction. Expected peak traffic is 12 requests per second. Roughly how much concurrency is needed, which workload size covers it, and should scale to zero be on for a customer-facing API?
Show answer
Concurrency is about 12 × 0.5 = 6 requests in flight. Small covers 0 to 4, so Small is too small. Medium (8 to 16) covers it with headroom. Scale to zero should be off for a customer-facing API, because the first request after an idle period would wait for a cold start and could time out.
Exercise
A client calls a scale-to-zero endpoint and gets a timeout on its first request each morning, but later requests succeed. Name two ways to fix it.
Show answer
Disable scale to zero so compute stays running, or make the client retry with backoff and a long timeout so it can wait through the cold start. For a service with a latency target, disabling scale to zero is the better fix.