# Token Bucket Rate Limiter

How a token bucket rate limiter lets bursts of requests through while holding steady traffic to a set average rate\.

By curious\-badger\-a8b8dd1e

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/3QfC2HyzbH78)

## Transcript

### 1. A bucket of tokens

Busy websites and apps need a fair way to say "not so fast"\. This little machine shows one: a token bucket rate limiter\. A rate limiter decides how many requests, which are asks for something, can get through in a given time\.

Drag empty space to turn the machine and look around\.

### 2. Tokens drip in

A token is a ticket that lets one request through\. The clock on the tower drops a fresh token into the bucket at a steady pace, here one every two seconds\. When the bucket is full, extra tokens spill out and are lost\.

This bucket holds 5 tokens: that size is its capacity\.

### 3. Every request pays a token

Each card on the track is a request, like a phone asking a website for a page\. At the gate it must hand over one token from the bucket\. If there is a token, the gate lifts and the request rides on to the server, the computer that answers it\.

You can also tap the round button on the machine\.

### 4. A full bucket allows a burst

Tokens saved up while things are quiet can all be spent at once\. Send eight requests together: the five banked tokens let five through straight away\. The rest find the bucket empty and are turned away into the tray\.

The capacity sets the biggest burst the limiter allows\.

### 5. Empty bucket: please wait

Now a request arrives every second, but a token arrives only every two seconds\. Once the saved tokens are used up, about one request in two gets through\. Turned\-away requests can try again later\.

Websites often answer a turned\-away request with the code 429, "Too Many Requests"\.

### 6. Two simple knobs

A token bucket has just two settings\. The bucket size decides how big a burst can be\. The refill speed decides how many requests get through on average over time\.

Try a faster refill and watch more cards reach the server\.

### 7. Where token buckets work

Token buckets quietly protect web services, game servers and computer networks every day\. They let a quiet user do a quick burst, while stopping anyone from flooding the system\. Fair, simple and fast\.
