October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Token Bucket vs Sliding Window: How to Choose a Rate-Limiting Algorithm

Token bucket allows controlled bursts; a sliding-window log enforces exact rolling quotas; and a sliding-window counter smooths boundaries with an approximate count. Learn how to choose and deploy each safely.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a token bucket when clients should be able to make controlled short bursts while staying within a steady long-term rate. Use a sliding-window log when an exact rolling quota matters more than the memory required to retain request timestamps. A sliding-window counter smooths fixed-window boundaries with much less state than a log, but estimates rather than exactly counts the rolling total.

There is no universally best rate limiter: the right choice depends on burst tolerance, quota precision, shared-state requirements, and what the service should do if its limiter becomes unavailable.

As an Amazon Associate I earn from qualifying purchases.

Token Bucket vs Sliding Window Explained

A rate limiter evaluates each request against a quota and decides whether to allow it, reject it, or sometimes delay it. The algorithms differ in how they represent recent activity and handle short spikes. “Sliding window” is not a single method: a timestamp log and a weighted counter have different precision and storage costs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

At a glance

Algorithm Burst behavior Precision and state Typical fit
Token bucket Allows bursts up to the bucket’s available capacity while tokens refill. Redis describes its example as exact; its comparison uses one hash key. Bursty APIs that need a controlled long-term rate but can tolerate short spikes.
Sliding-window log Does not allow requests to exceed the rolling-window quota. Exact rolling count; stores O(n) request entries in Redis’s comparison. High-value quotas or audit-sensitive limits where exactness justifies timestamp storage.
Sliding-window counter Smooths fixed-window boundaries instead of resetting abruptly. Near-exact estimate; Redis’s comparison uses two string keys. General-purpose APIs that need smoother rolling behavior without storing every request.
Fixed-window counter Can allow a boundary burst of up to twice the nominal limit. Approximate; Redis’s comparison uses one key. Simple limits where the boundary artifact is acceptable.

These state and precision descriptions reflect Redis’s documented algorithm comparison and examples, not a universal benchmark across implementations. Redis rate limiter documentation

How token bucket works

Imagine a bucket with capacity B tokens that refills at r tokens per unit of time. As time passes, tokens accumulate up to the capacity. A request is allowed if enough tokens are available, and its cost is subtracted from the bucket. If there are too few tokens, the limiter rejects or delays it.

For the common case, each request costs one token. Because tokens accumulate during quiet periods, a client can spend them on a short burst; the refill rate still constrains sustained use over time. Capacity determines the maximum accumulated burst, while refill rate determines the long-run pace.

This is useful when short spikes are legitimate—for example, a client making several requests after a user action—but an unbounded sustained rate is not. A token cost can also represent work rather than a request count, though support for weighted costs depends on the specific limiter or gateway.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
Algorithm Design
  • Used Book in Good Condition

How sliding-window log works

A sliding-window log keeps the timestamps of requests in the current rolling interval. For each new request, the limiter removes timestamps older than the interval, counts the remaining entries, and allows the request only if adding it stays within quota.

Because it records individual events, the log can enforce an exact rolling-window count rather than approximating one from larger buckets. The trade-off is memory: state grows with the number of requests retained. Redis characterizes its log option as exact, with O(n) entries and no bursts beyond the rolling limit. Redis rate limiter documentation

How sliding-window counter works

A sliding-window counter stores counts for the current and immediately previous fixed intervals. To estimate the rolling total at the current moment, it weights the previous interval’s count by the portion that overlaps the rolling window, then combines that with the current interval’s count.

This avoids keeping every timestamp and reduces the abrupt reset associated with a fixed-window counter. It remains an estimate, not the exact event-by-event count of a sliding-window log. Redis describes its example as near-exact, using two keys and smoothing the window boundary. Redis rate limiter documentation

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to use each algorithm

Choose token bucket for controlled bursts

Use it when temporary spikes should pass but the overall rate should replenish steadily. Set the refill rate and capacity deliberately: a generous capacity admits larger bursts even if the steady rate is low. Amazon API Gateway documents token-bucket throttling with a steady-state rate and a burst limit. AWS API Gateway: throttle requests to your HTTP APIs

Choose a sliding-window log for exact rolling quotas

