10 / 25 · Service boundaries

Rate limiting

Rate limiting controls how much work an identity or client may request over time. It protects capacity and fairness, but it is not a complete defense against every attack.

Work through a token bucket with 10 tokens and a refill rate of 2 tokens per second. Compare it with a fixed window, then consider shared counters and HTTP 429 responses.

What you will practise

  • Distinguish rate and concurrency
  • refill a token bucket
  • explain shared counters

Before you start: Identity, elapsed time and concurrency.

About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.

Download MP4Captions

Listen without the videoDownload audio

The same pre-recorded voice as the video. Starting audio pauses the video, and starting the video pauses audio.

Chapters and full transcript

Let us understand rate limiting. A library lets each student borrow a limited number of books so one student cannot empty the shelves. A short burst may be acceptable, but repeated borrowing must remain within a fair rate. The rule needs a reliable way to identify the student, not just the colour of their shoes. Rate limiting controls how much work an identity or client may request over time. It protects capacity and fairness, but it is not a complete defense against every attack.

Follow the normal path. Requests are assigned a deliberate identity key, such as a verified account and endpoint. A spoofable header cannot safely define a protected client budget. The token bucket refills over elapsed time up to its capacity. Capacity permits a bounded burst while the refill rate controls sustained use. SeatSpot protects sign-in and booking attempts from excessive traffic. Public browsing, expensive searches and paid customers may need different budgets; one shared limit for everyone can punish unrelated users.

Now change one condition. Checking and consuming a token must behave as one operation for shared state. Separate reads and writes can overspend under concurrency. A common misunderstanding is this: An IP header is always a reliable user identity. Client-controlled forwarding headers can be forged, and many people share one NAT address. Think about this. Ten tokens are spent immediately. After three idle seconds at two tokens/second, how many tokens remain? Pause the video and make your prediction. Six, capped by the bucket capacity of ten. Concurrent updates need an atomic implementation to preserve that rule.

Let us reason with a concrete example. A teaching token bucket has capacity 10 and refills at 2 tokens/second. If initially full, ten immediate requests can consume ten tokens. After 3 idle seconds it has six tokens, capped at ten. This model ignores distributed races until an atomic implementation is chosen. Choose a key and storage behavior explicitly. Protect critical actions with a deliberate fail-closed or degraded policy rather than silently giving unlimited access after a store failure. These figures are teaching examples, not production guarantees.

Turn the idea into a design decision. A request with enough tokens proceeds, but still faces authorization, request validation and downstream concurrency limits. Passing the limiter is not a correctness guarantee. A request without budget receives a clear rejection and retry guidance where appropriate. Clients should wait rather than creating an immediate retry storm. When you try the practice task, look for this result. Only the available budget is admitted; a 429 response can communicate a retry policy.

Check your understanding. If the shared limiter fails, enforce the chosen endpoint policy and alert. Independent local counters do not preserve a strict global quota across replicas. Here is a challenge. Three replicas each allow 100 requests/minute for one user. Is the global user limit 100? Pause before the answer. Not with independent counters: the user may receive up to roughly 300 across replicas, depending on routing and windows. Use an atomic shared budget or state explicitly that the policy is per instance. For an interview, choose a principal, atomic state and store-failure policy. Say what you would measure, not only which tool you would buy.

Start with an everyday picture

A library lets each student borrow a limited number of books so one student cannot empty the shelves. A short burst may be acceptable, but repeated borrowing must remain within a fair rate. The rule needs a reliable way to identify the student, not just the colour of their shoes.

Why SeatSpot needs this

SeatSpot protects sign-in and booking attempts from excessive traffic. Public browsing, expensive searches and paid customers may need different budgets; one shared limit for everyone can punish unrelated users.

Trace one step at a time

Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.

API callsIdentity keyAtomic bucketAllowed work429 response

Step 1 of 6 · Key: identity plus scope

Identify the principal

Requests are assigned a deliberate identity key, such as a verified account and endpoint. A spoofable header cannot safely define a protected client budget.

Read all six mechanism steps
  1. Identify the principal

    Requests are assigned a deliberate identity key, such as a verified account and endpoint. A spoofable header cannot safely define a protected client budget.

  2. Refill the budget

    The token bucket refills over elapsed time up to its capacity. Capacity permits a bounded burst while the refill rate controls sustained use.

  3. Consume atomically

    Checking and consuming a token must behave as one operation for shared state. Separate reads and writes can overspend under concurrency.

  4. Allow affordable work

    A request with enough tokens proceeds, but still faces authorization, request validation and downstream concurrency limits. Passing the limiter is not a correctness guarantee.

  5. Reject excess traffic

    A request without budget receives a clear rejection and retry guidance where appropriate. Clients should wait rather than creating an immediate retry storm.

  6. Plan storage failure

    If the shared limiter fails, enforce the chosen endpoint policy and alert. Independent local counters do not preserve a strict global quota across replicas.

Pause and make a prediction

Ten tokens are spent immediately. After three idle seconds at two tokens/second, how many tokens remain?

Answer aloud or write your reasoning in the lesson notes before revealing the explanation.

Correct a common misunderstanding

Claim to question: An IP header is always a reliable user identity.

Better explanation: Client-controlled forwarding headers can be forged, and many people share one NAT address.

Run a paper token bucket

A paper or whiteboard task. No cloud account or paid infrastructure is required.

  1. Start a bucket with capacity ten and ten tokens.
  2. Spend all ten on a burst, then advance time three seconds.
  3. Refill six and test another burst before enough time passes.

Check what you can explain

This is your self-check, not an automated grade. Try again without the diagram tomorrow.

0 of 3 goals checked. Choose one remaining idea to practise next.

These checkboxes reset when you leave. Use your saved lesson notes and completion marker to record lasting progress.

Teach it back

Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.

Try it before viewing the solution

Three replicas each allow 100 requests/minute for one user. Is the global user limit 100?

Check one key idea

Why can a fixed window admit a large short burst?

Your explanation and interview notes

Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.

Loading your local notes…

Official references and next reading

The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.