ThrottleKit provides two sliding window strategies that occupy different positions on the memory-vs-accuracy trade-off spectrum:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/AmeyaBorkar/throttlekit/llms.txt
Use this file to discover all available pages before exploring further.
slidingWindow— a sub-bucketed ring counter. Near-exact rolling window at any limit with O(buckets) memory per key. The sweet spot between fixed window (cheap, 2× boundary error) and the exact log (precise, unbounded memory).slidingWindowLog— an exact timestamp log. Stores every accepted unit’s timestamp and counts those within the trailingwindowMs. O(limit) memory per key; use for low or moderate limits where precision matters absolutely.
remaining stays accurate across repeated denials, and the count is always from a true rolling window with no reset spike at boundaries.
slidingWindow — sub-bucketed counter
The sub-bucketed counter divides the rolling window intoS equal slices and maintains a compact ring of counts, giving near-exact accuracy with O(buckets) memory per key.
How it works
The window is divided intoS equal sub-buckets (default S = 10), each of width w = windowMs / S. The current tick index is c = floor(now / w). On each check, ThrottleKit:
- Sums the counts for the
Snewest in-window ticks (the “full” buckets). - Weights the oldest partial bucket:
oldest * ((w − elapsed) / w), whereelapsed = now − c * w. - Estimates the total as
full + oldest * weight. - Admits if
estimate + cost <= limit.
1/buckets of the window), because only the single oldest bucket is weighted rather than measured exactly. With buckets: 10, the maximum error is 10% of the window width.
Options
The maximum units admitted within any trailing
windowMs.The rolling window length in milliseconds. The window trails the current time — there are no hard reset boundaries.
Number of sub-buckets the window is divided into. More buckets → smaller approximation error (bounded by
~1/buckets of the window) at O(buckets) memory per key. Default 10. Setting buckets: 1 recovers the classic single-previous-window weighted estimator.Code example
Trade-offs vs fixed window
Unlike fixed window, the sub-bucketed sliding window does not spike at boundaries — the rolling estimate smoothly ages out old counts as time advances. The trade-off is O(buckets) memory per key vs O(1) for fixed window.slidingWindowLog — exact
The log variant stores the timestamp of every accepted unit and counts only those within the trailing window, giving perfectly accurate rolling-window enforcement with no approximation error.How it works
The log stores the timestamp of every accepted unit and counts those withinnow - windowMs. Pruning the stale prefix is an index walk with no allocation. In Redis it uses a sorted set (ZREMRANGEBYSCORE + ZCARD + ZADD) inside one atomic Lua script, with members named by deterministic rank so the script is reproducible without TIME.
retryAfterMs is exact: the time until the oldest in-window unit expires and frees a slot.
Options
The maximum units accepted within any trailing
windowMs. Because the log stores one timestamp per unit, memory is O(limit) per key.The rolling window length in milliseconds.
Code example
Redis compatibility note
On Redis,slidingWindowLog uses a sorted set (ZSET) per key. This is different from slidingWindow, which uses a HASH ring. The ZSET approach requires no background cleanup: stale members are pruned atomically on each check via ZREMRANGEBYSCORE.
Comparison
slidingWindow | slidingWindowLog | |
|---|---|---|
| Memory per key | O(buckets) — fixed | O(limit) — grows with limit |
| Accuracy | Near-exact (error ≤ 1/buckets of window) | Exact |
retryAfterMs | Advisory approximation | Exact |
| Redis data structure | HASH ring | Sorted set (ZSET) |
| Best for | Any limit, bounded memory | Low/moderate limits, exact enforcement |
When to use each
UseslidingWindow when:
- Your limit is high (thousands or more) and you need bounded memory.
- A small approximation error (default ≤ 10% of window) is acceptable.
- You want to avoid the boundary burst of fixed window without the memory cost of the log.
slidingWindowLog when:
- Your limit is low or moderate (single digits to a few hundred).
- Exact accuracy is required — e.g., password resets, OTP sends, sensitive operations.
- You need the exact
retryAfterMs(time until the next slot opens).