QPS (Queries Per Second) and System Design 00:00:01 From users to a rate A hundred million daily users tells a capacity planner how many people came through the door. It says almost nothing about how close together they arrived or how many database calls each person triggered. This example begins with a hundred million daily users of a short-message service. At fifty requests apiece, those users create five billion requests over the day. Spread over eighty-six thousand four hundred seconds, the average is about fifty-seven thousand eight hundred seventy per second. Suppose the peak is three times the average. It would be about a hundred seventy-four thousand requests per second. If the plan assumes ten thousand simple reads per second per database and uncached requests cost a read each, investigate caching, distribution and the actual workload before choosing a fleet size. Multiply people by the assumed actions per person, then divide by seconds in the interval. Make every assumption visible so somebody can replace the estimate with a trace. 00:01:02 Separate reads from writes A URL shortener: 10 million new links a day, 100 reads for each. Switch to a link shortener. In a day it accepts ten million new links and serves a billion redirects. That works out to about a hundred sixteen writes and eleven thousand five hundred seventy-four reads per second on average. At three times average, the estimates approach three hundred fifty writes and thirty-five thousand reads per second. Compare each operation with a representative test. Ten thousand simple reads a second is an assumption in this example. Under those assumptions one store fits the writes but not all peak reads. A cache for popular destinations is one option. A hundred reads and one durable write may have very different costs. The same total rate can require a different system after only the mix changes. 00:01:53 A gauge needs a denominator One server, tested at 100 QPS, through a busy hour. One worker was tested at a hundred queries per second. At forty arriving queries each second it has room to spare. Sixty-five per second is still below the illustrative seventy-percent operating target. At eighty-five per second, the original target has been crossed. Look at waiting time and plan additional ready capacity. 110 arrive and 100 finish. Ten more wait every second. If two ready workers split one hundred ten arriving queries evenly, each takes fifty-five a second. Compare the observed rate with a tested capacity for that particular operation. A utilization estimate derived from an unrelated workload can look precise while pointing to the wrong limit. 00:02:40 Choose capacity for the surviving fleet Peak 2,800 QPS. One server was tested at 1,000. For a peak of twenty-eight hundred requests per second, three thousand-per-second workers average about nine hundred thirty-three requests each. Four ready workers average seven hundred a second, seventy percent of their tested capacity. Remove one worker and the remaining three return to about ninety-three percent. With five active workers the average is five hundred sixty requests per second each. Lose one of those five and the remaining four each take seven hundred. Failover still has to redirect the actual traffic. Work out the busiest expected interval and repeat the arithmetic after a worker is removed. Spare capacity matters only if the remaining path can accept and complete the redirected traffic. 00:03:32 One client request can fan out 1,000 requests a second are not 1,000 queries a second. A thousand incoming requests per second, each doing one database lookup, produce a thousand queries per second. If each request instead issues twenty individual lookups, the database receives twenty thousand queries per second. A list query and one batched query for its items would reduce that to two thousand queries per second in this example. At 80% hits 400 remain. Only worth it if you need it. Count calls again at the database boundary. Caching, batching and a request that looks up several records all change how a rate seen at the entrance translates into work inside. 00:04:17 When the sale opens 100,000 people wait for a sale. The average day says 50 QPS. 4.3 million page views a day: 50 a second, 150 at three times. 100,000 people reload every 5 seconds at 9:00. Let 500 people in each second. The page of the waiting room is static and comes from a CDN. Everybody is inside after 200 seconds, and the database sees a steady load. A waiting room changes when attempts are admitted. It can protect the booking database from a burst, while making time in line part of the customer's experience. Put the rate on a named boundary. Distinguish arriving people, application requests, backend queries and completed work before using a single number to purchase machines.