Skip to content

rate limit library for distributed applications 🔥

License

Notifications You must be signed in to change notification settings

sonirico/pacemaker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PaceMaker

Rate limit library

Implemented rate limits:

Fixed window rate limit

Fixed window limits—such as 3,000 requests per hour or 10 requests per day—are easy to state, but they are subject to have spikes at the edges of the window, as available quota resets. Consider, for example, a limit of 3,000 requests per hour, which still allows for a spike of all 3,000 requests to be made in the first minute of the hour, which might overwhelm the service.

Starts counting time windows when the first request arrives.

Example

Fixed truncated window rate limit

Same as Fixed Window rate limit but truncates the rate limit window to the rate interval configured in order to adjust to real time intervals passing. E.g:

  1. Rate limit interval is configured for new windows every 10 seconds
  2. First request arrives at 2022-02-05 10:23:23
  3. Current rate limit window: from 2022-02-05 10:23:20 to 2022-02-05 10:23:30

Example

Fixed window with token bucket variant (token refill at window's rate)

Works as any other fixed-window rate limiter. However, the meaning of 'capacity' of the inner fixed-window rate limiter changes from the total amount of requests that can be made to the total amount of tokens (points) that can be spent in that same window. This variant is particularly useful, for instance, when working on the crypto game field as many crypto exchanges employ this strategy. Take for example, binance.

Example


You can refer to google architecture docs to read more about rate limits.

Storages

  • Memory. Useful for non-distributed applications and testing purposes. Do not use on production unless you deliberately don't care about keeping rate limit state.
  • Redis. github.com/go-redis/redis is employed as Redis client

TODO:

  • Token bucket rate limit
  • Leaky bucket rate limit
  • Composite rate limit
  • Service rate limit