Single logo
  • Courses
  • Blog
  • About
Join for freeJoin free
Learning pathServe a Model in Production: Docker, Latency and Scaling
  1. 1
    Online, Batch or Streaming? Picking the Serving Pattern for an ML Model
  2. 2
    A Production FastAPI for an ML Model: Input Limits, /health, /ready and a Model Version
  3. 3
    Batch Scoring a Million Rows: Chunks, a Run ID and a Safe Rerun
  4. 4
    Docker Essentials for ML: Images, Containers and Volumes with a Model API
  5. 5
    Containerising a Model API: A Dockerfile for FastAPI and scikit-learn, Line by Line
  6. 6
    Latency Math: p50, p95, p99 and Why the Tail Reaches Your Users
  7. 7
    Measuring Latency of an ML API: Timing Inside with OpenTelemetry, Outside with a Load Test and Locust
  8. 8
    Making ML Inference Fast: ONNX, Micro-Batching and Quantisation, Measured
  9. 9
    Sizing and Autoscaling an Inference Service: From One Replica to a Replica Count
  1. Blog
  2. /
  3. Machine Learning
  4. /
  5. MLOps and Production ML

Learning path: Serve a Model in Production: Docker, Latency and Scaling · Step 9 of 9

Sizing and Autoscaling an Inference Service: From One Replica to a Replica Count

How many copies of a model API do you need, and when do you add more? Measure one replica (it flattens at about 64 requests a second here), pick a row with a latency budget, size for a target with headroom, and scale early on the queue, not on CPU.

Chaand Sheikh

·27 November 2026Serving Models

What you will learn

  • Find what one replica can take from a concurrency sweep, and spot the knee
  • Pick an operating point with a p95 latency budget
  • Turn a traffic target into a replica count with headroom
  • Explain why startup time and queue length, not CPU, should drive autoscaling

Before you start

  • Latency Math: p50, p95, p99
  • Measuring Latency of an ML API

Our rain API answers well, and we know how to read its latency from Latency Math. But one copy only goes so far. How many copies do we need, and when do we add more?

Checkout lanes, and staff on the way

Think of a supermarket with a few checkout lanes open. A rush comes in and the lines get long, so the manager calls for more staff. The catch: those staff need ten minutes to get there. For those ten minutes the lines keep growing, and nothing the manager does can help. When the staff arrive, the lanes open and the lines slowly clear.

A lane is a replica, the travel time is startup time, the line is the queue, and the skill is calling staff before the rush reaches the door.

What one replica can take

Sizing starts with one replica. Here's the load test for one uvicorn worker on a local CPU (load test on the same machine), 640 requests per row, zero errors in every row:

Callers at onceRequests/sp95 (ms)
152.828.1
259.749.3
463.986.1
862.9171.6
1660.2356.2
3261.9717.2
6462.61,243.6
One replica flattens at about 64 requests a second knee: 4 callers, 63.9 req/s 0 64 1 2 4 8 16 32 64 callers at once (requests/s on the left; p95 grows from 28 to 1,244 ms)
One replica on this machine: throughput stops climbing at four callers. After that, extra callers only wait.

Throughput climbs from about 53 to 64 requests a second, then stops. From 8 to 64 callers it sits between 60 and 63, while p95 keeps rising the whole way. So four callers at once is the knee for this replica. About 64 requests a second is all it will give, and anything past that is just waiting in line.

Pick a row with a budget

Which row do we plan with? That depends on a latency budget. In capacity.py:

import math


def pick_concurrency(rows, budget_ms):
    """Best throughput whose p95 fits the budget."""
    ok = [r for r in rows if r["p95"] <= budget_ms]
    return max(ok, key=lambda r: r["rps"]) if ok else None

It keeps only the rows whose p95 fits the budget, then takes the highest throughput. With a budget of 100 ms, rows from 8 callers up drop out, and it picks concurrency 4: about 64 requests a second at a p95 of 86 ms. Tighten the budget to 50 ms and it picks concurrency 2: about 60 requests a second at 49 ms.

Nobody measures a budget. We choose it, and the table tells us what it costs.

From a target to twenty-three replicas

# capacity.py, below pick_concurrency
def instances_needed(target_rps, per_rps, headroom=0.7):
    usable = per_rps * headroom
    return math.ceil(target_rps / usable)
  1. Usable: we don't plan to run a replica flat out, so we take 70% of 63.9, about 44.8 requests a second.
  2. Divide: for a target of 1,000 requests a second, 1,000 / 44.8 is about 22.3.
  3. Round up: you can't run a third of a replica, so ceil gives 23.

