Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 03 · System Design · Holding up under load · about 34 min to read

Watch: Rate limiting: the bouncer at the door
▶ VideoPlay it as a journeyTutor comes with a plan

Rate limiting: the token bucket

The Bouncer at the Door
The Bouncer at the Door · Open board

This board shows a rate limiter. It is a check that every request passes before it reaches the server. You do not need earlier lessons to read this one.

Before you start. Three facts. A server finishes a fixed amount of work each second, and one request is one job given to it. A client is the program that sends requests for a user, such as a phone app or a script. For counting, a client and its user are the same thing. When one computer asks another a question and waits for the answer, that wait is a round trip. Counting inside one computer takes microseconds. A round trip takes milliseconds, and across the world it takes most of a second.

Lesson 13 of 115 in System Design

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The problem: everyone arrives in the same second
  • The idea: a bucket of tokens
  • How the token bucket refills
  • The leaky bucket: a queue with a fixed exit speed
  • Why a fixed window lets in double
  • What 429 means, and why “try later” is not enough
  • What a rate limiter cannot stop
  • Fixed window, sliding counter and sliding log, measured
  • Why the counter can lock a client out
  • Where the counter lives, and the price of being exact
  • What the headers say in a real API

Terms it explains: client, round trip, queue, flood, rate limiting, limiter, API gateway, token, bucket, capacity, refill rate, token bucket, burst, leaky bucket, fixed window, sliding window counter, status code, 429, headers, exponential backoff, jitter, IP address, botnet, sliding-window counter, sliding-window log, 200, counter, 199, log, key, atomic, Fail-open, Fail-closed, A, consensus, CAP, CP, AP, slice, overshoot.

Lab

The limit is 5 a minute. How many can one client land in a two-second span?

Stay on Reset on the minute, press Next request until the list passes the rollover, and watch What the bouncer is counting.

Open The bouncer at the door

Review System Design