Skip to content

How a token bucket protects an API from your own burst

Two clients send the same traffic to an API that allows 20 requests per second and answers 429 above that. The traffic is quiet, then 100 requests arrive within 0.4 s at 6 s. Nothing more arrives until 9 s, then 12 per second, then quiet again.

Both clients retry. Only the right one has a rate limit. Press Play: the page slows down at key moments and says what happened.

t = 0.0 s

RequestRetryAPI answeredAPI answered 429Failed by the rate limit (RateLimitedError, nothing sent)

No rate limit

retry: { maxRetries: 3 }
0attempts sent to the API
0answered 429
0of them retries
0requests succeeded

With a rate limit

rateLimit: { tokensPerSec: 15, burst: 5, maxWaitMs: 5000 }
0waiting for a token
0failed: RateLimitedError
0answered 429
0requests succeeded

Attempts reaching the API (per second)

The dashed line is the API limit. Above it, the API answers 429. The bucket flattens the burst into a steady flow below the line.

How long each request waited for a token

Each point is one attempt in the right lane. The wait grows by 1/tokensPerSec for every request ahead in line.

A request whose wait would pass maxWaitMs fails at once. It is drawn at the wait it would have needed.

What the token bucket gives you

  1. A ceiling on your own rate. The bucket holds up to burst tokens and refills at tokensPerSec. Every attempt takes one token before it goes out. In any span of T seconds, at most burst + tokensPerSec × T attempts start.
  2. A burst waits in line. When the bucket is empty, an attempt waits until its own token exists. The line is first in, first out. With this page's 5 s limit, the first 85 go out (the 85th after about 5 s) and the last 15 fail. With no limit, the 100th waits about 6 s and still goes out.
  3. A limit on the waiting. With maxWaitMs, an attempt that would wait longer fails at once with RateLimitedError. Nothing is sent and the caller knows right away.

A bulkhead limits how many attempts run at the same time. A rate limit limits how many start per second.

The two are not the same. An API that answers in 50 ms can see 100 attempts per second from only 5 running at once, so a bulkhead alone would not stop it.

What it costs

  • Waiting is latency. At 15 tokens per second, a request at position 85 in the line waits about 5 s.
  • You choose the rate. The bucket does not read the API's real limit. A 429 or a Retry-After header does not change it, so a rate set too high still gets 429s.
  • Burst and rate add up. This API counts each calendar second, and a full bucket can send burst + tokensPerSec in one of them. Set the burst to 40 and the right lane gets 429s too.
  • Retries take tokens, the same as first attempts. A retry that hits maxWaitMs fails the whole request, and RateLimitedError is never retried.
  • The bucket lives in one process. Ten servers, each with 15 per second, can send 150 per second together.

Things to try: set maxWaitMs to "no limit". Every burst request now succeeds, and the last one waits about 6 s. Set it to 500 ms and most of the burst fails at once.

In Vereda

HttpClient.create({ timeout: { attemptMs: 5_000 }, rateLimit: { tokensPerSec: 15, burst: 5, // default: max(1, tokensPerSec) maxWaitMs: 5_000, // omit to wait as long as needed }, partitions: { "api.partner.com": { rateLimit: { tokensPerSec: 10, burst: 20 }, }, }, })

This page runs the same bucket as TokenBucket in src/queue/rate-limiter.ts. An attempt reserves its token when it arrives, and the balance can go below zero.

That is why waiting is first in, first out without a queue object. A request over maxWaitMs reserves nothing.

Each attempt calls takeToken in src/queue/bulkhead.ts: the first one in src/core/client.ts, retries in src/queue/retry.ts.

The token comes before any bulkhead slot or global permit, so a waiting attempt holds no concurrency. If you call cancel() or timeout.totalMs passes while it waits, the token goes back to the bucket.

Simplified model: the API counts attempts per calendar second, sends no Retry-After and answers each in about 80 ms. Retries wait 0.3 to 0.5 s; in Vereda, your backoff setting decides that delay.

A test, test/demos/rate-limiter.parity.test.ts, runs this page's model against the real HttpClient, so the page has to change when the library does.