Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ByteByteGo Daily] Rate Limiting Fundamentals #145

Open
reboottime opened this issue Jul 5, 2023 · 5 comments
Open

[ByteByteGo Daily] Rate Limiting Fundamentals #145

reboottime opened this issue Jul 5, 2023 · 5 comments

Comments

@reboottime
Copy link
Owner

reboottime commented Jul 5, 2023

Overview

This is a note extracted from Rate Limiting Fundamentals, By Alex Xu.

Reference:

  1. Rate limiting by Alex Xu
  2. Wikipedia, priority queue and its implementation
@reboottime
Copy link
Owner Author

reboottime commented Jul 5, 2023

Rate Limiting Definition and Examples

Rate limiting controls the rate at which users or services can access a resources, like an API, a service, or a network. It plays a critical role in protecting system resources and ensuring fair use among all users.

When the rate of requests exceeds the threshold defined by the rate limiter, the requests are throttled or blocked. Some real world examples can be as following:

(Please notice there is a difference on throttling and blocking)

example of rate limiting

@reboottime
Copy link
Owner Author

reboottime commented Jul 5, 2023

Benefits of Rate Limiting

Rate limiting is an integral part of modern large-scale applications.

  • Preventing resource starvation: it helps prevent resource starvation caused by Denial of Service (DoS) attacks ( by rejecting the excess calls).
  • Reduce cost
    • Reduce outbound cost of third party paid APIs
    • preventing overusing of a resource
  • prevent servers from being overloaded: While rate limiting is vital in preventing DoS attacks, it also plays a pivotal role in general load balancing(how load balancing is done under the hood like AWS?) and service quality maintenance

@reboottime
Copy link
Owner Author

reboottime commented Jul 5, 2023

Applications of Rate Limiting

example

The applications of rate limiting can be applied at

  • application level: limit how many tickets can be sold on a per hour to protect fairness among all users
  • user account level: how many tickets an user can purchase for a concert to protect fairness among all users
  • API level, for example, open AI API rate limiting

@reboottime
Copy link
Owner Author

reboottime commented Jul 5, 2023

Core Concepts of Rate Limiting

Most rate limiting implementations share three core concepts. They are the

  • the limit: defines the ceiling for allowable requests or actions within a designated time span.
  • the window : is the time period where the limit comes into play.
  • the identifier: The identifier is a unique attribute that differentiates between individual callers. A user ID or IP address is a common example

Rate limit core concepts


After surpassing the rate limit, the action (limiting response) can be three catetgories

  • blocking: Blocking takes place when requests exceeding the limit are denied access to the resource. It is commonly expressed as an error message such as HTTP status code 429 (Too Many Requests).
  • throttling: by comparison, involves slowing down or delaying the requests that go beyond the limit. An example would be a video streaming service reducing the quality of the stream for users who have gone over their data cap.
  • shaping: on the other hand, allows requests that surpass the limit. But those requests are assigned lower priority.

three limiting responses

@reboottime
Copy link
Owner Author

reboottime commented Jul 5, 2023

Common Rate Limiting Algorithms

This article introduced algorithms

  • Fixed window counter
  • sliding window log
  • sliding window counter
  • token bucket
  • leaky bucket

The algorithms explaination content are unavailable for unpaid user ):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant