Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 2 of 6 · Worked example · 20 min · revision 1

Sizing a tier: per-instance capacity, headroom and Little's law

Size a stateless API tier step by step: turn online users into a request rate, check a load-test result against Little's law, choose headroom from queueing behaviour, add zone-failure capacity and size connection pools, with a best, expected and worst case.

Contentsand lesson path

What you will learn

  • Size a stateless tier from peak load, per-instance capacity and headroom, using Little's law.

01. The problem

You run the order-tracking API of a food delivery app. While an order is on its way, the app polls this API for the courier's position and the estimated arrival time. The API is stateless: it reads from a database and keeps nothing between requests. Your job is to decide how many instances the tier needs at the evening peak, and how big each instance's database connection pool should be.

Every number below is an assumption for this exercise, not data from a real company. In real work, each one comes from a measurement or a forecast, and you write it down so that others can challenge it:

  • At the evening peak, about 180,000 app sessions are open, and each open app polls once every 25 s. A forecast range for the request rate is 6,000 to 9,000 requests per second.
  • The latency target (SLO) is p99 under 300 ms.
  • A load test of one instance found: 32 worker threads, a mean service time of 60 ms per request at low load, and p99 staying under 300 ms up to 450 requests/s, then crossing it.
  • Each request holds a database connection for 12 ms on average.
  • Instances are spread evenly over 3 availability zones, and the tier must survive losing one zone.

The tool that ties these numbers together is Little's law: for any system with a clear boundary, observed over a period long enough that it is in a steady state, the average number of items inside equals the arrival rate times the average time each item spends inside, or L = λ × W. The prerequisite skill, Requirements and estimation, covers how to estimate a peak from usage; here you turn a peak into machines.

02. Step 1: Turn online users into a request rate

The first trap is to treat 180,000 online users as 180,000 requests per second, or as 180,000 requests in flight. Neither is right. A user spends almost all their time looking at the map, not waiting for the API. Each user cycles between waiting for a response (R, about 0.07 s) and think time (Z, here the 25 s polling interval).

Little's law applied to the whole loop of users gives the response time law: users = X × (R + Z), where X is the request rate. So X = 180,000 ÷ 25.07 ≈ 7,180 requests per second, which matches the middle of the forecast; plan with 7,200/s. Now apply Little's law to a different boundary, the API tier only: requests in flight L = 7,200/s × 0.07 s ≈ 504. The same app has 180,000 users online and about 500 requests inside the tier at any moment. Always say which boundary your L belongs to.

03. Step 2: Check the load-test result with Little's law

Per-instance capacity must come from a measurement, but Little's law gives you a sanity check. Inside one instance, at most 32 requests can be in service at once. With a mean service time of 0.06 s, the instance cannot complete more than 32 ÷ 0.06 ≈ 533 requests per second, however the load test was run. The measured knee, 450/s, sits below that ceiling, which is what you expect: queues build up and the p99 crosses the target before all 32 workers are busy every instant.

If a load test ever reports more than the ceiling, distrust the test before you trust the number. Common causes are responses served from a cache, fast error responses counted as successes, or a smaller payload than production. Also check how the test generated load. A tool that waits for each response before sending the next request slows down exactly when the server does, and can hide the queueing you are trying to measure; the Performance antipatterns skill covers open- and closed-loop load generators.

04. Step 3: Choose the headroom

Why not plan to run each instance at its 450/s knee? Because waiting time grows steeply as utilisation, the fraction of time a resource is busy, approaches 100%. The simplest queueing model, M/M/1 (one server, random arrivals, steady state), shows the shape:

Python · queue_model.py
# M/M/1 model: one server, random arrivals, steady state. Not a measurement.
for rho in (0.5, 0.7, 0.8, 0.9, 0.95):
    print(f'utilisation {rho:.0%}: time in system {1 / (1 - rho):4.1f} x service time')
Expected output (a model, not a measurement)
utilisation 50%: time in system  2.0 x service time
utilisation 70%: time in system  3.3 x service time
utilisation 80%: time in system  5.0 x service time
utilisation 90%: time in system 10.0 x service time
utilisation 95%: time in system 20.0 x service time

An instance with 32 workers behaves better than this single-server model until it is close to saturation, but the hockey-stick shape is the same, and it is worse for the tail than for the mean. Headroom also has to absorb things the model ignores: forecast error, bursts that are higher than the per-minute peak, a slower dependency raising the service time, a deploy that takes instances out, and the minutes an autoscaler needs to add capacity (lesson 04). A common choice is to plan for 60 to 75% of the measured knee at the forecast peak. This example uses 70%, so each instance is planned at 450 × 0.7 = 315 requests per second.

05. Step 4: Count instances, then add zone failure

