GOAT EngineerPreparing your learning space…

System Design · Practice problem · Medium

Rate Limit Each Client

Give each client its own token bucket, kept in one store that every server checks, so the limit holds across all your servers and one client cannot spend another's allowance. Then decide what happens when that store cannot answer.

Medium means two needs that pull against each other. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.

What is the situation?

You run a public API that online shops call to ask where a parcel is. Each customer has an API key, and every plan allows 100 calls a second. A customer who has been under that may also save up and send a batch of up to 80 calls at once. Every call needs an answer from Tracking, a service another team runs. Tracking takes about 10 ms and accepts 1,000 calls a second from you, and past that it refuses calls from anyone. Today Customer A's script has a bug and sends 1,000 calls a second on one key. Customer B sends a steady 95 a second on its own key, inside its plan. Thousands of small shops send 500 a second between them. A refused call still takes a server's time, because the server has to read a call before it can say no. Design the path from the client to Tracking, so that a client over its limit is held to that limit and nobody else notices. Any customer could be the next one to go wrong.

What must your design do?

  • A client over its limit gets between 85 and 120 calls a second through, however loud it is. If your design needs something to count and that something fails, the upper figure may slip while it is down.
  • A customer inside its plan, batches included, has at least 99% of its calls served, or at least 97% while one of your servers is down.
  • At least 99% of small shops' calls are served, within 100 ms at the 99th percentile.
  • Every call is answered with what Tracking said. Tracking alone takes about 10 ms, so we check that at least 100 small shop calls a second reach it, and that the median small shop call takes at least 12 ms.
  • The system costs $365 a month or less.

What does Run test?

Run works without an account. It checks your design against 2 tests you can read before you start:

  • One script goes wrong. Customer A's script sent 1,000 calls a second, Customer B a steady 95 a second, and the small shops 500 a second between them, for 30 seconds.
  • A customer sends a batch. The same traffic, and at second 10 Customer B sent a batch of 80 calls at once.

What does Submit add?

Submit needs an account. It runs 3 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.

Which parts can you use?

Already on your canvas: Tracking.

  • Load Balancer: about 50,003 a second, $18 a month each
  • API Gateway: about 2,000 a second, $35 a month each
  • App Server: about 2,000 a second, $45 a month each
  • Counter Store: about 100,003 a second, $30 a month each
  • Database: about 5,000 a second, $120 a month each
  • Cache: about 100,003 a second, $30 a month each

Every connection takes 1 ms, and the budget is $365 a month.

What does this problem leave out?

  • A refused client that slows down or retries. Every script here keeps sending at the same pace, whatever it hears.
  • Which counting algorithm you use. Every limit here is a token bucket; fixed and sliding windows are not offered.
  • The moment between reading a count and changing it. A shared count here changes in one step, so two servers can never spend one token.
  • How a client proves who it is. Each call arrives with a key we trust, and limits by address are not offered.
  • A standby copy of the count, or a separate count per region.
  • Plans that differ between customers. Every customer here has the same one.
  • A fleet that changes size during a run.

Where does this problem fit?

Its category is Design the building block: Build the primitive itself. It practises concurrency control, rate limiting & protection and reliability & ops.

For the whole system, read the interview breakdown Design a Distributed Rate Limiter.

See every practice problem.

By GOAT Engineer. Explore System Design · Privacy Policy · Contact.