Skip to content

Correct rate-limiter windows, admission counting, and sizing - #77

Merged
gitgik merged 1 commit into
masterfrom
users/gitgik/fix-rate-limiter-contract
Nov 22, 2026
Merged

gitgik merged 1 commit into
masterfrom
users/gitgik/fix-rate-limiter-contract

Conversation

@gitgik

@gitgik gitgik commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Problem

The limiter mixes seconds and minutes, describes an incorrectly anchored rolling window, admits one request beyond the quota, and loses simultaneous requests if timestamps are used as set members. Its sizing and write-back-cache advice also overstate what the design establishes.

Changes

  • Define admission counting, exact window boundaries, retries, and failure-policy assumptions.
  • Distinguish aligned windows, exact admission logs, and conservative minute buckets, with worked boundary and eligible-success traces.
  • Explain atomic decisions, cleanup, the 61-bucket bound, and false rejections.
  • Correct decimal payload calculations and distinguish memory estimates from measured Redis allocations or throughput.
  • Explain why consistent hashing and independent write-back caches do not preserve a hard shared limit.

Update Markdown and both notebooks. No new services, dependencies, or executable lab.

Validation

Independently recomputed the published trace, boundary burst, bucket example, and payload equations with Python. Both notebook schemas pass; code, outputs, and metadata are unchanged. git diff --check passes.

GitHub CI passes on Python 3.9, 3.10, and 3.11. Markdown links and Codacy pass.

These are specification and worked-example corrections, not Redis integration or failover test results.

Define admission counting and rolling boundaries; distinguish conservative buckets and repair sizing, atomicity, and caching advice. Keep Markdown and both notebooks aligned. Independently checked worked traces and payload arithmetic; execution state is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7411b498-db61-4f72-8629-4af2b007c3a4
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The sliding-window diagram contradicts the stated admission times, and the fixed-window steps omit the key expiration needed to support the memory estimate.

2 open issues
What changed in this PR

This PR corrects the rate-limiter design guide across Markdown and both notebooks, clarifying quota semantics and the limits of the proposed storage and distributed designs. Changes are documentation-only; no executable implementation is added.

Changes:

  • Define admission counting, window boundaries, atomic decisions, and conservative bucket behavior.
  • Correct payload estimates and distinguish them from measured Redis memory use or throughput.
  • Clarify failure policies, sharding, and why independent write-back caches cannot enforce a shared hard limit.
File Description
executed_designing_api_rate_limiter.ipynb Updates the executed notebook’s explanatory text and worked examples.
designing_api_rate_limiter.md Revises the design specification, calculations, and operational guidance.
designing_api_rate_limiter.ipynb Mirrors the documentation corrections in the source notebook.

Review effort: Balanced

With a rate limit of 2 messages per second, the two messages sent at the 300th millisecond (m1) and 400th millisecond (m2), we'll count them as two messages starting from the 300th of that second to the 300th of the next second (making up one second).

As a result, we will therefore throttle M3, M4 as shown above.
Look backward from every request, not forward from the first request. With `L = 2` and `W = 1000 ms`, admissions at `300 ms` and `400 ms` cause `m3` and `m4` in the diagram to be rejected. Those admissions expire at `1300 ms` and `1400 ms`, respectively, so later requests can succeed without an aligned reset.
We can keep this in a hashtable, where:
1. Calculate `bucket_id = floor(t / 1000)`.
2. If the key is absent or its stored bucket differs, use this bucket with count zero.
3. Reject if `count >= 3`; otherwise increment the count and admit.
@gitgik
gitgik merged commit 37cf3ae into master Nov 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants