Deploying Machine Learning Models at Scale with Web Frameworks
As machine learning matures from research to production, model deployment has emerged as a critical challenge for realizing business value from AI. According to a recent Algorithmia survey of IT leaders, 55% of companies have not deployed models into production and 40% require more than 30 days to do so. The 2021 Kaggle State of Data Science survey found that the share of data scientists who have deployed a model to production has stalled at ~50% since 2018.
The gap between developing models and deploying them into production applications is one reason Gartner predicts that 85% of AI projects will fail to deliver value through 2022. To unlock the full potential of AI, it‘s clear that model deployment must be a first-class concern for data science teams, on par with data preparation and model training.
Choosing a Web Framework for Model Deployment
To make model predictions available to end-users, a common pattern is to expose the model via a web API using a Python web framework. Lightweight frameworks like Flask and FastAPI are popular for their simplicity and performance, while full-featured frameworks like Django offer more built-in functionality.
Here‘s a comparison of key features and performance metrics:
| Framework | Async | Data Validation | ORM | Perf (req/sec) |
|---|---|---|---|---|
| Flask | No | No | No | 16,098 |
| FastAPI | Yes | Yes | No | 50,258 |
| Django | No | No | Yes | 10,300 |
Performance benchmarks from TechEmpower Web Framework Benchmarks and FastAPI benchmarks.
For simple deployments, Flask is a good choice for its flexible routing and templating. FastAPI provides higher performance and automatic request validation using Python type hints. For more complex use cases requiring a database and admin interface, Django may be worth the added overhead.
Best Practices for Reliable Model Deployments
Regardless of web framework, there are several best practices teams should follow to ensure reliable and scalable model deployments:
Versioning Models and APIs
It‘s crucial to version both serialized model files and the APIs that serve them. This allows for controlled rollouts, A/B testing, and the ability to rollback buggy versions. Daan Odijk et al. recommend using semantic versioning (e.g. v1.2.0) for APIs and including the model version, dataset version, and hyperparameters in model file names.
Optimizing Inference Performance
While training models can take hours or days, predictions in production need to be fast, often on the order of milliseconds. To optimize inference latency and throughput, Ville Tuulos advises using vectorized operations, avoiding Python loops, and leveraging hardware accelerators like GPUs and TPUs when possible. Caching frequent requests and optimizing web server config (e.g. gunicorn/uWSGI workers) is also critical.
Model Monitoring
Deployed models can fail silently in subtle ways, making monitoring essential for catching issues like data drift and performance degradation. Google‘s MLOps guidelines recommend defining SLOs for model performance and setting up alerts for when they are violated. Open source tools like WhyLabs and Evidently can help track model metrics and detect abnormalities before they impact users.
Securing Models and APIs
Exposing models via APIs introduces new security risks that must be mitigated. Andrew Clark warns that APIs should validate and sanitize all inputs, even from trusted sources, to prevent attacks like SQL injection. Authentication and rate limiting are essential for blocking malicious requests. For sensitive use cases, differential privacy techniques can help protect user data during inference.
Advanced Deployment Architectures
As model deployments grow in scale and complexity, data teams are turning to more advanced architectures:
Micro-batching Requests
TensorFlow Serving supports micro-batching requests and running them together through the model for efficiency gains. Requests within a short window (e.g. 5ms) are collected into a batch that is fed to the model as a single input tensor. This amortizes the overhead of loading the model and can greatly increase throughput at the cost of some added latency.
Streaming Inference
While most deployments use a request-response pattern, some applications require making predictions on streaming data in real-time. Architectures like Kafka Streams or Flink can be used to consume input events, run them through the model, and output predictions to a storage system with low latency. Chip Huyen explores techniques like incremental learning and approximate queries to deploy models on streaming data.
Kubernetes Model Deployments
Deploying models on Kubernetes has become popular for its declarative configuration, self-healing properties, and ability to scale on any cloud platform. Open source libraries like Seldon Core, KFServing, and BentoML provide custom resources to deploy models on pre-built inference servers with features like autoscaling, monitoring, and canary rollouts.
Using a Service Mesh
A service mesh like Istio can provide additional reliability and security features on top of Kubernetes model deployments. Istio includes a sidecar proxy to mediate all network traffic and implement policies like rate limiting, circuit breaking, and mutual TLS. It also enables advanced deployment strategies like traffic mirroring (shadowing), fault injection, and outlier detection.
Model Deployment in the MLOps Lifecycle

