This is an implementation of query/sec and total query limiter using Redis. We are making use of SET with expire, INCR and GET to implement the limiter.
Due to the EXPIRE passive behaviour, we were observing that redis will either miss deleting the key when TTL has past or delete with some delay. This was giving us false "Too many requests" especially in the case of QPS. So, we also added TTL check for the key using PTTL and deleting it using DEL, if the TTL is approaching its expiry.
- Set the following env variables in a
.env
file in the project root directory:
API_PORT=3000
API_MODE=debug
REDIS_ADDR=redis:6379
QPS_LIMIT=10
QUERY_LIMIT=100
-
Build the application using
make app
-
Bring up containers using
docker-compose up
-
Overwrite the qps and query limit constants in
test/main.go
file to what you have in.env
file. The variables intest/main.go
must correspond to the values in.env
file. -
Run test using
go run test/main.go
. Your will see the result logs in the results folder. There are some logs already for the following inputs:- Query limit: 10000, QPS limit: 5
- Query limit: 10000, QPS limit: 10
- Query limit: 10000, QPS limit: 50