Latency, throughput and availability
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)On sale day ShopLite's owner says: "The site felt slow, and last week it was down for an hour." Slow and down are feelings. To design for them, you need numbers.
Your turn: put numbers on "slow" and "down"
Last Friday ShopLite's checkout felt slow, and the site was down for an hour. Before you read on, decide on paper:
- What would you measure to say "slow": the time of one request, or how many requests finish each second?
- If 1 request in 100 takes 5 seconds and the rest take 50 ms, is the site fast? Which number tells you?
- One hour of downtime in a 30-day month: what percentage of the month is that?
Latency vs throughput: the checkout queue
Latency is the time from sending a request to getting the full answer, usually in milliseconds (ms). Throughput is how many requests the system finishes per second (RPS).
Think of billing counters in a supermarket. The example sends 12 customers through one counter, then through three, and prints the time at checkout and the customers billed per hour.
Example 1 · Three counters triple throughput, the bill still takes 2 minutes
public class CheckoutQueueDemo { static void simulate(int counters) { int customers = 12; int billMinutes = 2; int totalTimeInShop = 0; int lastFinish = 0;
for (int i = 0; i < customers; i++) { int round = i / counters; // how many customers this counter served before int finish = (round + 1) * billMinutes; // wait for those, then get billed totalTimeInShop += finish; lastFinish = Math.max(lastFinish, finish); }
double throughputPerHour = customers * 60.0 / lastFinish; System.out.printf("%d counter(s): bill takes %d min, average time at checkout %.1f min, %.0f customers/hour%n", counters, billMinutes, (double) totalTimeInShop / customers, throughputPerHour); }
public static void main(String[] args) { simulate(1); simulate(3); }}1 counter(s): bill takes 2 min, average time at checkout 13.0 min, 30 customers/hour 3 counter(s): bill takes 2 min, average time at checkout 5.0 min, 90 customers/hour
What this code does
- With one counter the 12th customer waits 22 minutes before a 2-minute bill, so the average time at checkout is 13 minutes and throughput is 30 customers an hour.
- Three counters bill 90 customers an hour and cut the average to 5 minutes, but the billing time itself is still 2 minutes. More servers help latency only by cutting the queue.
The 2 minutes one bill takes is the work time of one request. The wait in the queue adds to it, and the total is the latency a user feels. More counters raise throughput, but each bill still takes 2 minutes.
Percentiles and the slow tail
p50 / p95 / p99 are percentiles: the time within which 50, 95 or 99 in 100 requests finish. To compute one, sort the timings and pick the value at that rank.
An average hides slow users; percentiles show them. The first example makes 1,000 fake ShopLite requests, 2 in 100 of them slow, and prints the average next to p50, p95 and p99.
Example 2 · 1,000 fake requests: average vs p99
import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Random;
public class PercentileDemo { // Nearest-rank percentile: the value below which p% of the sorted timings fall. static long percentile(List<Long> sorted, double p) { int rank = (int) Math.ceil(p / 100.0 * sorted.size()); return sorted.get(Math.max(rank, 1) - 1); }
public static void main(String[] args) { Random random = new Random(7); List<Long> timings = new ArrayList<>(); for (int i = 0; i < 1000; i++) { if (random.nextInt(100) < 2) { timings.add(800L + random.nextInt(700)); // 2 in 100 requests hit a slow query } else { timings.add(20L + random.nextInt(60)); } }
long sum = 0; for (long t : timings) { sum += t; } Collections.sort(timings);
System.out.println("Requests: " + timings.size()); System.out.println("Average: " + sum / timings.size() + " ms"); System.out.println("p50: " + percentile(timings, 50) + " ms"); System.out.println("p95: " + percentile(timings, 95) + " ms"); System.out.println("p99: " + percentile(timings, 99) + " ms"); System.out.println("Max: " + timings.get(timings.size() - 1) + " ms"); }}Requests: 1000 Average: 65 ms p50: 49 ms p95: 77 ms p99: 976 ms Max: 1478 ms
What this code does
- The timings are made up with a seeded
Random, so every run prints the same numbers. 98% of requests take 20-79 ms and 2% hit a slow query of 800-1,499 ms. - The average of 65 ms and the p95 of 77 ms look healthy, yet p99 is 976 ms: about 10 users in every 1,000 wait almost a second.
percentilesorts first, then picks the value at rank ⌈p/100 × n⌉.
Example 3 · One slow call in a hundred becomes most page loads
public class TailFanoutDemo { public static void main(String[] args) { // Each backend call is slow 1% of the time (its p99). // A page that waits for N calls is slow if ANY one of them is slow. double slowChance = 0.01; int[] callsPerPage = {1, 5, 10, 50, 100};
for (int n : callsPerPage) { double pageSlow = 1 - Math.pow(1 - slowChance, n); System.out.printf("%3d calls per page -> %5.1f%% of page loads are slow%n", n, pageSlow * 100); } }}1 calls per page -> 1.0% of page loads are slow 5 calls per page -> 4.9% of page loads are slow 10 calls per page -> 9.6% of page loads are slow 50 calls per page -> 39.5% of page loads are slow 100 calls per page -> 63.4% of page loads are slow
What this code does
- The chance that at least one of n calls is slow is 1 − 0.99ⁿ. With 1 call it is 1%; with 100 calls it is 63.4%.
- So a backend p99 becomes the typical page time for pages that fan out to many calls. Keeping the tail low matters more as systems grow.
With 1,000 timings, p99 is the 990th smallest. Teams set targets on percentiles, such as "p99 under 500 ms", because a percentile says how many users had a bad experience.
Tail latency means the slowest requests, p99 and above. It matters more when one page needs many backend calls: if each call is slow 1% of the time, a page that waits for 100 calls is slow most of the time (the second example).
Fix the error
Fix the percentile crash
Read the error, then pick the line that fixes it.
p100 of 5 sorted timings crashes. Pick the line that fixes it.
Error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5
Availability: nines, series and copies
Availability is the percentage of time a system answers correctly, often written as nines: 99.9% is three nines. Each extra nine cuts the allowed downtime by ten.
Availability is a budget of minutes. The first example turns nines into minutes of downtime. The formulas below show what happens when parts are chained or copied.
series: A = A1 × A2 × A3 (every part needed)parallel: A = 1 − (1 − A1)ⁿ (n copies, any one is enough)[Phone] --> [App 99.9%] --> [MySQL 99.9%] = 99.80%[Phone] --> [App x2 = 99.9999%] --> [MySQL 99.9%] = 99.90%Parts are in series when a request needs every one of them, such as the app server and then the database: multiply their availabilities. A chain is always less available than its weakest part. Parts are in parallel when they are copies and any one can serve the request: the group is down only when all copies are down at once. This is redundancy: extra copies that remove a single point of failure.
Example 4 · Nines turned into minutes of downtime
public class NinesDemo { public static void main(String[] args) { double minutesPerMonth = 30 * 24 * 60; // a 30-day month: 43,200 minutes double minutesPerYear = 365 * 24 * 60; // 5,25,600 minutes double[] targets = {99.0, 99.9, 99.95, 99.99, 99.999};
System.out.println("Target Down per month Down per year"); for (double target : targets) { double downShare = (100 - target) / 100; System.out.printf("%-8s %8.1f min %8.1f hours%n", target + "%", downShare * minutesPerMonth, downShare * minutesPerYear / 60); } }}Target Down per month Down per year 99.0% 432.0 min 87.6 hours 99.9% 43.2 min 8.8 hours 99.95% 21.6 min 4.4 hours 99.99% 4.3 min 0.9 hours 99.999% 0.4 min 0.1 hours
What this code does
- The downtime share is (100 − target) / 100, multiplied by 43,200 minutes in a 30-day month or 5,25,600 minutes in a year.
- Going from 99.9% to 99.99% shrinks the monthly budget from 43.2 to 4.3 minutes, which is why each extra nine costs so much more.
Example 5 · Series multiplies, parallel copies add nines
public class SeriesParallelDemo { // In series: every part must be up, so multiply the availabilities. static double series(double... parts) { double result = 1.0; for (double a : parts) { result *= a; } return result; }
// In parallel (copies): the group is down only when every copy is down. static double parallel(double a, int copies) { return 1.0 - Math.pow(1.0 - a, copies); }
static String pct(double a) { return String.format("%.4f%%", a * 100); }
public static void main(String[] args) { double app = 0.999; double db = 0.999;
System.out.println("App alone: " + pct(app)); System.out.println("App -> DB in series: " + pct(series(app, db))); System.out.println("2 app servers: " + pct(parallel(app, 2))); System.out.println("2 app servers -> DB: " + pct(series(parallel(app, 2), db))); System.out.println("2 apps -> 2 DB copies: " + pct(series(parallel(app, 2), parallel(db, 2)))); }}App alone: 99.9000% App -> DB in series: 99.8001% 2 app servers: 99.9999% 2 app servers -> DB: 99.8999% 2 apps -> 2 DB copies: 99.9998%
What this code does
- App and database in series give 99.8%, lower than either part alone. Two app servers in parallel give 99.9999%, but the chain is still held back by the single database at 99.9%.
- Only when the database also has a second copy does the whole path reach 99.9998%. Redundancy helps only if every part of the chain gets it.
Pick the lowest target the product really needs: 99.9% (about 43 minutes a month) suits a shop checkout, and a staff report can live with 99%. More nines need copies of everything and cost much more.
Back to your turn: one hour of downtime is 60 / 43,200 = 0.14% of a 30-day month, so last Friday left ShopLite at about 99.86%, just under the 99.9% target.
Predict the output
Which change helps most?
Read the code and pick what it prints.
Round 1 of 2
ShopLite's app tier has two copies, MySQL has one. Which change raises the whole path most?
Next step
Check your understanding
11 quick questions