/ratelimiter

Spring Boot Rate Limiter with Redis (Spring Boot, Java 21, JUnit, Integration Test, Github Actions, Test Container, Docker, Redis)

Primary LanguageJava

RATE LIMITER WITH REDIS

Main Information

📖 Information

  • This application is a Spring Boot application demonstrating rate limiting with Redis for user management operations.
  • Explanation:
    • UserController: The API endpoint for managing user information. It includes methods to handle creating, retrieving, updating, and deleting user data. This controller interacts with the UserService to perform these operations.
    • UserService: Contains the business logic for user management. It performs CRUD operations on the User entity and interacts with the RateLimiterService to apply rate limiting rules.
    • RateLimiterService: Utilizes Redis to track and enforce rate limiting rules. It ensures that the number of requests made by a user does not exceed the specified limit within a given time window.
    • UserRepository: Extends Spring Data JPA's JpaRepository, providing methods for performing database operations on user entities. This layer abstracts the data access operations, making it easier to manage user data.
    • Redis Configuration: Redis is configured to support rate limiting functionality. The configuration includes setting up a RedisTemplate for efficient data access and operations. This template handles the interactions with Redis, such as incrementing request counts and setting expiration times for keys, ensuring accurate rate limiting.
    • Validation: The application uses Hibernate Validator for validating user input. This ensures that the data passed to the API endpoints is in the correct format and adheres to the defined constraints.
    • Exception Handling: Custom exception handling mechanisms are implemented to manage various error scenarios, such as user not found, email already exists, and rate limit exceeded.

Explore Rest APIs

Method Url Description Request Body Header Valid Path Variable Request Param No Path Variable
POST /api/v1/users/save Create a new user CreateUserRequest
GET /api/v1/users/{id} Retrieve a user by ID {id} - Valid UUID
PUT /api/v1/users/{id} Update an existing user UpdateUserRequest {id} - Valid UUID
DELETE /api/v1/users/{id} Delete a user by ID {id} - Valid UUID
GET /api/v1/users Retrieve a paginated list of users UserPagingRequest

Technologies

  • Java 21
  • Spring Boot 3.0
  • Restful API
  • Lombok
  • Maven
  • Junit5
  • Mockito
  • Integration Tests
  • Docker
  • Docker Compose
  • CI/CD (Github Actions)
  • Postman
  • Spring Bean Validation

Postman

Import postman collection under postman_collection folder

Prerequisites

Define Variable in .env file

DATABASE_USERNAME={DATABASE_USERNAME}
DATABASE_PASSWORD={DATABASE_PASSWORD}

  • Maven or Docker

Docker Run

The application can be built and run by the Docker engine. The Dockerfile has multistage build, so you do not need to build and run separately.

Please follow directions shown below in order to build and run the application with Docker Compose file;

$ cd ratelimiter
$ docker-compose up -d

If you change anything in the project and run it on Docker, you can also use this command shown below

$ cd ratelimiter
$ docker-compose up --build

Maven Run

To build and run the application with Maven, please follow the directions shown below;

$ cd ratelimiter
$ docker run --name redis -p 6379:6379 -d redis
$ mvn clean install
$ mvn spring-boot:run

Docker Image Location

https://hub.docker.com/repository/docker/noyandocker/ratelimiter/general

Screenshots

Click here to show the screenshots of project

Figure 1

Figure 2

Figure 3

Figure 4

Figure 5

Figure 6

Figure 7

Figure 8

Contributors