From Research to Deployment
A model that works in a notebook is only the beginning. Productionizing AI requires infrastructure, monitoring, versioning, and operational rigor. This is where ML engineering meets DevOps—welcome to MLOps.
Learning Objectives
- Understand the MLOps lifecycle: from experimentation to production to retirement
- Design data pipelines and feature stores for reproducible ML
- Implement model versioning, experiment tracking, and reproducibility
- Deploy models: batch vs real-time, edge vs cloud, containers vs serverless
- Monitor production models for drift, performance degradation, and failures
- Build feedback loops and continuous training systems
The Production Gap
87% of ML projects never make it to production. The gap between a working prototype and a reliable production system is vast—filled with infrastructure, operations, monitoring, and organizational challenges that notebooks don't prepare you for.
In research, you optimize for model accuracy. In production, you optimize for reliability, latency, cost, maintainability, and the ability to update without breaking things. These are different disciplines.
- Reliability: 99.9% uptime, graceful degradation
- Latency: p50 < 100ms, p99 < 500ms for many applications
- Scalability: Handle 10x traffic spikes
- Cost efficiency: Inference at scale is expensive
- Reproducibility: Same code + data = same model
- Observability: Know when things go wrong
| Research | Production |
|---|---|
| Static dataset | Streaming, changing data |
| Manual experiments | Automated pipelines |
| Accuracy matters | Latency, cost, reliability matter too |
| One environment | Dev, staging, prod, edge |
| Train once | Continuous training |
| Evaluate offline | Monitor in real-time |
The MLOps Lifecycle
Data Pipeline
- Data ingestion
- Validation
- Transformation
- Feature engineering
- Feature store
Training Pipeline
- Experiment tracking
- Hyperparameter search
- Model training
- Evaluation
- Model registry
Deployment
- Model packaging
- Serving infrastructure
- A/B testing
- Canary releases
- Rollback capability
Monitoring
- Performance metrics
- Data drift detection
- Model drift detection
- Alerting
- Feedback collection
Data Pipeline and Feature Engineering
In production, data is the most challenging part. It's messy, constantly changing, and the same features need to be computed identically for training and inference.
Data Validation
Before data enters your pipeline, validate it. Catch schema changes, null values, distribution shifts, and anomalies early.
- Schema validation (types, columns)
- Statistical validation (ranges, distributions)
- Semantic validation (business rules)
- Freshness validation (data is recent)
Feature Stores
A centralized repository for features that ensures consistency between training and serving. Features computed once, used everywhere.
- Reuse: Share features across models
- Consistency: Same computation for training/serving
- Time-travel: Point-in-time correct features
- Low latency: Precomputed for real-time serving
Training-Serving Skew
When features are computed differently in training vs serving, models perform worse in production than in evaluation. This is a silent killer.
- Different code paths for offline/online computation
- Data leakage in training (future data available)
- Different preprocessing libraries or versions
- Aggregation windows computed differently
The Data Flywheel
Production systems should collect data that improves future models. Log predictions, outcomes, and user feedback. Build active learning pipelines that identify valuable examples. The best production systems get better automatically.
Experiment Tracking and Reproducibility
Given the same code, data, and configuration, you should get the same (or statistically equivalent) model. Without reproducibility, you can't debug, compare, or reliably improve.
Code Versioning
Git for everything. Tag releases. Link commits to experiments.
Data Versioning
DVC, Delta Lake, or LakeFS. Version datasets like code.
Environment Versioning
Docker containers, conda environments. Pin all dependencies.
Experiment Tracking
MLflow, Weights & Biases, Neptune. Log everything automatically.
Model Registry
Central catalog of trained models with metadata and lineage.
What to Track for Every Experiment
Model Deployment Patterns
Batch Inference
Run predictions on large datasets periodically (hourly, daily).
- Predictions don't need to be immediate
- Large volume of predictions
- Complex models that are slow
Real-Time Inference (Online)
Serve predictions on-demand via API, typically sub-second latency.
- User-facing applications
- Decisions needed immediately
- Input data available at request time
Streaming Inference
Process continuous data streams, making predictions as events arrive.
- Data arrives continuously
- Need to react to events quickly
- State needs to be maintained
Edge Inference
Run models on device (phone, IoT, browser) rather than server.
- Privacy-sensitive data
- No/unreliable network
- Ultra-low latency required
🛠️ New to DevOps? Quick Crash Course
An API (Application Programming Interface) is a way for programs to talk to each other. Think of it like a waiter at a restaurant: you tell the waiter what you want (request), and they bring it back (response). Your model becomes an API endpoint that receives data and returns predictions.
A container packages your code + all its dependencies (Python version, libraries, etc.) into a single portable unit. It's like a shipping container: everything inside is bundled together and works the same anywhere. Docker is the tool that creates and runs these containers.
Kubernetes manages many containers across many machines. It handles scaling (run more copies when busy), healing (restart crashed containers), and routing (send requests to healthy instances). Think of it as the "operating system" for running containers in production.
Don't worry! You don't need to master all of this to understand ML systems. Most teams have dedicated infrastructure engineers. Your job is to know these exist and communicate effectively.
Serving Infrastructure Options
Model Servers
Dedicated servers for ML inference: TensorFlow Serving, Triton, TorchServe
Containers (K8s)
Package model + server in Docker, deploy on Kubernetes
Serverless
AWS Lambda, Google Cloud Functions, Azure Functions
Managed ML Platforms
SageMaker, Vertex AI, Azure ML
Hands-On: Deployment Artifacts
Here are production-ready templates for the most common deployment patterns. Copy and adapt these for your own projects.
# Dockerfile for ML Model Serving
FROM python:3.10-slim
WORKDIR /app
# Install dependencies first (cache layer)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy model and code
COPY model/ ./model/
COPY app.py .
# Expose port for inference
EXPOSE 8000
# Run with uvicorn
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
from fastapi import FastAPI
from pydantic import BaseModel
import torch
app = FastAPI()
# Load model at startup (not per-request!)
model = torch.load("model/model.pt")
model.eval()
class PredictionRequest(BaseModel):
text: str
class PredictionResponse(BaseModel):
prediction: str
confidence: float
@app.post("/predict", response_model=PredictionResponse)
async def predict(request: PredictionRequest):
with torch.no_grad():
# Your preprocessing here
output = model(preprocess(request.text))
pred_class = output.argmax().item()
confidence = output.softmax(dim=-1).max().item()
return PredictionResponse(
prediction=CLASS_NAMES[pred_class],
confidence=confidence
)
@app.get("/health")
async def health():
return {"status": "healthy"}
version: '3.8'
services:
ml-api:
build: .
ports:
- "8000:8000"
volumes:
- ./model:/app/model # Hot-reload models
environment:
- MODEL_PATH=/app/model/model.pt
- LOG_LEVEL=debug
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3
torch==2.0.1
fastapi==0.100.0
uvicorn[standard]==0.23.0
pydantic==2.0.0
numpy==1.24.0
Quick Commands
docker build -t my-model-api:v1 .
docker run -p 8000:8000 my-model-api:v1
curl -X POST http://localhost:8000/predict -H "Content-Type: application/json" -d '{"text": "Hello world"}'
Safe Deployment Strategies
Don't deploy new models directly to all users. Use progressive rollout strategies to catch problems before they affect everyone.
Shadow Deployment
Run new model in parallel, compare outputs without serving to users. Zero risk to users.
Canary Deployment
Gradually increase traffic to new model. Monitor metrics at each stage. Rollback if problems appear.
A/B Testing
Randomly assign users to variants. Measure business metrics (not just ML metrics). Statistical significance before decision.
Blue-Green Deployment
Maintain two production environments. Switch traffic instantly. Easy rollback by switching back.
Production Monitoring
ML models degrade silently. Unlike traditional software that crashes when broken, ML models continue producing outputs—just worse ones. You won't know unless you monitor.
System Metrics
Traditional DevOps monitoring—infrastructure health.
- Latency (p50, p95, p99)
- Throughput (requests/sec)
- Error rates
- CPU/Memory/GPU utilization
- Queue depths
Data Quality Metrics
Is the input data what the model expects?
- Missing values
- Schema violations
- Out-of-range values
- Data freshness
- Volume anomalies
Data Drift
Has the input distribution changed since training?
- Feature distributions (PSI, KL divergence)
- Embedding drift
- Covariate shift detection
Model Performance
Is the model still accurate? (Requires ground truth)
- Accuracy, precision, recall over time
- Performance by segment
- Prediction distribution changes
- Concept drift detection
Business Metrics
Is the model achieving its business goal?
- Conversion rates
- Revenue impact
- User engagement
- Customer satisfaction
Fairness Metrics
Are there disparities in how the model treats groups?
- Performance by demographic
- Prediction rate parity
- Error rate parity
Monitoring Without Ground Truth
When labels are delayed or unavailable, monitor proxy signals: prediction confidence distributions, prediction rate changes, user behavior (do users accept recommendations?), and input data drift. These can signal problems before you have ground truth.
Continuous Training and Feedback Loops
Static models decay. Production systems need mechanisms to retrain models on fresh data—automatically or with human oversight.
Scheduled Retraining
Retrain on a fixed schedule (daily, weekly, monthly). Simple but may retrain unnecessarily or not quickly enough.
Triggered Retraining
Retrain when drift or performance degradation is detected. More efficient but requires good monitoring.
Online Learning
Update model continuously as new data arrives. No discrete retraining—model always current.
Human-in-the-Loop Retraining
Fully automated retraining can propagate errors. Consider requiring human approval for:
- Model promotions to production
- Significant training data changes
- Models in high-stakes domains
- First deployment of new architectures
Common Misconceptions
"If the model works in testing, it'll work in production"
Test environments differ from production in data distribution, scale, latency requirements, and edge cases. Models that ace offline evaluation often struggle with real-world messiness.
The accurate framing: Offline evaluation is necessary but not sufficient. Use shadow deployments, canary releases, and continuous monitoring to validate in production.
"Deploy once and you're done"
The world changes. Data drifts. User behavior evolves. Competitors adapt. A model that's excellent today may be mediocre in three months.
The accurate framing: ML systems need continuous monitoring, regular evaluation, and mechanisms for updating. Deployment is the beginning, not the end.
"The ML code is the hard part"
In production systems, ML code is typically 5-10% of the codebase. The rest is data pipelines, serving infrastructure, monitoring, testing, and configuration management.
The accurate framing: ML engineering is mostly engineering. Data quality, infrastructure reliability, and operational excellence determine production success.
Interactive Lab: Deployment Decision Tree
Answer questions about your use case to get deployment architecture recommendations.
What is the latency requirement?
Key Considerations
- Latency: Determines real-time vs batch architecture
- Data location: Privacy and connectivity constraints
- Scale: Influences infrastructure complexity and cost
- Model size: Large models need more resources
Check Your Understanding
What is training-serving skew?
What is a canary deployment?
Why is data drift monitoring important?
What's the primary purpose of a feature store?
In a production ML system, approximately what percentage of code is typically ML-specific?
Congratulations!
- What AI actually is and how intelligence emerges from optimization
- Core ML methods: decision boundaries, neural networks, backpropagation
- Modern architectures: embeddings, attention, transformers, LLMs
- Generative AI: diffusion models, multimodal systems
- Evaluation, responsible AI, and production deployment
This foundation prepares you to dive deeper into specialized areas: computer vision, NLP, reinforcement learning, AI safety research, or applied ML engineering. Keep learning, stay curious, and build responsibly.