Skip to content
Ameen Abozaid
Planned lessonMID-034-L02

Token and leaky buckets

  • Lesson 2 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

explain the main algorithms and their burst behavior.

Planned topics

  • Token bucket: tokens refill at a rate up to a capacity; each request uses one. Allows bursts up to capacity.
  • Leaky bucket: requests enter a queue that drains at a fixed rate; smooths output, drops overflow.
  • Fixed window: count per minute; simple, but allows a double burst at the window edge.
  • Sliding window: counts over the last N seconds; fairer, costs more memory.
  • Choose token bucket for typical APIs: simple and burst-friendly.
Draft outline details

Demonstration

java
// token bucket, refill 10/s, capacity 20
double tokens = Math.min(capacity, tokens + elapsedSeconds * 10);
if (tokens >= 1) { tokens -= 1; allow(); } else reject();

Check your understanding

Which algorithm allows a short burst yet limits the average rate?

Show answer

Token bucket.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.