By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Model serving is the bridge that takes a trained machine‑learning model and makes it available to other software (web apps, mobile apps, downstream pipelines) via an API. In production you expose the model as a REST endpoint, a FastAPI/Flask service, or a gRPC method, then send new data to get predictions on‑demand (online) or in bulk (batch). Why it matters: without a reliable serving layer, a model that scores 0.95 AUC in notebooks never reaches users. Real‑world example: a SaaS churn‑prediction model is trained nightly, saved as churn.pkl, and a Flask micro‑service returns {"prob":0.82} for each incoming customer record, enabling the sales team to prioritize outreach in real time.
churn.pkl
{"prob":0.82}
POST /predict
FROM python:3.11
COPY model.pkl /app/
CMD ["uvicorn","app:app","--host","0.0.0.0"]
joblib.dump(model, "model.pkl")
tf.saved_model.save(model, "saved_model/")
python # sklearn example import joblib joblib.dump(best_clf, "model.pkl") # TensorFlow example tf.saved_model.save(tf_model, "saved_model/")
/predict
app = FastAPI() model = joblib.load("model.pkl") # cold‑start load
@app.post("/predict") def predict(payload: dict): df = pd.DataFrame([payload]) prob = model.predict_proba(df)[:, 1][0] return {"probability": float(prob)} 3. Containerize the service with Docker, pinning Python and library versions.dockerfile FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"] `` 4. Deploy to a cloud platform (Kubernetes, AWS ECS, GCP Cloud Run). Set horizontal pod autoscaling on CPU or request latency. 5. Monitor & Log – expose Prometheus metrics (/metrics`), log request IDs, latency, and error rates. Hook alerts to PagerDuty for > 95th‑percentile latency breaches. 6. Iterate – when a new model version is ready, push a new Docker image, roll out via a canary deployment, and compare KPI (e.g., churn lift) before full promotion.
3. Containerize the service with Docker, pinning Python and library versions.
`` 4. Deploy to a cloud platform (Kubernetes, AWS ECS, GCP Cloud Run). Set horizontal pod autoscaling on CPU or request latency. 5. Monitor & Log – expose Prometheus metrics (
np.float64
float(prob.item())
jsonable_encoder
os.getenv
Scenario: Your churn API latency spikes to 2 seconds after a weekend of heavy traffic. Answer: Scale out the service (add more pods) and enable a warm‑up request to keep the model in memory. Why: More replicas share the load; warm‑up avoids repeated cold starts.
Scenario: You need to serve a 10‑GB recommendation model to 5 K RPS with < 50 ms latency. Answer: Use gRPC with a GPU‑enabled pod and enable request batching (e.g., batch size = 32). Why: Binary protocol reduces overhead; batching amortizes GPU kernel launch cost.
Scenario: A client sends a JSON payload with a missing feature column. Answer: Return a 400 error with a clear validation message; do not fall back to default predictions. Why: Silent failures corrupt downstream decisions and hide data‑quality issues.
churn:v1.2.3
sklearn
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.