Back to blog

An Interactive Guide To Rate Limiting

5 min read
rate-limiting

Learn about rate limiting algorithms with interactive apps, including Token Bucket, Leaky Bucket, Fixed Window, and Sliding Window methods

An Interactive Guide To Rate Limiting

Introduction

Rate limiting is an essential strategy for every backend application. It prevents a single user or client from overwhelming resources and degrading service quality for everyone else. Here are some of the primary benefits of rate limiting:

  • Prevents resource starvation by ensuring fair API usage across all consumers.

  • Reduces hosting and infrastructure costs by bounding peak loads.

  • Provides baseline protection against Denial of Service (DoS/DDoS) and brute-force attacks.

Below are the four most common rate limiting algorithms, each with a simulation you can poke at. In every one, requests travel from a client through the limiter to a server: green dots get through, red ones are rejected. The strip underneath each simulation records the last ten seconds: what came in, and what went on to the server.

Token Bucket

How It Works

  • A bucket holds a fixed maximum number of tokens.

  • New tokens are added to the bucket at a constant refill rate.

  • When a request arrives:

    • If a token is available, one token is consumed and the request is processed.

    • If no tokens are available, the request is immediately rejected (HTTP 429) or queued.

  • Key characteristic: Allows for short, configurable bursts of traffic as long as enough tokens have accumulated.

Try it: let the bucket fill up, then hit Burst of 8. All eight get through at once, because ten tokens were saved up. Hit it again straight away and most are rejected until tokens drip back in.

Try it · Token bucket
Client
Server
Tokens10 / 10
+2/s
In
To server
−10s−5snow
Bucket capacity10 tokens
Refill rate2/s
Background trafficoff
Accepted
0
Dropped
0
Passed
–
Each request spends one token; tokens drip back in at the refill rate. A full bucket lets a burst straight through.

Leaky Bucket

How It Works

  • Imagine a bucket with a small hole at the bottom that leaks water at a steady, fixed rate.

  • Incoming requests are added to the bucket like drops of water.

  • Requests are processed (or "leaked") at a constant, smooth rate.

  • If the bucket fills to maximum capacity when a new request arrives, the overflow is dropped.

  • Key characteristic: Smooths out traffic bursts and outputs a steady stream of requests to downstream services.

Try it: send a Burst of 8. Nothing is rejected, but look at the timeline: the requests come in together and go to the server evenly spaced. The cost is waiting, shown under Average wait. Send a second burst before the first drains and the overflow is dropped.

Try it · Leaky bucket
Client
Server
Queue0 / 8
2/s out
In
To server
−10s−5snow
Bucket size8 requests
Leak rate2/s
Background trafficoff
Accepted
0
Dropped
0
Average wait
–
time spent queued
Requests wait in the bucket and leave at a fixed rate, however they arrive. Compare the evenly spaced “to server” row with the bursty “in” row.

Fixed Window Counter

How It Works

  • Time is divided into fixed-size chronological windows (e.g., 1 minute or 1 hour).

  • A counter tracks the total number of requests made by a client within the current window.

  • If the counter exceeds the allowed threshold, subsequent requests are blocked until the next window begins.

  • Key characteristic: Extremely simple and memory-efficient, but susceptible to traffic bursts (up to 2x the limit) at the boundary between adjacent windows.

Try it: hit Boundary burst. It sends a full window's worth of requests just before a window ends and another just after the next begins. Every window stays within its limit, yet the server gets twice the limit within a fraction of a second. The red band on the timeline marks the spot.

Try it · Fixed window
Client
Server
This window0 / 5
resets in 3.0s
In
To server
0/5
0/5
0/5
0/5
−10s−5snow
Limit per window5 requests
Window length3s
Background trafficoff
Accepted
0
Dropped
0
Passed
–
Each shaded column is one window, labelled with how many requests it let through. A red band marks a stretch one window long that let through more than the limit.

Sliding Window Counter

How It Works

  • Maintains a rolling time window by tracking request timestamps or combining previous and current window counts.

  • When a request arrives, the system calculates how many requests occurred in the sliding interval preceding the current moment.

  • If the count is within the limit, the request is permitted; otherwise, it is throttled.

  • Key characteristic: Prevents the boundary burst issue of fixed windows while maintaining high accuracy and smooth rate limiting.

There are two common variants, and the demo has both:

  • Log: store the timestamp of every accepted request and count those in the last window. Exact, but memory grows with the limit.

  • Counter: keep just two counts, for the current and previous fixed windows, and weigh the previous one by how much of it still overlaps the sliding window. If the previous window had 4 requests, the current one has 2, and we're 60% of the way through the current window, the estimate is 4 × 0.4 + 2 = 3.6. Two integers per client, and accurate enough in practice.

Try it: send requests until you're rejected, then watch the amber window slide on. Each old request drops out of it one at a time, unlike a fixed window, which forgets everything at once.

Try it · Sliding window
Client
Server
Last 3s0 / 5
exact count
In
To server
0/5
−10s−5snow
Limit per window5 requests
Window length3s
Background trafficoff
Accepted
0
Dropped
0
Passed
–
The amber band is the window: always the last few seconds, ending now. Log mode counts requests in it exactly; counter mode estimates from two fixed windows.

Side by Side

Here are all four limiters with the same budget, six requests every two seconds, fed exactly the same traffic. Switch patterns and compare what reaches the server:

Try it · Four limiters, same traffic
In
Token
Leaky
Fixed
Sliding
−10s−5snow
LimiterLet throughBusiest 2sAvg wait
Token bucket–0–
Leaky bucket–0–
Fixed window–0–
Sliding window–0–
Each limiter allows 6 requests per 2 seconds. Green ticks reached the server, when they reached it; red ticks were rejected.
  • Steady traffic under the limit passes through all four the same way. Algorithms only differ under pressure.

  • Bursts: the token bucket passes the start of each burst instantly; the leaky bucket accepts the same amount but delivers it at an even pace, adding wait time; the window limiters cut each burst off at six.

  • Window boundaries: the fixed window lets twelve through in well under a second. That's the ▲ in the Busiest 2s column. The sliding window never exceeds six.

Which one to pick depends on what you're protecting. Use a token bucket when short bursts are fine (most public APIs). Use a leaky bucket when the downstream service needs a steady pace. Use a sliding window counter when you need a hard ceiling at low cost, and a fixed window when simplicity matters more than the edge cases.