Spring Boot: Basic Rate Limiting using Bucket4j
Rate Limiting and Uses
Rate limiting is a strategy for restricting access to an API. It limits the number of calls that can be made to an endpoint within a certain time thus defending the API against overuse, malicious use, etc.
Bucket4j
It is a library based on the Token-bucket algorithm. As per the Token-bucket algorithm, there is a bucket with limited tokens as its capacity. The user needs to get a token to access an endpoint. If the bucket has no token, then reject the user request. We will also replenish the token at a fixed rate after the consumption.
Spring Boot Application
Create the Spring Boot starter project.
Add the Maven Dependency.
<dependency>
<groupId>com.bucket4j</groupId>
<artifactId>bucket4j-core</artifactId>
<version>8.1.0</version>
</dependency>
A sample pom.xml of the application.
<?xml version="1.0" encoding="UTF-8"?>
<project…