Rate limiting with a token bucket

A guard gives each IP address a bucket of tokens and answers with 429 Too Many Requests once it runs dry, before the route even runs.

The bucket holds 5 tokens. Every request below /rate-limit/api takes one, and 1 token per second flows back in, so short bursts pass, but a client sending faster than that gets a Retry-After header.

Each response carries RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset headers, which drive the bucket below. Fire a burst to empty it, then watch it refill.

GET /rate-limit/api/quote

5 / 5 tokens

    The buckets live in the memory of one server process. With several instances, keep them in a shared store like Redis, so all instances count together.