Image Source: MLOps: Continuous delivery and automation pipelines in machine learning
Model deployment is one component in the end-to-end machine learning engineering lifecycle, also known as MLOps. MLOps aims to apply DevOps principles like CI/CD, monitoring, and automation to make developing and deploying models more reliable and efficient.
Within this process, models developed by data scientists must be packaged with their code and dependencies for reproducibility. MLOps platforms like Determined AI, Comet, and Sagemaker coordinate model deployment alongside experiment tracking, distributed training, hyperparameter tuning, and data/model versioning.
This table summarizes some of the key deployment capabilities offered by these platforms:
| Platform | Framework Support | Model Monitoring | Security & Compliance | Deployments |
|---|---|---|---|---|
| Determined AI | TensorFlow, PyTorch, Keras | Integration with Prometheus, Grafana, etc. | Role-based access control, Audit logging | Kubernetes, AWS ECS/EKS, GCP GKE, Azure AKS |
| AWS SageMaker | TensorFlow, PyTorch, MXNet, Chainer, Scikit-learn | Built-in metrics, alerts, explainability | IAM roles, VPC security, HIPAA compliance | SageMaker endpoints, Elastic Inference |
| Azure ML | TensorFlow, PyTorch, Scikit-learn | Integration with Azure Monitor | Azure AD auth, Secrets management, Network isolation | AKS, ACI, Functions, IOT Edge |
| GCP Vertex AI | TensorFlow, PyTorch, Scikit-learn, XGBoost | Explainable AI, Continuous evaluation | IAM roles, VPC service controls, Audit logging | Vertex AI Endpoints: GKE, Serverless |
Comparison data from vendor documentation as of June 2023
As MLOps matures, these platforms are becoming more full-featured and aim to cover the entire model development lifecycle, including deployment. For smaller teams, managed platforms can greatly accelerate time-to-value, while larger organizations may choose to build their own MLOps stack using open-source tools.
Future of Model Deployment: 2023 and Beyond
Looking ahead, we can expect model deployment to become increasingly automated and standardized as ML matures in the coming years. A few key trends I predict will take hold:
-
Serverless deployments will become the default for all but the most latency-sensitive applications. Serverless abstracts away the underlying infrastructure and automatically scales with traffic. AWS Lambda, Google Cloud Functions, and Azure Functions already support deploying ML models and this trend will accelerate.
-
Feature stores will serve as the interface between models and data, enabling feature reuse across both training and inference. Tecton, Feast, and Hopsworks are building managed platforms to handle the complex process of materializing feature values consistently for training and serving.
-
Inference at the edge will grow to enable low-latency, privacy-preserving predictions without sending data to the cloud. Frameworks like ONNX allow models to be deployed to edge devices and gateways. Techniques like federated learning and collaborative inference will enable model updates without transferring raw data.
-
Explainable and fair deployments will become table stakes as AI is applied to high-stakes domains. Regulators are already mandating that certain model decisions are interpretable and unbiased. Libraries like AIX360, InterpretML, and FairLearn will be integrated into deployment pipelines to provide explanations and audit model fairness in production.
Conclusion
Model deployment is the bridge from development to production that makes machine learning valuable for the real world. As companies race to apply AI to more domains, reliable and scalable model deployment is a prerequisite for success.
While still nascent today, over the next decade, MLOps platforms that automate the model deployment lifecycle will become as ubiquitous as DevOps tooling is for traditional software. ML teams that master these tools and best practices will have a significant advantage in building mission-critical intelligent applications.
To dive deeper, check out these additional resources:
- Engineering best practices for Machine Learning (SE-ML GitHub)
- Machine Learning in Production (Video course by John Langford)
- Serving Machine Learning Models (Book by Boris Lublinsky, Michael Katsnelson, and Max Marmer)