A partner company starts integrating with your API. On day one, their developer writes a loop to load test data, and suddenly your API answers slowly for every other client. Nobody attacked you: one client simply had no limit. Rate limiting controls how many requests each client may send in a given time. Requests within the limit are served; the rest get the status code 429 Too Many Requests .
This post compares the common rate limiting algorithms, then implements a token bucket per client in Spring Boot with the Bucket4j library.
Why Rate Limit Your API?
Fair use: one client's traffic can't slow down the API for everyone else.
Protect expensive resources: database queries, calls to paid third-party APIs, and CPU-heavy work.
Security: rate limiting is one of the defenses against Unrestricted Resource Consumption , number 4 in the OWASP API Security Top 10 , and it slows down brute-force attacks on login endpoints.
Business plans: a free plan with 10 requests per minute and a paid plan with 1,000 is a…