To enforce per-user rate limits in a multi-instance Spring Boot app, use a distributed rate-limiting mechanism like Redis. Here are the key strategies you can refer to:
- Distributed Counter with Redis: Track requests per user with Redis keys, expiring after the rate limit window.
- Use Filters to Apply the Rate Limiter: Integrate the rate limiter into Spring's request lifecycle.
- Implement Token Bucket for Smoothing: Optionally, use a token bucket algorithm for even request distribution.
Here is the code snippet for the above steps:
Hence, the above strategies can be used to enforce per-user rate limits in a multi-instance Spring Boot app.