/basic-rate-limiting-demo-java

This project show how to implement a basic rate limiting using Redis in Java

Primary LanguageJavaScriptMIT LicenseMIT

Basic Redis Rate-limiting Demo Spring (Java)

This application demonstrates rate limiting using Redis and Spring.

Try it out

Deploy to Heorku

Run on Google Cloud (See notes: How to run on Google Cloud)

How to run on Google Cloud

  1. Open link google cloud console.

1 step

  1. Click "Edit and deploy new revision" button.

2 step

  1. Add environment.

3 step

  1. Select vpc-connector and deploy application.

4  step

Problem with unsupported flags when deploying google cloud run button

How it works?

1. How the data is stored:

  1. New responses are added key-ip:
     SETNX your_ip:PING limit_amount
     Example: SETNX 127.0.0.1:PING 10 
    more information

  2. Set a timeout on key:
    EXPIRE your_ip:PING timeout
    Example: EXPIRE 127.0.0.1:PING 1000 
    more information

2. How the data is accessed:

  1. Next responses are get bucket:
    GET your_ip:PING
    Example: GET 127.0.0.1:PING   
    
    more information

  2. Next responses are changed bucket:
    DECRBY your_ip:PING amount
    Example: DECRBY 127.0.0.1:PING 1
    more information

How to run it locally?

Open the files server/.env.example to see the available environment variables. You may set these variables when you start the application.

- REDIS_URL: Redis server url
- REDIS_HOST: Redis server host
- REDIS_PORT: Redis server port
- REDIS_PASSWORD: Redis server password

Run backend

  1. Install gradle (Use Gradle 6.3 or later) (on mac: https://gradle.org/install/)

  2. Install JDK (use 8 or later version) (on mac: https://docs.oracle.com/javase/10/install/installation-jdk-and-jre-macos.htm)

  3. Set any relevant environment variables (if not connecting to Redis on localhost:6379). For example:

$ REDIS_PORT=6379
  1. From the root directory of the project, run the following commands:
cd server
./gradlew build
./gradlew run
  1. Point your browser to localhost:5000.