logo

System Design : Rate Limiting

Introduction

blog thumbnail

In system design, rate limiting is a critical mechanism used to control the number of requests or actions a user, service, or IP address can make within a defined period. By capping the request rate, rate limiting helps maintain system stability, fairness, and performance, especially under high load or potentially abusive situations.

Types of Rate Limiting

Fixed Window Algorithm

blog thumbnail

When it comes to rate limiting, there are several approaches, each suited to different use cases and system requirements.

The Fixed Window method is one of the simplest. It restricts the number of requests that can be made within a predefined, unchanging timeframe—usually per minute, hour, or day. For example, an API might allow 100 requests per hour. Once that limit is reached, no further requests can be made until the window resets. This approach is easy to implement but can be rigid, especially in situations where traffic is not evenly distributed.

Sliding Window Algorithm

blog thumbnail

On the other hand, the Sliding Window method introduces more flexibility. Rather than sticking to a fixed period, it tracks the time of each request and adjusts the rate limit accordingly. As each new request comes in, it slides the window forward to account for that request’s timestamp. This way, the system can handle bursts of traffic without locking users out immediately after a fixed period expires. This method is better suited for systems that need to be more dynamic and responsive.

Leaky Bucket Algorithm

blog thumbnail

The Leaky Bucket algorithm works by regulating traffic over time, allowing a steady stream of requests to pass through. Think of it like water leaking from a bucket at a constant rate. If the bucket is full (i.e., the system is overwhelmed), incoming requests are dropped or queued until there's space to process them. This method is great for smoothing out traffic spikes, ensuring that requests are handled at a steady rate, without letting a sudden surge cause system overload.

Token Bucket Algorithm

Finally, the Token Bucket algorithm offers the most flexibility, especially when dealing with burst traffic. It allows tokens to accumulate over time, and each request consumes a token. If there are tokens available, the request is processed; if not, it's denied or queued. This approach is beneficial for systems that need to allow occasional bursts of activity but still want to enforce an overall rate limit. For example, a system might allow a burst of 10 requests at once, as long as tokens are available, but then throttle back once those tokens run out.

Final Thoughts

In today’s world, where digital systems are interconnected and face heavy and unpredictable traffic, rate limiting has become a key element in robust system design, ensuring services can handle demand without compromising on quality or accessibility.