PLAIN/TEXT — ISSUE 145/456 COMPUTER SCIENCE · FOUNDATION / TOKEN-BUCKET LIMITS · ~4 MIN READING DATE: 2027-02-23 ARCHIVE: https://plaintext.click/newsletter/ SUBJECT: How an app allows a burst without allowing a flood How an app allows a burst without allowing a flood ================================================== / START HERE An app may accept several quick button presses but reject a long stream of automated requests. Rate limiting is a rule that controls how much work a caller may submit over time. A token bucket is a counter of saved permissions to send work; it has a maximum size and refills at a chosen rate. / WHY NOW A rule saying only “five requests per second” leaves an important question unanswered: must those requests be evenly spaced? A bucket makes the allowed burst explicit rather than hiding it inside a vague average. / THE IDEA The system keeps a counter of available tokens, where a token is permission for a unit of work. Tokens are added at a chosen refill rate, up to a fixed capacity. Each accepted request removes one token in the simplest version. When the counter is empty, a new request is rejected or delayed according to policy. Unused permission can accumulate, but only up to the capacity. This lets a quiet client save enough permission for a brief burst without earning unlimited future traffic. The refill rate controls long-run replenishment; capacity controls how much accumulated permission can be spent immediately. / THE FORMAL IDEA tokens_before = min(B, tokens_after_previous + rΔt) - B is bucket capacity in tokens; r is refill rate in tokens per second; Δt is elapsed seconds. - min selects the smaller value. An accepted one-token request subtracts one after this update. These are permissions, not security login tokens. / RUN THE TINY EXAMPLE Change one thing. Predict what happens. Toy bucket: B = 10, r = 2 tokens/s; start full. Eight simultaneous one-token requests leave 10 − 8 = 2 tokens. Wait three seconds with no requests: min(10, 2 + 2 × 3) = 8 tokens. Change only the wait to one second: min(10, 2 + 2 × 1) = 4 tokens, so only four immediate one-token requests can now pass. The eight-request opening burst exceeds the refill rate over that instant, yet it is allowed because permission had accumulated. Continuing at eight requests every second would drain the bucket faster than two tokens per second can replenish it. / SO WHAT? This separates two design questions: how much sudden legitimate work can the service absorb, and how much continuing work should one caller receive? Raising capacity helps the first without changing the refill rate. Raising the refill rate changes sustained allowance. Requests with different costs can consume different token amounts, preventing an expensive operation from being treated as equal to a trivial check. / ONE CAVEAT Distributed enforcement, clock behaviour and refill granularity affect exact limits. Some products implement best-effort targets rather than strict ceilings; rate limiting also does not decide whether a caller is authorised to perform an operation. / KEEP THIS Bucket capacity permits accumulated bursts, while refill rate limits how quickly permission returns after those bursts are spent. / SOURCE TRAIL [1] AWS — API Gateway token-bucket throttling https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html [2] IETF — Token-bucket metering https://www.rfc-editor.org/rfc/rfc2697.html NEXT: How a microphone turns moving air into a voltage --- HARD IDEAS. PLAIN TEXT. You are receiving this because you asked for one clear computer-science or physics idea each day. Unsubscribe: https://plaintext.click/unsubscribe View in browser: https://plaintext.click/newsletter/2027/02/2027-02-23-rate-limit-token-bucket.html From: Plain/Text / Brickston Consulting · 30 N Gould St Ste N, Sheridan, WY 82801 US · knowledge@plaintext.click