AlgoViz

Throughput

easy 30 min

Throughput is how much work a system finishes per second.

Video lesson · Chapter 1 of 5

Four hundred requests arrive every second

0:00 / 4:23

One narrow lane decides

1:15Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Throughput is the amount of work completed in a unit of time: requests, transactions, messages or megabytes per second. Only finished, successful work counts.

Latency asks how long one request takes. Throughput asks how many we can finish per second. They are different questions: a wide road does not make a single car faster.

Most systems are a chain of stages. Work cannot leave the chain faster than its narrowest stage lets it through. That stage is the bottleneck.

Why it exists

If work arrives faster than it can be finished, it piles up. Waiting times grow, callers give up and try again, and the pile grows faster. Knowing the limit before the crowd arrives is the point of measuring it.

It also tells you what to buy. Widening a stage that is not the bottleneck changes nothing at all.

A short history

  1. 1961

    John Little proves a formula that had been used without proof: the average number in a system equals the arrival rate times the average time inside.

  2. 1984

    Eliyahu Goldratt's novel The Goal popularises the idea that a factory runs at the speed of its bottleneck.

  3. 1985

    Datamation prints "A Measure of Transaction Processing Power", prepared by Jim Gray with colleagues, a shared test for transactions per second.

  4. 1988

    The Transaction Processing Performance Council is founded to stop vendors from measuring in ways that suited themselves.

  5. 1999

    Dan Kegel's C10K page asks how one server can handle ten thousand clients at once.

  6. 2014

    Jay Kreps reports two million small writes a second into Kafka on three machines, and describes exactly how it was measured.

Words to know

Bottleneck
The stage with the lowest capacity.
Bandwidth
The most a link could carry. Throughput is what it does carry.
Concurrency
How many pieces of work are in progress at the same time.
Utilisation
Work done divided by the most that could be done.
Batch
Several operations sent as one.
Goodput
Throughput that is useful: successful, and on time.
Saturation
The point where extra load only makes queues longer.

How it works

Step through the idea, or press Play.

Step 1 of 3

Count. Completed, successful work per second

Understand it

Start here

The highway toll story

Imagine a highway with a toll booth. Count the cars that pass in a minute: that is throughput. How long one car needs is latency. More booths let more cars through, although each car still needs its ten seconds.

Six cars a minute, and how to get more
6 carsa minuteOne booth, 10 s a car

Cars, people and tonnes per minute are three throughputs of the same road. Always say what you count.

The idea

Throughput and latency, side by side

LatencyThroughput
AsksHow long for one?How many per second?
Unitmsrequests/s, MB/s, TPS
Better isLowerHigher
Is aboutThe user's experienceThe system's capacity
Example200 ms per request1,000 requests/s
Both examples can be true at once: 200 requests are then in progress. Latency is a topic of its own.

Planning for it

  • We know one server does 100 requests a second
  • The sale may bring 10,000 users, each asking once a second
  • 100 servers at full use, more like 143 at 70% plus one spare
  • Cache what can be cached, to need far fewer
  • Balance the load and rehearse it

Ignoring it

  • The system does 100 requests a second
  • 10,000 users arrive on sale day
  • Requests pile up until memory or patience ends
  • The site stops answering, and retries add more load
  • Lost sales, angry customers

Units

Four kinds of throughput

  • Requests (RPS, QPS)

    Web APIs and search. Small service: 100 to 1,000. Medium: 10,000 to 100,000. Very large: over a million.

  • Transactions (TPS)

    Databases and payments. One transaction is all or nothing and may hold several queries.

  • Data (MB/s, Gb/s)

    Files, video, backups. Watch bits and bytes: divide by 8.

  • Messages

    Queues and streams. Small messages cost more per byte than large ones.

A water park has the same choice: rides per hour, litres per minute through the pipes, tickets per hour at the gate. Different activities, different rates. How requests, queries and transactions per second differ is explained in Queries per second.

Rough sizes

