Managed Online Endpoints
This lesson explains how managed online endpoints and deployments are created, when a scoring script is needed, how deployments are sized and scaled, and how request settings affect errors.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what an MLflow deployment needs compared with a custom model.
- Explain the init() and run() functions of a scoring script.
- Describe instance count, quota headroom and autoscaling.
- Relate request settings to 408 and 429 errors.
Creating the endpoint and deployment
A managed online endpoint and its deployments are defined in YAML and created with the CLI, or with the equivalent SDK classes. The endpoint is created first, then each deployment.
# endpoint.yml
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineEndpoint.schema.json
name: churn-ep
auth_mode: aad_token# blue.yml
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json
name: blue
endpoint_name: churn-ep
model: azureml:churn:7
instance_type: Standard_DS3_v2
instance_count: 3az ml online-endpoint create --file endpoint.yml -g rg-ml-prod -w mlw-churn-prod
az ml online-deployment create --file blue.yml --all-traffic -g rg-ml-prod -w mlw-churn-prodBecause churn:7 is an MLflow model, this deployment needs no scoring script or environment: Azure Machine Learning generates both. --all-traffic sends 100 percent of the endpoint's traffic to the new deployment once it is created successfully.
Scoring scripts for custom models
A custom model, or an MLflow model that needs custom pre- or post-processing, uses a scoring script with two functions. init() runs once when the container starts and loads the model, typically from the folder in the AZUREML_MODEL_DIR environment variable. run() runs for every request, receives the request body and returns a JSON-serializable result.
import json, os, joblib, pandas as pd
def init():
global model
model = joblib.load(os.path.join(os.environ["AZUREML_MODEL_DIR"], "model", "model.pkl"))
def run(raw_data):
rows = pd.DataFrame(json.loads(raw_data)["data"])
return {"churn_probability": model.predict_proba(rows)[:, 1].tolist()}code_configuration:
code: ./score
scoring_script: score.py
environment: azureml:inference-env:2Expensive work belongs in init(), so it happens once per instance rather than once per request. An exception raised in init() stops the container from becoming ready, and an exception in run() returns an error to the caller.
Sizing and scaling
A deployment's capacity comes from its instance_type and instance_count. Microsoft recommends at least three instances for production deployments, so that the service stays available while instances are updated or recovered. Quota must cover about 20 percent more cores than the instances use, which Azure reserves for upgrades.
Autoscale adjusts the instance count with Azure Monitor autoscale rules, for example adding an instance when average CPU exceeds 70 percent for five minutes and removing one below 30 percent, within a minimum and maximum. Schedules can raise capacity before known peaks. Autoscale settings are created with az monitor autoscale create against the deployment's resource ID.
| Request setting | Default | Effect |
|---|---|---|
request_timeout_ms | 5,000 ms | Requests running longer return HTTP 408 |
max_concurrent_requests_per_instance | 1 | Requests beyond this queue or return HTTP 429 |
Liveness and readiness probes check that the container is healthy and ready; a model that takes a long time to load needs a longer initial delay so that the probes do not fail before init() finishes.
Identity, networking and data collection
The deployment's managed identity pulls the image and model; a user-assigned identity needs AcrPull and Storage Blob Data Reader granted in advance. Setting the endpoint's public_network_access to disabled makes it reachable only through the workspace's private endpoint, and in a managed virtual network the deployment's outbound traffic follows the workspace's isolation mode.
The data collector logs the deployment's inputs and outputs to Blob Storage, which model monitoring then analyses. It is enabled per deployment, with a sampling rate from 0 to 100 percent.
data_collector:
sampling_rate: 1.0
collections:
model_inputs:
enabled: "True"
model_outputs:
enabled: "True"For custom scoring scripts, the azureml-ai-monitoring package's collector is called inside run() to log pandas DataFrames of inputs and outputs, with a correlation ID that later joins them to ground truth.
Timeouts under load
A managed online deployment with two Standard_DS3_v2 instances serves a gradient boosting model. During the morning peak, clients receive HTTP 429 responses, while CPU on both instances stays below 40 percent. Each request takes about 150 milliseconds. The deployment uses default request settings.
With the default max_concurrent_requests_per_instance of 1, each instance processes one request at a time, so concurrent requests are rejected while CPU sits mostly idle. Raise max_concurrent_requests_per_instance to a value the instance can handle, verified by load testing, and configure autoscale with a minimum of three instances for availability and growth at peak.
Raising request_timeout_ms addresses HTTP 408, not 429. A larger instance type would add CPU that is already unused. Switching to key authentication does not affect concurrency.
Under load, clients receive HTTP 429 from a managed online deployment with one instance and default settings. Which setting is the likely limit?
Show answer
max_concurrent_requests_per_instance, which defaults to 1. Requests beyond it are rejected with 429, so raise it to suit the model or add instances.
Exam Tips
Remember for the exam
- MLflow models deploy with no scoring script; custom models need init() and run().
- Use at least three instances in production; quota needs about 20 percent extra.
- request_timeout_ms (default 5,000) causes 408s; max_concurrent_requests_per_instance (default 1) causes 429s.
- The data collector logs model_inputs and model_outputs for monitoring.