AlgoViz

Queries per second

easy 30 min

QPS is how many queries arrive, or can be answered, each second.

See it run

From people and days to a rate, from the rate to a peak, and from the peak to a decision.

Operations

Users to QPS

A short-message service with 100 million daily users.

Step 1 of 5

Users

StepWorkingResult
Usersassumed100 million
A day
Average
Peak
So

100 million people a day.

Step 1: Users

def average(users, each):    return users * each / 86_400def peak(users, each):    return 3 * average(users, each)
Answers
What load?
You leave with
Average and peak

In your head: 5 billion, take five zeros off, 50,000. Times three, 150,000. Same decision.

In the real world

The same queries per second doing real jobs. Pick one and step through it.

100,000 people wait for a sale. The average day says 50 QPS.

Step 1 of 4

The average

Way to countQPS
Average × 3150/s
People / interval

4.3 million page views a day: 50 a second, 150 at three times.

Step 1: The average

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Planning for the average

    critical

    The system fails in the evening, the only time that counts.

    Wrong
    servers(avg, 1000)
    Right
    servers(3 * avg, 1000)
  • Reads plus writes

    major

    The sum hides which part is hard.

    Wrong
    total = 11_690        # "shard it"
    Right
    writes, reads = 350, 35_000# one database, a cache
  • Requests as queries

    major

    One request may make twenty.

    Wrong
    db_qps = rps
    Right
    db_qps = rps * queries_each * (1 - hits)
  • Peak from too short a window

    minor

    Two queries 10 microseconds apart look like 200,000 QPS.

    Wrong
    peak = count / elapsed   # elapsed ~ 0
    Right
    peak = max(per_second_buckets)

Interview tips

  • Formulas

    • Average = daily / 86,400.
    • Peak = 3 × average.
    • Servers = ⌈peak / (cap × 0.7)⌉ + 1.
  • Memory trick: QPS

    • Queries: count them.
    • Per: divide by time.
    • Second: 86,400 in a day.
  • Say

    • "I assume 20 each."
    • "Reads 600, writes under 1."
    • "So one database."
  • Ask

    • Does a clock bring them together?
    • Reads for each write?
    • Which region?