Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 153/ 160
08. MLOps & Model Deployment

Serverless Model Deployment

Deploy lightweight models on serverless platforms with cold-start awareness.

AdvancedServerless · Cold Start3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Serverless lets you run your model only when someone asks for a prediction, and you pay just for that time, but the first request after a quiet period is slower.

Think of it like…

A taxi versus owning a car. For occasional trips you pay per ride and never worry about parking or maintenance, but each pick-up has a wait (the cold start) and past a certain number of trips a month, owning is cheaper.

Where you’ve seen it

A scoring model for a sporadically used internal tool is a 40 MB ONNX gradient-boosted model. It runs on an AWS Lambda container image (zip packages are limited to 250 MB unzipped, container images can be up to 10 GB). The handler loads the model into a global variable outside the handler function so warm invocations reuse it. A cold start costs about 2.7 seconds (image pull, runtime start, importing libraries, loading the model, illustrative), while a warm invocation takes about 15 ms. If 1% of requests hit cold starts, p50 stays near 20 ms but p99 is about 2.7 seconds, so the team measures p99 and cold-start rate, not just averages. Cost at illustrative pricing of $0.0000166667 per GB-second plus $0.20 per million requests: a 1 GB function running 0.1 seconds uses 0.1 GB-seconds = $0.00000167, plus $0.0000002 per request, about $0.00000187 per invocation, or $1.87 per million. A small always-on server at $70 a month breaks even at 70 / 0.00000187 ≈ 37.4 million requests a month, an average of 14.4 requests a second (37.4M / 2,592,000 seconds), so below that rate serverless is cheaper, and above it a container service with steady load is. Lambda allocates CPU in proportion to memory (about one vCPU at 1,769 MB), so raising memory can reduce duration enough to cost less overall; the team benchmarks 512 MB, 1,024 MB and 2,048 MB. Options to tame cold starts: provisioned concurrency (keeps N instances warm at an hourly charge), minimum instances on Cloud Run, smaller images (ONNX Runtime instead of full scikit-learn and pandas), lazy loading and snapshotting features where available. Cloud Run is attractive for ML because one instance handles many concurrent requests (up to 80 by default), amortising the model load, and supports larger containers. The service is fronted by an API gateway with authentication and throttling, validates inputs with the shared contract, logs the model version and sets timeouts and a reserved concurrency cap so a traffic spike can't exhaust the downstream database. Models over a few hundred MB, deep networks needing a GPU or workloads with steady high traffic move to a container service or managed endpoint.

Words you’ll hear

Cold start
The extra latency when a new instance must start and load the model.
Provisioned concurrency / min instances
Pre-warmed instances kept ready to avoid cold starts.
GB-second
The unit of serverless compute billing: memory in GB times seconds of runtime.
Reserved concurrency
A cap or guarantee on simultaneous executions of a function.

Visual diagram

Loading interactive diagram

Theory & deep dive

How serverless inference behaves

A serverless platform runs your code in an execution environment that it creates on demand. A cold start includes pulling the image or package, starting the runtime, importing libraries and loading the model; with a 40 MB ONNX model and a lean image this may take about 2.7 seconds. A warm invocation reuses a live environment, so only prediction time (around 15 ms) remains. Put the model load in module scope (outside the handler) so it runs once per environment; loading inside the handler repeats it on every request. Instances are kept warm for a limited and unpredictable time after traffic, so cold-start frequency depends on request patterns: steady traffic keeps environments warm, sparse or bursty traffic creates many cold starts, and a spike that needs 100 concurrent instances causes 100 cold starts at once.

Limits shape design: package size (Lambda zip 250 MB unzipped, container images up to 10 GB), memory and CPU (CPU scales with memory on Lambda), timeouts (15 minutes on Lambda), concurrency (account and function limits) and no GPU on most functions. Prefer ONNX Runtime or a lean scikit-learn build over heavy frameworks, trim image layers and avoid importing pandas if you only need NumPy. Cloud Run runs containers with request concurrency (default 80 requests per instance), so one loaded model serves many simultaneous requests, which reduces the number of cold starts and the memory cost per request.

Latency profile

99 of 100 requests hit warm instances at about 15 ms; 1 of 100 is cold at about 2.7 s. p50 ≈ 15–20 ms, p99 ≈ 2.7 s, mean ≈ 0.99 × 15 ms + 0.01 × 2,700 ms ≈ 42 ms. The mean looks fine; the p99 is what a user waiting on a cold start experiences.

Key takeaway: Load the model at module scope, measure the cold-start rate and p99 and choose runtimes and images that keep initialisation small.

Cost, break-even and when to choose something else

