Skip to main content

Designing a Scalable API Rate Limiter in nodejs Application

Scenario 1:

Let's say, you are building a Public API Service where user can access the Service using that API. Meanwhile, you need to protect from DDOS Attack of that Public API Service.

Scenario 2:

Consider that we built a Product. we need to provide free trial to the users to access the Product service.

Solution:

On both the scenarios, Rate limiting Algorithm is a way to solve the Problems.

Rate limiting Algorithm :

Firstly, Rate limiting algorithm is a way to limit the access to API's. For example, Let's say that an user request a API in the rate of 100 Requests/Second. it might cause of the problem of server overload.

To avoid this problem, we can limit the access to the API's. Like, 20 Requests/minute for an user.

Different Rate Limit Algorithm:

Let's see type of rate limiting algorithms in software application development:

Token Bucket:

it stores the maximum number of token it can provides for a requests per minute. For Example, For an user, API Rate limiter sets 5 tokens per minute. so, user can send maximum 5 requests to server per minutes. After that, server drops the request.

Mainly,Redis is used to store the information for fast access of the request data.

token bucket

How it works

Let's say User1 sends request to server. server checks whether the request time and previous request time is greater than a minute. if it is less than a minute, it will check the token remaining for the specified user.

If it is Zero, Server drops the request.

Leaky Bucket

It is a Queue which takes request in First in First Out(FIFO) way.

leaky bucket 1

Once, Queue is filled. server drops the upcoming request until the queue has space to take more request.

For Example, Server gets request 1,2,3 and 4. Based on the Queue size. it takes in the request. consider the size of queue as 4. it will take requests 1,2,3 and 4.

After that, server gets request 5. it will drop it.

Fixed window Counter

it increments the request counter of an user for a particular time. if counter crosses a threshold. server drops the request. it uses redis to store the request information.

For example, Server gets the request from an user. if the user request info is present in redis and request time is less than the time of previous request, it will increment the counter.

Once, the threshold is reached. server drops the upcoming request for a specified time.

Cons

Let's say that server gets lots of request at 55th second of a minute. this won't work as expected

Sliding Logs

sliding logs

it stores the logs of each request with a timestamp in redis or in memory. For each request, it will check the count of logs available for an user for a minute.

further, if the count is more than the threshold, server drops the upcoming requests.

On the other hand, there are few disadvantages with this approach. let's say if application receives million request, maintaining log for each request in memory is expensive.

Sliding window counter

This approach is somewhat similar to sliding logs. Only difference here is, Instead of storing all the logs,we store by grouping user request data based on timestamp.

For example, Once server receives a request by an user. we check the memory for the request timestamp. if it is available, we increment the counter of it. if it is not available, we insert it as new record.

In that way, we don't need to store each request as a separate entry , we can group them by timestamp and maintain a counter for it.

Implmenting Sliding window Counter in Node.js App

Complete source code can be found here

Prerequisites

create a directory and initialize package.json using the following command

1npm init --yes

After that, Install Express and redis for the application using the following command

1npm i express redis moment

Redis Client is used to connect with redis server. moment is used for storing the request timestamp.

Firstly, create a file server.js and add the following code.

1const express = require("express")
2const rateLimiter = require("./slidingWindowCounter")
3const app = express()
4
5const router = express.Router()
6
7router.get("/", (req, res) => {
8 res.send("<h1>API response</h1>")
9})
10
11app.use(rateLimiter)
12app.use("/api", router)
13
14app.listen(5000, () => {
15 console.log("server is running on port 5000")
16})

Secondly, create a file slidingWindowCounter.js and add the following code.

1const redis = require("redis")
2const moment = require("moment")
3const redisClient = redis.createClient()
4
5module.exports = (req, res, next) => {
6 redisClient.exists(req.headers.user, (err, reply) => {
7 if (err) {
8 console.log("problem with redis")
9 system.exit(0)
10 }
11
12 if (reply === 1) {
13 redisClient.get(req.headers.user, (err, redisResponse) => {
14 let data = JSON.parse(redisResponse)
15
16 let currentTime = moment().unix()
17 let lessThanMinuteAgo = moment()
18 .subtract(1, "minute")
19 .unix()
20
21 let RequestCountPerMinutes = data.filter(item => {
22 return item.requestTime > lessThanMinuteAgo
23 })
24
25 let thresHold = 0
26
27 RequestCountPerMinutes.forEach(item => {
28 thresHold = thresHold + item.counter
29 })
30
31 if (thresHold >= 5) {
32 return res.json({ error: 1, message: "throttle limit exceeded" })
33 } else {
34 let isFound = false
35 data.forEach(element => {
36 if (element.requestTime) {
37 isFound = true
38 element.counter++
39 }
40 })
41 if (!isFound) {
42 data.push({
43 requestTime: currentTime,
44 counter: 1,
45 })
46 }
47
48 redisClient.set(req.headers.user, JSON.stringify(data))
49
50 next()
51 }
52 })
53 } else {
54 let data = []
55 let requestData = {
56 requestTime: moment().unix(),
57 counter: 1,
58 }
59 data.push(requestData)
60 redisClient.set(req.headers.user, JSON.stringify(data))
61
62 next()
63 }
64 })
65}
  • it checks if the user exists in redis memory, if exists, process further. if not, insert the user details with counter value and request timestamp.
  • If user already exists, it will check if count of request within the last minute exceeds the threshold. if it exceeds, server drops the request.
  • If it does not exceeds, it will increment the counter if any timestamp matches. else, it will insert .

Scalable API Rate Limiter in nodejs , 

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...