Skip to content

hardeep0598/rate-limiter-design

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rate Limiting Algorithms Repository

This repository contains Go language implementations of various rate limiting algorithms, designed to control the rate of incoming requests or actions in different scenarios.

Algorithms Included

1. Token Bucket Algorithm

In the Token Bucket algorithm, we process a token from the bucket for every request. New tokens are added to the bucket with rate r. The bucket can hold a maximum of b tokens. If a request comes and the bucket is full it is discarded.

The token bucket algorithm takes two parameters:

  1. Bucket size: the maximum number of tokens allowed in the bucket
  2. Refill rate: number of tokens put into the bucket every second

2. Leaky Bucket Algorithm

Leaky Bucket is a simple and intuitive way to implement rate limiting using a queue. It is a simple first-in, first-out queue (FIFO). Incoming requests are appended to the queue and if there is no room for new requests they are discarded (leaked).

The leaking bucket algorithm takes the following two parameters:

  1. Bucket size: it is equal to the queue size. The queue holds the requests to be processed at a fixed rate.
  2. Outflow rate: it defines how many requests can be processed at a fixed rate, usually in seconds.

3. Sliding Window Algorithm

The algorithm keeps track of request timestamps. Timestamp data is usually kept in cache, such as sorted sets of Redis. When a new request comes in, remove all the outdated timestamps. Outdated timestamps are defined as those older than the start of the current time window. Add timestamp of the new request to the log. If the log size is the same or lower than the allowed count, a request is accepted. Otherwise, it is rejected.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages