Skip to content

rate limiter in api-gateway using sliding-window algorithm

Notifications You must be signed in to change notification settings

arsalanaa44/rate_limiter

Repository files navigation

here is Mehran, start coding --

rate limiter

rate limiter in api-gateway using sliding-window algorithm

Introduction

This is the entry of an api-server using rate limiter. It's designed to control the rate of actions that can be performed by a user or a system. The main goal is to prevent any part of the system from being overwhelmed by too many requests.

We use sliding-window algorithm to implement rate limiting. There is also a middleware that imposes a monthly quota on each user.

The code structure is based on project-layout.

Up and Running

first you should have docker installed in your system. you can follow instruction on this link.

after that build and run the project image running this command

docker compose up -d .  

Getting started

first you need to signup:

curl --request POST \
  --url https://localhost:8080/signup \
  --header 'Content-Type: application/json' \
  --data '{
    "month_size_limit": 100, 
    "minute_rate_limit": 4
    }

you will see response like this:

{
	"ID": "d8fff3b4-388d-48f3-82c3-81924c0de5b4",
	"month_size_limit": 100,
	"minute_rate_limit": 4
}

you can do it by run bash script here.

after that you can send your data as follows:

curl --request GET \
  --url https://localhost:8080/hello \
  --header 'Data-ID: id_sampleid' \
  --header 'Data-Size: 26' \
  --header 'User-ID: d8fff3b4-388d-48f3-82c3-81924c0de5b4' # id from signup respose

by passing the middlewares, you will access to api which here is HI.

HI

you can see changes by run redis insight in your browser

https://localhost:8001/

Design

before accessing the API-server we have some middlewares to pass.

Technology

Handler

HTTP handler are defined in handler package. Echo is an awesome HTTP framework that has everything you need.

Configuration

The main part of each application is its configuration. There are many ways for having configuration in the project from configuration file to environment variables. Koanf has all of them in a one beautiful package. The main points here are:

  • Having a defined and typed structure for configuration
  • Don't use global configuration. each module has its configuration defined in config module and it will pass to it in its initiation.
  • Print loaded configuration at startup, so everyone can validate the applied configuration.

Logging

zap is one the best logger for structure logging. zap forces you name loggers with Named method. By using the named logger you can easily find you module logs in your log aggregator.

database

redis, which stands for Remote Dictionary Server, is known for its speed, reliability, and performance, delivering sub-millisecond response times and enabling millions of requests per second for real-time applications. so I decided to use it, reading data types and found the best model to use.

limiter algorithm

I searched for different algorithms of rate limiter. the Sliding Window algorithm works by maintaining a sliding window of fixed duration which is best fit for our project. find this page and use code from this repo.

extra useful github resources and articles:

custom-rate-limiting-in-golang-using-redis-with-docker
implementing-a-sliding-log-rate-limiter-with-redis-go
rate-limiting-go-application
tollbooth

About

rate limiter in api-gateway using sliding-window algorithm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published