Skip to content

Design an API Rate Limiter, stage 4 of 10: break it

The limiter that leaks

The buckets are in Redis, as decided. The logic is a direct translation of the token bucket. Find the lines that let a 60/minute key make 4,000 requests, and the line that made the incident worse.

System so far· 5 parts
1234CLIENTAPI clientsEDGELoad balancerSERVICEAPI instancesCACHERedisDATABASEPostgres

Select a component to see what it is responsible for and which state it owns.

  1. 1API clients → Load balancer: Requests with API key
  2. 2Load balancer → API instances: Round-robin across instances
  3. 3API instances → Redis: Atomic take-tokens script
  4. 4API instances → Postgres: Admitted requests; plan lookups (cached)

What you need to know

0 of 2 checks done
  1. A read-modify-write reads a value, computes a new one, and writes it back. If it takes two round trips (a GET, then a SET), other clients can read the same old value in between.

    Each of them computes its own update from the same starting point, and the last write wins. The other updates are lost.

  2. Check

    A bucket holds 1 token. Three instances each GET it at the same moment, see 1 token, allow their request and SET the bucket to 0. How many requests were admitted, and how many tokens were spent?