BeginnerCoder

Lesson 22 — Token bucket limiter (expert)

A bucket that refills

A token bucket is a fairer rate limiter. The bucket holds up to max tokens; each request takes one. Tokens refill over time (here: rate tokens per second), so a client can make a short burst and then continues at a steady pace. Real APIs (GitHub, Stripe) work like this.

We pass the time in seconds as now so the result is predictable.

Why it matters: it allows normal bursts while still stopping floods.

Want it another way?
Task

At the start of take(now), refill the bucket for the time since last (rate tokens per second, never above max), then remember now as last.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Node.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.