How to deploy a scikit-learn model as a real-time REST API endpoint
Summary
- Use MLflow to log and register your scikit-learn model in Unity Catalog, then deploy it as a real-time endpoint through Model Serving
- Model Serving automatically creates a REST API with built-in authentication, monitoring, and serverless scaling
- Deployment takes minutes: log your model, register it with an alias, create an endpoint, and start serving predictions
How to deploy a scikit-learn model as a real-time REST API endpoint
Deploying a scikit-learn model as a REST API on Databricks takes three steps: log your trained model with MLflow, register it in Unity Catalog, and create a serving endpoint. Model Serving handles the REST infrastructure, autoscaling, and monitoring automatically.
What is MLflow model logging?
MLflow is an open-source framework for tracking experiments and packaging models. When you log a scikit-learn model with mlflow.sklearn.log_model(), MLflow captures the model object, its dependencies, and a signature (input and output schemas). Autologging can do this automatically for popular frameworks.
Key capabilities to evaluate
| Capability | Why it matters |
|---|---|
| Model packaging | Capturing dependencies ensures your model runs consistently in production without environment drift |
| Inference signatures | Defining input/output schemas enables automatic request validation and type conversion |
| Real-time API creation | REST endpoints eliminate the need to manage infrastructure; Model Serving handles routing and concurrency |
| Monitoring and logging | Tracking predictions, latency, and errors helps detect model drift and performance issues |
| Automatic scaling | Serverless infrastructure adjusts compute resources based on request volume without manual tuning |
How Databricks approaches scikit-learn model deployment
1. Log and register your model
After training your scikit-learn model, log it with MLflow in a Databricks notebook:
import mlflow
import mlflow.sklearn
with mlflow.start_run():
mlflow.sklearn.log_model(model, "model", input_example=X_train.head())
mlflow.register_model("runs:/<run-id>/model", "my_model")
Once registered in Unity Catalog, you can reference it by name: models:/my_catalog.my_schema.my_model.
2. Create a serving endpoint
In the Model Serving UI or via API, specify your registered model and create an endpoint. Model Serving deploys the model as a REST API that accepts JSON requests and returns predictions.
3. Make predictions
Call your endpoint with JSON:
curl -X POST https://your-workspace.cloud.databricks.com/serving-endpoints/my-endpoint/invocations \\
-H "Authorization: Bearer <token>" \\
-H "Content-Type: application/json" \\
-d '{"dataframe_split": {"columns": ["age", "income"], "data": [[45, 75000]]}}'
Model Serving automatically scales to handle request spikes and provides single-digit-to-low latency for production workloads. All data is encrypted in transit and at rest.
Use cases
- Real-time recommendations: Deploy a classification model that scores user interactions for personalized content
- Fraud detection: Run predictions on incoming transactions in milliseconds to flag suspicious activity
- Pricing optimization: Update dynamic prices in real time by calling a regression model for each product request
FAQs
Can I deploy multiple versions of the same model?
Yes. Use MLflow aliases (e.g., "Champion", "Challenger") to mark different model versions. You can then gradually shift traffic from one version to another for A/B testing or canary deployments.
What happens if my model's dependencies change?
MLflow captures the exact Python packages and versions your model needs. When deployed, Model Serving creates a reproducible environment, preventing dependency drift between development and production.
How do I monitor prediction latency?
Model Serving logs endpoint health metrics including latency, error rate, and CPU usage. You can query inference logs stored in Delta tables to analyze request patterns and detect performance degradation over time.
Is there a way to test my endpoint before going to production?
Yes. Create a staging endpoint first and test with sample data. Once validated, you can route production traffic by updating the alias to point to the production endpoint version.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.