For the peak alone you need 7,200 ÷ 315 = 22.9, so 23 instances; always round up. Now the failure requirement. With instances spread evenly over three zones, losing a zone leaves two thirds of them, and those must still carry the peak. Each zone therefore needs ceil(23 ÷ 2) = 12 instances, 36 in total. The general rule for Z zones is Z × ceil(N ÷ (Z − 1)). The price is visible: 36 instances instead of 23 for this availability target; with only two zones it would be 46.

Check the result the other way round. With one zone lost, 24 instances share 7,200/s, which is 300/s each: under the 315/s plan and well under the 450/s knee.

06. Step 5: Size the pools and give a range

Little's law sizes the database pool too. Draw the boundary around one instance's connections: in the worst planned case, 300 requests/s each hold a connection for 0.012 s, so on average 300 × 0.012 = 3.6 connections are busy. A pool of about 8 per instance leaves room for bursts. Then add it up across the tier: 36 instances × 8 = 288 connections, while PostgreSQL's max_connections is typically 100 out of the box. Either raise the limit carefully, put a connection pooler in front of the database, or use smaller pools. Instance count and pool size are one decision, not two.

Finally, a single answer hides how uncertain the inputs are. The script below repeats the whole calculation for a best, expected and worst case, varying the peak and the measured knee:

Python · sizing.py
"""Sizing the order-tracking tier. Every input is an assumption for this exercise."""

import math

USERS_ONLINE = 180_000  # app sessions open at the evening peak
THINK_S = 25.0          # one status poll per user every 25 s
WORKERS = 32            # worker threads per instance
SERVICE_S = 0.060       # mean service time per request at low load
TARGET = 0.70           # run at no more than 70% of the measured knee
ZONES = 3               # instances spread evenly, must survive losing one zone
DB_HOLD_S = 0.012       # mean time a request holds a database connection

# Response time law (Little's law with think time): users = X * (R + Z)
print(f'peak from users: {USERS_ONLINE / (THINK_S + 0.07):,.0f} req/s')
print(f'worker ceiling per instance: {WORKERS / SERVICE_S:.0f} req/s')


def size(label: str, peak: int, knee: int) -> None:
    per_instance = knee * TARGET
    for_peak = math.ceil(peak / per_instance)
    per_zone = math.ceil(for_peak / (ZONES - 1))
    rate_zone_lost = peak / (per_zone * (ZONES - 1))
    print(
        f'{label:<8} peak {peak:>5}  knee {knee}  for peak {for_peak:>2}'
        f'  total {per_zone * ZONES:>2}  zone lost {rate_zone_lost:3.0f}/s'
        f'  db conns busy {rate_zone_lost * DB_HOLD_S:.1f}'
    )


size('best', 6_000, 500)
size('expected', 7_200, 450)
size('worst', 9_000, 380)
Expected output
peak from users: 7,180 req/s
worker ceiling per instance: 533 req/s
best     peak  6000  knee 500  for peak 18  total 27  zone lost 333/s  db conns busy 4.0
expected peak  7200  knee 450  for peak 23  total 36  zone lost 300/s  db conns busy 3.6
worst    peak  9000  knee 380  for peak 34  total 51  zone lost 265/s  db conns busy 3.2

Read the result as a decision, not a fact: plan for about 36 instances, let the autoscaler go up to about 51 if the worst case arrives, and say which measurement would narrow the range. Here the knee matters as much as the peak, so a second load test with production-like data is worth more than a better traffic forecast.

07. Check your understanding

Practice check · 3 questions

Sizing with Little's law

Three sizing judgements with new numbers.

Q1. During a 10-minute flash sale, requests arrive at 1,200/s while the tier completes only 1,000/s. An analyst averages the number of requests inside the tier over those 10 minutes and divides it by 1,200/s to report the average time a request spends inside. What is wrong with this?
Q2. An instance runs 8 worker processes, and each request needs 25 ms of a worker's time. A load test reports 600 requests/s per instance with flat latency. What should you conclude?
Q3. For the same peak, plan A runs 10 instances at 90% of their measured knee and plan B runs 13 instances at 70%. Why do most teams choose B?
Practice only: not graded, not saved, not evidence.

08. Apply it

Size a tier you know, or the cart service from the lab of this skill, with the same five steps. Write down every assumption with its unit and its source (measured, forecast or guessed). Then change the two most uncertain inputs and see how much the instance count moves. If one input dominates the result, that is the next thing to measure. For the lab service, python client.py bench measures throughput at different instance counts, the data for the scalability fit in lesson 05. It shows where throughput flattens, not the latency knee: for the knee you also need latency percentiles at each load level.

09. Review prompt

Without looking back: state Little's law and the two conditions that make it safe to use; explain why 100,000 users online is not the same as 100,000 requests in flight; show how a worker count and a service time give an upper bound for one instance; explain why you plan below the measured knee; and write the formula for the number of instances across Z zones that must survive the loss of one.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check