Repository navigation
Correct rate-limiter windows, admission counting, and sizing - #77
Merged
Merged
Conversation
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
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
There was a problem hiding this comment.
🟡 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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
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 --checkpasses.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.