Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Token Bucket Algorithm
Jayant
Jayant

Posted on

     

Token Bucket Algorithm

These Algo. typically used as Rate limiting Algorithm.
It is used to control amount of data or number of requests that can be proccessed in a given time period.

Token Bucket Algo

WORKING :

  • Token are added to the Bucket at a fixed rate.
  • The Bucket has a Max. Capacity, it can't hold more tokens than that.
  • When a Request came, the Bucket is checked for number of token available
    • If req. tokens are available then remove that tokens from the bucket and process the request.
    • If no token are available, the request is rejected or pushed to a retry queue.

IMPLEMENTATION :

Token Bucket can be implemented in respect to user or application.
Means either we can use it rate limit the user, that a certain user can only make 10req/sec.
or we can use it to rate limit the application, that a certain application can only make 10req/sec.

classTokenBucket{privaterefillRate:number;// no of token added to the bucket per secondprivatecapacity:number;privateBucket:number;privatelastRefillTime:number;constructor(refillRate:number,capacity:number){this.refillRate=refillRate;this.capacity=capacity;this.Bucket=capacity;this.lastRefillTime=Date.now();}publicrefill(){consttime=Date.now();constdiff=time-this.lastRefillTime;consttokensToAdd=Math.floor(diff*this.refillRate);this.Bucket=min(this.capacity,this.Bucket+tokensToAdd);this.lastRefillTime=time;}publicallowRequest(){// Update the Bucket with tokensrefill();if(this.Bucket>=1){this.Bucket=this.Bucket-1;returntrue;}returnfalse;}}
Enter fullscreen modeExit fullscreen mode

Example : LIMITING REQUESTS PER SECOND FOR USER IN NESTJS

Token Bucket Algo

FLOW

Client──>NestJSAPI(Producer)└──alwayssendtoRabbitMQ(freeorpremiumqueue)Worker(Consumer)└──Pullfromqueue(s)└──CheckRedistokenbucket├──Allowedprocessjob└──Notallowedrequeuewithdelay
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Lazy Programmer. Trying to build things as easy as Possible
  • Location
    Gurgaon, India
  • Education
    Amity University
  • Joined

More fromJayant

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp