How would you design rate limiting for a public API using a token bucket in Redis?
PICTURE THIS: A SENTENCE BECOMES TOKENS
The model does not read letters like humans. It reads these pieces, then predicts the next one.
Simple meaning
Each client key has a bucket of tokens that refill at a constant rate
WHY — Rate limiting instead of guessing?
Why interviewers care about Rate limiting:
question about Rate limiting.
trade-offs, and what you would actually do on a Backend project - not buzzwords.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens with tokens?
Before the model can read a sentence, it goes through these steps:
- 1Each client key has
a bucket of tokens that refill at a constant rate
- 2a request consumes a
token or gets 429.
- 3Redis holds the counter
with a Lua script or INCR plus TTL so many app nodes share the limit.
- 4Context mix
Token bucket allows short bursts
- 5a fixed window is
simpler but clumpier at window edges.
- 6Decode
IDs turn back into readable text.
EXAMPLE — See it in action
Let's see how a real sentence is tokenized (tokens may vary by model):
Note: Actual tokens and IDs depend on the tokenizer (e.g., GPT, Llama, etc.).
Key takeaway
Each client key has a bucket of tokens that refill at a constant rate a request consumes a token or gets 429.