Serverless pricing combines compute (GB-seconds) and requests. At illustrative prices of $0.0000166667 per GB-second and $0.20 per million requests, a 1 GB function running 0.1 seconds costs 0.1 × 0.0000166667 = $0.00000167 plus $0.0000002, about $0.00000187 per call, or $1.87 per million. A $70-a-month server therefore breaks even at 70 / 0.00000187 ≈ 37.4 million requests a month, an average of 14.4 requests a second; below that, serverless is cheaper (and has no idle cost), above it a container service running steadily costs less. Tune memory: because CPU scales with memory, a 2 GB function that runs in 40 ms can cost less than a 512 MB one that takes 200 ms; measure with a power-tuning approach rather than guessing.

Reduce cold-start pain with provisioned concurrency or minimum instances (you pay for always-warm capacity, which narrows the cost advantage), smaller images, lazy loading of rarely used components and keeping the function warm only when the latency SLO truly demands it. Choose something else when models exceed a few hundred MB, need a GPU, require sub-100 ms p99 without paying for warm capacity, or serve steady high traffic: use a container service (Cloud Run with minimum instances, ECS, Kubernetes) or a managed endpoint. Production hygiene is the same as any service: authentication, input validation, timeouts, a reserved concurrency cap to protect downstream systems, structured logs with model version, monitoring of cold-start rate and errors and tests that run the exact deployed image.

Break-even

Per call ≈ $0.00000187. Server $70/month. Break-even = 70 / 0.00000187 ≈ 37.4M requests ≈ 14.4 req/s average. At 2M requests a month serverless costs about $3.74; at 100M, about $187, and a pair of small servers is cheaper.

Key takeaway: Compute the request-volume break-even, tune memory for cost and move to containers or managed endpoints when load, size or latency demand it.

How it works, step by step

Step 1 of 4
ML engineer

1. Check fit

Confirm the model is small, CPU-friendly and traffic is spiky or low, and estimate the break-even request volume.

break-even ≈ 37.4M req/month
Key numbers at this step
Limits
size, memory, timeout

When to use it & trade-offs

What it is

Serverless model deployment runs inference in platforms such as AWS Lambda, Google Cloud Run or Cloud Functions and Azure Functions, where you supply code or a container and the provider starts instances on demand, scales them (including to zero) and bills by requests and compute time rather than by always-on servers. The central trade-off is the cold start: when no warm instance exists, the platform must pull the image, start the runtime and load the model into memory before it can answer, which adds seconds to that request. Serverless suits small, CPU-friendly models (linear models, gradient-boosted trees, small ONNX networks), spiky or low traffic and teams that want no server management; it is a poor fit for large models, GPUs and strict low-latency guarantees without extra configuration such as provisioned concurrency or minimum instances.

Why it matters: A model called a few thousand times a day does not justify a server running 24/7, and traffic that jumps from zero to hundreds of requests a second punishes fixed capacity. Serverless matches cost to use and removes patching and capacity planning. The risks are practical: package-size limits, model load time at every cold start, concurrency and memory limits, noisy tail latency and costs that overtake a server beyond a certain request volume. Estimating those trade-offs before choosing avoids surprises.

When to reach for it

  • Internal tools and low-traffic scoring APIs
  • Event-driven scoring triggered by uploads or messages
  • Prototypes that need public endpoints without servers

When to avoid or be careful

  • Large deep-learning models and GPU inference on functions
  • Loading the model inside the request handler
  • Judging latency by the mean instead of p99

Where to host a small model

Serverless function
Best for: Sporadic traffic and small models

Strengths: Scales to zero; no servers; pay per use.

Watch out: Cold starts; size and timeout limits.

Container service with min instances (Cloud Run, ECS)
Best for: Moderate steady traffic and larger images

Strengths: Concurrency per instance; fewer cold starts.

Watch out: Pays for warm minimums.

Managed endpoint or Kubernetes
Best for: High volume, GPUs and strict latency

Strengths: Predictable performance; full control.

Watch out: Always-on cost and more operations.

Interview signals & numbers to know

When this topic comes up

  • “When would you deploy a model on Lambda?”
  • What a cold start includes
  • Cost break-even against a server
  • Provisioned concurrency and min instances
  • Why Cloud Run's request concurrency helps

Benchmarks & rules of thumb

≈ $1.87 -
cost per million 1 GB × 100 ms invocations at illustrative prices
≈ 37.4M req/month -
break-even versus a $70/month server (≈ 14.4 req/s)
250 MB / 10 GB -
Lambda zip and container image size limits

Common mistakes & follow-ups

Common interview mistakes

  • Ignoring cold starts until the p99 alerts fire.
  • Keeping a 1.5 GB framework image for a 40 MB model.
  • Not capping concurrency and overwhelming the downstream database.

Likely interviewer follow-ups

  • How would you reduce cold-start latency?
  • At what traffic level would you move to containers?
  • How would you secure a public serverless endpoint?
Interview tip: Compute the break-even request rate; it turns a vague serverless-versus-server debate into a number.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Serverless Model Deployment with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Cloud ML Platforms (AWS SageMaker / GCP Vertex AI)
Next topic
Model Registry & Stage Transitions