The project's four tests for these helpers pass (4 passed). Still, the 1,000 and the 70% are our choices, and this assumes copies add up in a straight line. So 23 is an estimate. Load-test a small pool on your target hardware before you trust it.

A burst, and two minutes to wait

Twenty-three covers 1,000 requests a second, but traffic doesn't sit still. Let's play out a burst. The traffic and startup time here are for illustration; the arithmetic uses the measured 63.9 per replica.

  • We're at 500 requests a second. The same function says 12 replicas, which can do at most about 767.
  • Traffic jumps to 1,000. We want 11 more replicas, and say each takes two minutes to load its model and get ready.
  • For those two minutes, about 233 extra requests pile up every second: about 28,000 in the queue.
  • A request joining the back of that queue waits about 36 seconds.

Nothing has failed, and the service is already no use to anyone. Startup time belongs in the plan, and a big model on a GPU can take longer still.

Traffic above capacity during startup builds a queue traffic 500 traffic 1,000 new replicas ready capacity about 767 (12 replicas) queue grows about 233/s 2 minutes of startup (illustration): about 28,000 queued, about 36 s wait
For illustration: while new replicas start, every request above capacity joins the queue.

Scale on the queue, not on CPU

What should trigger the scale-up? The obvious answer is CPU. Look at the sweep again: alone, a request takes about 17.6 ms at p50. At 64 callers, about 1,017 ms. Throughput stayed flat, so the work per request hardly changed. Nearly all of that extra time is waiting in line.

A CPU graph can't show you that. CPU wasn't recorded in this run, but once a worker is busy all the time, CPU sits at the top whether four callers are waiting or sixty-four. And on a GPU service, CPU can look calm while the GPU is full.

Requests in flight can show it: that number went from 4 to 64 as the callers grew, and it tells you about the waiting right away. So scale on queue length or requests in flight. For text models, count tokens in flight, because one long input can cost as much as many short ones.

A simple scaling policy

No autoscaler ran in this project, so take this as a sketch:

  • Scale up early. The knee was four in flight per replica, so a trigger below that gives new copies time to arrive.
  • Scale down slowly. Wait until traffic has stayed low for a while, because you just paid for that startup time. Keep a minimum running.
  • Protect the replicas you have. If the queue is past a limit, say no quickly with a 503 instead of making everyone wait. Cap long inputs too, because one huge input can run a GPU out of memory.
  • Test it. Send a burst on purpose, and watch queue time separately from inference time.

Common mistakes

  • Sizing from the best p50 you ever saw. Size from a row that meets your p95 budget at its load.
  • Planning replicas at 100%. Leave headroom; here 70%.
  • Treating the replica count as a fact. It assumes copies add up in a straight line. Load-test a pool.
  • Ignoring startup time. A two-minute start can build a queue of tens of thousands of requests.
  • Scaling on CPU. A busy worker pins CPU whether 4 or 64 requests are waiting.

Quick recap

StepResult here
Measure one replicaflattens near 64 requests/s from 4 callers onward
Choose a budget100 ms p95 picks concurrency 4 (86 ms); 50 ms picks 2
Size for a target1,000 requests/s at 70% headroom: 23 replicas (estimate)
Plan for startup2-minute start: about 28,000 queued (illustration)
Pick the signalqueue length or requests in flight, not CPU

Try it yourself

Change the headroom to 50% in instances_needed. How many replicas do you get for 1,000 requests a second?

Built this? Share your solution in the StudyEasy AI Community.

Join free on Skool →
← Previous
Making ML Inference Fast: ONNX, Micro-Batching and Quantisation, Measured
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool
Go deeper with a course

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)

Related articles
Python Virtual Environments: One Project, One Toolbox

Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.

Git and GitHub Fundamentals: Commits, Branches, Push and Pull

The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.

Testing a FastAPI App with pytest: Three Small Tests

A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.

Config and Logging Instead of Hard-Coded Values in ML Training

Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.

On this page
  • Checkout lanes, and staff on the way
  • What one replica can take
  • Pick a row with a budget
  • From a target to twenty-three replicas
  • A burst, and two minutes to wait
  • Scale on the queue, not on CPU
  • A simple scaling policy
  • Common mistakes
  • Quick recap
  • Try it yourself
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool
Go deeper with a course

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.