Leaky Bucket

Leaky Bucket

The leaky bucket algorithm is another method used for rate limiting. Unlike the token bucket algorithm, where tokens are added periodically, in the leaky bucket algorithm, tokens are leaked or removed at a constant rate, regardless of whether requests are made or not.

Here's how it works:

  • Imagine a bucket with a small hole in the bottom. Water (representing requests) is poured into the bucket at a variable rate.

  • The bucket has a maximum capacity, and if it fills up, any excess water (requests) spills out and is discarded.

  • At the same time, water leaks out of the bucket at a constant rate.

  • If the incoming rate of water is higher than the rate at which it leaks out, the bucket will eventually fill up and requests will be delayed or discarded until there is space available in the bucket.

In computing terms, the leaky bucket algorithm works similarly:

  • Requests are added to the "bucket" at a variable rate.

  • If the bucket fills up (exceeds its capacity), excess requests are either delayed or discarded.

  • Requests are processed at a constant rate, regardless of whether the bucket is full or not.

Here is a java implementation :

To view or add a comment, sign in

Others also viewed

Explore content categories