0
0
HLDsystem_design~3 mins

Why Rate limiting algorithms (token bucket, leaky bucket) in HLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your website could stop overloads automatically without you lifting a finger?

The Scenario

Imagine you run a popular website where thousands of users send requests every second. Without any control, some users might flood your servers with too many requests at once, causing slowdowns or crashes. You try to manually track each user's request count and block them when they send too many, but it quickly becomes overwhelming.

The Problem

Manually counting requests for each user is slow and error-prone. It requires constant checking and updating, which wastes resources. It's hard to keep track accurately when many users act at the same time. This leads to mistakes, unfair blocking, or letting too many requests through, causing system overload.

The Solution

Rate limiting algorithms like token bucket and leaky bucket automate this control smoothly. They allow requests up to a limit and then slow down or block excess requests in a fair and efficient way. These algorithms handle bursts and steady flows gracefully, protecting your system without manual effort.

Before vs After
Before
if user_requests > limit:
    block_request()
else:
    allow_request()
After
if token_bucket.has_token():
    token_bucket.consume_token()
    allow_request()
else:
    block_request()
What It Enables

It enables systems to handle high traffic reliably by controlling request rates automatically and fairly, preventing overload and improving user experience.

Real Life Example

Think of a water faucet filling a bucket (token bucket). Water flows in steadily, but you can only take out water if there's enough in the bucket. This controls how fast you can fill your glass, just like rate limiting controls request flow to a server.

Key Takeaways

Manual request tracking is slow and unreliable under heavy load.

Token bucket and leaky bucket algorithms automate fair and efficient rate control.

These algorithms protect systems from overload and improve stability.