ThingAboutBasis
Hard disk, sequential150 MB/sRule of thumb
SATA SSD500 MB/sNear the limit of the 6 Gb/s interface
NVMe SSD3,500 MB/sPCIe 3 drives; newer ones are faster
10 Gb/s network1,250 MB/sArithmetic
One relational database, simple lookups10,000/s to plan withRule of thumb, can go further
One Redis server, simple commands100,000/sRedis docs show a benchmark near 180,000
Kafka, three machines2 million small messages/sPublished 2014 test, 100-byte messages, 6 partitions
RabbitMQ, one queuetens of thousands/sRule of thumb, depends on durability
The database and Redis lines are the same as in Numbers that decide. Measure your own work before you promise a number.

Limits

Bandwidth is not throughput

Throughput: what flows

  • The cars that really pass
  • "Right now we push 3 Gb/s"
  • Measured

Bandwidth: what could flow

  • The number of lanes
  • "This link can carry 10 Gb/s"
  • A property of the link

What a link allows

1 Gb/s8=125 MB/s,125 MB/s1 KB=125,000 requests/s\frac{1\ \text{Gb/s}}{8} = 125\ \text{MB/s}, \qquad \frac{125\ \text{MB/s}}{1\ \text{KB}} = 125{,}000\ \text{requests/s}
A theoretical ceiling for 1 KB requests, before headers.

Factors

Five things that set it

A toy factory makes as many toys as its workers (concurrency), its doors (bottleneck), its machines (processing power) and its materials (starved resources) allow. The weakest one wins.

  1. 1

    Concurrency

    Four workers do up to 4 times the work of one, if they have cores to run on and do not wait for the same lock.

  2. 2

    Bottlenecks

    A fast API at 10,000 a second in front of a database at 100 a second is a system at 100 a second.

  3. 3

    Resource limits

    CPU 85%, memory 60%, disk I/O 95%: the disk is the one to look at.

  4. 4

    Network bandwidth

    Large answers fill the link long before the CPU is busy.

  5. 5

    Connection pool

    10 connections, each doing 50 requests a second (20 ms each), allow 500 a second.

The narrowest stage decides

Users send 2,000 requests a second through a load balancer to two API servers, each able to do 5,000 a second. Both query one database that can do only 100 a second, which limits the whole system to 100.
Two servers could do 10,000. The database does 100. The system does 100.

Throughput of a chain

X=min⁡i(workersitimei)min⁡(10,000, 100)=100/sX = \min_i \left( \frac{\text{workers}_i}{\text{time}_i} \right) \qquad \min(10{,}000,\ 100) = 100/\text{s}
Each stage can do workers divided by the time of one item. If a stage sees only some of the items, divide its capacity by that share.

Find the bottleneck

The last box is common: one test client cannot send enough.

The law

Little's law

Work in progress = throughput × latency

L=λW1,000/s×0.2 s=200 in progressL = \lambda W \qquad 1{,}000/\text{s} \times 0.2\,\text{s} = 200\ \text{in progress}
LL is the number inside, λ\lambda the rate and WW the time inside in seconds. Averages over a steady period, for any system.

Read it three ways

You knowYou getExample
Rate and timeHow many are in progress3,000/s × 5 ms = 15 connections busy
Workers and timeThe most you can finish200 threads / 0.2 s = 1,000/s
Number inside and rateThe time each takes500 waiting / 100/s = 5 s

Pushing past the limit

Work finished after the caller gave up is wasted. Reject early instead: a short, quick no keeps the rest fast.

Improve

Six ways to get more through

A supermarket opens more lanes (parallel), packs similar items together (batching), keeps an express lane (fast path) and weighs the fruit in advance (work done ahead).

StrategyExampleHelps when
Parallel processingThree requests of 100 ms: 300 ms in a row, 100 ms together, 3 times the throughputCores are idle or work waits for I/O
Batching1,000 inserts at 5 ms: 5 s. One insert of 1,000 rows: 50 ms, 100 times fasterEach call has a fixed cost
Caching1 ms from a cache, 10 ms from the databaseReads repeat
Load balancing10,000/s over 4 servers: 2,500/s eachServers are the bottleneck
Async and queuesThe API answers at once; two workers do the jobSpikes, and work nobody waits for
Connection poolingAbout 50 ms to open, about 0.1 ms to borrowOpening costs more than using
Times are example values. See load balancing, caching and queues.

A queue between fast and slow

Users send requests to an API server, which puts jobs on a queue and answers at once. Two worker machines take jobs from the queue and write the results to a database.
Orange is asynchronous. A queue smooths a spike. It does not help if workers are too few all day.

Batching: share the fixed cost

t(n)=f+n⋅vf=5, v=0.045 ms:t(1)≈5,t(1,000)=50 mst(n) = f + n \cdot v \qquad f = 5,\ v = 0.045\ \text{ms}:\quad t(1) \approx 5,\quad t(1{,}000) = 50\ \text{ms}
ff is the round trip and the commit, vv the work per row. Bigger batches add less and less, make the first row wait, and lose more when they fail.

One by one, or one batch

Stacking improvements, worked out

StepTime per request at the databaseOne machineThroughput
Nothing10 ms connect + 10 ms query2 connections100/s
+ Connection pool10 ms2 connections200/s
+ Cache, 80% hitsonly 1 request in 5 arrives2 connections1,000/s
+ Batch two queries into one5 ms each2 connections2,000/s
+ 4 machines, each with its datathe same8 connections8,000/s
An invented system, so that the steps can be checked. The last step holds only if the four machines do not share another bottleneck.

What a cache does to the store behind it

Xtotal=Xdb1−hh=0.8: ×5h=0.99: ×100X_{\text{total}} = \frac{X_{\text{db}}}{1 - h} \qquad h = 0.8:\ \times 5 \qquad h = 0.99:\ \times 100
Only misses reach the database. If hits fall from 99% to 98%, its load doubles.

Measure

How to measure it

Sit by the road with a clicker and count cars for an hour, or count one minute and multiply by 60. Both work, and both can mislead: the minute may be unusual.

  1. 1

    Counter

    Count up forever and subtract: 1,000,000 at 60 s and 1,060,000 at 120 s is 60,000 / 60 = 1,000 requests a second.

  2. 2

    Rate over a window

    Prometheus: rate(http_requests_total[5m]) is the average over 5 minutes. irate uses only the last two samples, so it jumps more. Both handle counters that restart at zero.

  3. 3

    Sampling

    Keep 1% at random: 100 a second measured means about 10,000. Good for detail, poor for rare events.

Four numbers on the dashboard (example)

  • Now: 1,247/s

    12% above the usual for this hour.

  • Peak: 3,891/s

    The highest so far, with its date and time.

  • Use: 62%

    Of tested capacity. 70% is the line to act on.

  • Good: 98.5%

    1.5% failed. Count goodput, not attempts.

Tools

KindTools
Metrics and dashboardsPrometheus, Grafana
APMDatadog
Load testingwrk, ab, k6, Locust, JMeter
Database benchmarkpgbench
Shell
wrk -t12 -c400 -d30s http://localhost:8080/apiab -n 10000 -c 100 http://localhost:8080/api
wrk: 12 threads, 400 connections, 30 seconds. ab: 10,000 requests, 100 at a time. Only test systems you own.

Plan

From throughput to machines

Scale which part?

How to turn daily users into a peak is in Queries per second.

Summary

The whole topic in one minute.

Throughput is finished work per second, and the narrowest stage sets it. Find that stage, widen it, and look for the next one.

Work in progress = rate × time. With a fixed number of workers, slower work means less throughput. Parallelism, batching, caching, balancing, queues and pooling each help one kind of bottleneck.

  • Throughput is finished, successful work per second.
  • The narrowest stage sets it. Widen that one only.
  • In progress = rate × time.
  • Parallelism raises throughput without making one request faster.
  • Past saturation, throughput stays flat or falls. Say no early.

Sources