Using Redis to implement the rate limiter Use Redis in the implementation of the rate limiter to save the database of calls that could overload. Redis allows precisely to limit the number of requests. Especially in our case, we use SQLite which has a system of "Concurrent Access" which could lengthen the time of the requests if this number is too important simultaneously.
Redis has specifics functions to implement a rate limiter like expire which use to specify a lifetime of a key.
You can access to the documentation of the API by running the node instance and access to localhost:3001/documentation
You can access to the documentation and test the API from this link:
leaderboard.alexdana.me/documentationleaderboard.alexdana.me/leaderboard/
You can deploy this project using pm2.
To setup a first deployement
pm2 deploy production setupand to deploy the project:
pm2 deploy productionTo generate the image:
docker build -t leaderboard .Prepare the environement:
docker pull redis # Download an instance of redis
docker network create redis-node # create a network to let the redis instance and our instance (node) communicateTo start the project with the image :
docker run -d --net redis-node --name instance_redis redis
docker run -d --net redis-node -p 3001:3001 --name instance_node leaderboardTo reset all instance:
docker kill $(docker ps -q)
docker rm $(docker ps -a -q)We will need differents tools : redis, node, npm (or yarn) and pm2.
First you need to install redis.
If you have wget, execute these commands:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make test
sudo make installor if you don't have it, just download the tar.gz from this link: http://download.redis.io/redis-stable.tar.gz
and execute these commands:
tar xvzf redis-stable.tar.gz
cd redis-stable
make
sudo make installNow you have : redis-cli, redis-benchmark, redis-check-aof, redis-check-rdb, redis-sentinel and redis-server.
Next, you need to install NodeJS if you don't have it.
brew install nodeIf it already installed, update it using:
brew upgrade nodeFor ubuntu:
sudo apt update
sudo apt install nodejs npmYou only need to install it using npm or yarn
npm install pm2 -gYou need to prepare the project:
npm install
node_modules/.bin/sequelize db:migrate
node_modules/.bin/sequelize db:seed:all # If you want to add 100.000 entriesYou need to run an instance of redis:
redis-serverand launch a node instance:
NODE_ENV=production npm startIf the NODE_ENV is missing the project will be run in development mode.