Use it when crossing a rolling limit has a meaningful cost and the memory needed for timestamp entries is acceptable. It is a stronger fit for precise, high-value limits than for very high-volume traffic where retained event state is prohibitive.

Rank #4
Sale
The Algorithm Design Manual
  • More and Improved Homework Problems
  • Self-Motivating Exam Design
  • Take-Home Lessons
  • Links to Programming Challenge Problems
  • More Code, Less Pseudo-code

Choose a sliding-window counter for a practical middle ground

Use it when a fixed-window reset is too rough but storing each request is too expensive. Be clear in system documentation that the result is an approximation.

Choose fixed window only if its boundary behavior is acceptable

A fixed-window counter is simple, but a client can use its full quota just before a window resets and again just after. That can produce a burst of up to twice the nominal limit around the boundary, according to Redis’s comparison. If that behavior is unacceptable, use a method that accounts for recent traffic across the boundary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Design the quota and shared state

Before choosing an algorithm, decide what identity the quota applies to: a user, IP address, API key, tenant, or another client identifier. Then decide whether the limit is local to one process, shared across service instances, or coordinated across regions. Those choices can matter as much as the algorithm.

Best Value

A per-process counter can be bypassed when a load balancer routes one client’s requests to different servers. If the intended quota applies across the whole service, instances need shared state and an atomic decision. Redis describes using a shared store and Lua scripts to keep the read-decide-update operation together, preventing concurrent requests from double-spending tokens or overwriting counter updates. Its examples include fixed-window, sliding-window, and token-bucket approaches, with ecosystem integrations such as Node.js, Python, Go, and API gateways. Redis rate limiter documentation

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Plan for failures and client retries

Choose fail-open or fail-closed intentionally

If the limiter’s state store is unavailable, a fail-open policy allows requests to proceed; a fail-closed policy denies them. Choose based on the consequences: allowing requests may protect availability but weaken quota enforcement, while denying them may preserve a limit but block legitimate traffic. Bound the limiter check with a timeout so a slow dependency cannot hold request handling indefinitely. Redis’s Go implementation guide identifies this policy choice and timeout as operational considerations. Redis token-bucket rate limiter with Redis and Go

Make denial and retry behavior understandable

When a request is rejected, return a clear rate-limit response and give clients a way to retry without immediately repeating the same burst. AWS says API Gateway can return HTTP 429 when rate or burst targets are exceeded and advises clients to resubmit failed requests in a rate-limited way. Its configured throttle settings are best-effort targets, not guaranteed request ceilings. AWS API Gateway throttling documentation

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Account for distributed and edge enforcement

A shared counter is not automatically global. Cloudflare documents that its rate-limit counters are maintained by data center rather than globally across its entire network, with an exception for multiple data centers associated with a geographical location. A client reaching different locations may therefore encounter behavior unlike a single centralized counter. Check the counter scope of the service you deploy rather than assuming a provider-wide quota. Cloudflare: how request rate is determined

For workloads where requests vary substantially in compute cost, request count alone can be a poor measure of load. Cloudflare documents cost-based rate limiting for Enterprise customers using Advanced Rate Limiting: the origin returns a numeric score in a response header, and a rule applies a score budget per client over a period. The documented score range is 1 to 1,000,000. This is a plan-qualified product feature, not a property of rate-limiting algorithms generally. Cloudflare rate-limiting parameters

What the algorithms cannot tell you

There is no universal evidence that one of these algorithms is always faster or cheaper. Actual latency, throughput, and storage use depend on the implementation, state backend, traffic pattern, and deployment topology. Redis describes its own rate-check example as sub-millisecond, but that is a vendor description of its solution, not a general benchmark for arbitrary systems. Likewise, a cloud provider’s configured limit may be a target rather than a hard ceiling, and edge counters may have a narrower scope than a globally shared quota.

Quick Recap

SaleBestseller No. 2
Algorithm Design
Algorithm Design
Used Book in Good Condition
$223.93
Bestseller No. 3
SaleBestseller No. 4
The Algorithm Design Manual
The Algorithm Design Manual
More and Improved Homework Problems; Self-Motivating Exam Design; Take-Home Lessons; Links to Programming Challenge Problems
$65.49
SaleBestseller No. 5
Introduction to the Design and Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
Used Book in Good Condition
$142.68

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.