What if a simple control could stop your system from crashing under heavy user traffic?
Why Design a rate limiter in HLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a popular website where thousands of users try to access your services at the same time. Without any control, some users might flood your system with too many requests, causing slowdowns or crashes for everyone else.
Manually tracking each user's request count and timing is slow and error-prone. It's like trying to count raindrops during a storm by hand--easy to lose track and impossible to keep up. This leads to system overloads, unfair usage, and poor user experience.
A rate limiter automatically controls how many requests each user can make in a given time. It acts like a traffic light, allowing requests through at a safe pace and blocking excess ones. This keeps your system stable and fair for all users.
if user_requests > limit: block_request() else: process_request()
rate_limiter.allow_request(user_id) # returns True or FalseIt enables your system to handle high traffic smoothly while preventing abuse and ensuring fair access for everyone.
Think of an online ticket booking site that limits each user to buying only 5 tickets per minute to prevent scalpers from hoarding all tickets.
Manual request tracking is slow and unreliable under heavy load.
Rate limiters automatically control request flow to protect system health.
This ensures fair use and a better experience for all users.
Practice
Solution
Step 1: Understand the role of rate limiter
A rate limiter restricts how many requests a user or client can send in a certain time to prevent overload.Step 2: Identify the correct purpose
Among the options, only controlling request rate matches the rate limiter's function.Final Answer:
To control the number of requests a user can make in a given time -> Option AQuick Check:
Rate limiter = control request rate [OK]
- Confusing rate limiter with load balancer
- Thinking it speeds up database queries
- Assuming it stores user data
Solution
Step 1: Recall sliding window mechanism
Sliding window rate limiter tracks timestamps of requests in a time window, removing old ones as time moves.Step 2: Choose data structure for efficient insert and remove
A queue allows adding new timestamps at the end and removing old timestamps from the front efficiently, matching sliding window needs.Final Answer:
Queue -> Option CQuick Check:
Sliding window = queue for timestamps [OK]
- Using stack which is LIFO, not suitable
- Choosing hash map without order
- Picking binary tree which is complex here
Solution
Step 1: Track requests in 10-second window
Requests at 1, 3, 7 are allowed as they are within limit 3 per 10 seconds.Step 2: Check request at second 9
At second 9, previous requests at 1, 3, 7 are still within 10 seconds window (from -1 to 9). So 3 requests already made, this 4th request exceeds limit and is rejected.Final Answer:
Request at second 9 -> Option AQuick Check:
4th request in 10s window = rejected [OK]
- Ignoring requests older than 10 seconds
- Allowing all requests without limit
- Counting requests incorrectly
Solution
Step 1: Understand fixed window behavior
Fixed window counts requests in fixed intervals, resetting count at window end.Step 2: Identify burst cause
Requests near end of one window and start of next can both be allowed, causing bursts.Final Answer:
Fixed window resets counters abruptly causing bursts -> Option DQuick Check:
Fixed window reset causes bursts [OK]
- Confusing sliding window with fixed window
- Blaming rate limit value instead of algorithm
- Ignoring window reset behavior
Solution
Step 1: Consider scalability and accuracy needs
Millions of users require distributed design to avoid bottlenecks and reduce latency.Step 2: Evaluate options
Centralized fixed window causes bottleneck; client-only token bucket is insecure; no rate limiting risks overload. Distributed sliding window with local caches and sync balances accuracy and scalability.Final Answer:
Distributed sliding window using local caches and periodic sync -> Option BQuick Check:
Distributed sliding window = scalable + accurate [OK]
- Choosing centralized approach causing bottlenecks
- Relying on client-only enforcement
- Ignoring rate limiting and risking overload
