A burst on one rate limit key ends in a 500 when compare-and-swap retries run out (flaky on Windows)

Open
#852 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
47/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
backend, databases

Research direction

Start with Bucket::update, back_off, and TRIES in crates/cache/src/rate_limit.rs, then run a_burst_of_concurrent_attempts_within_the_limit_all_succeed in tests/rate_limiting.rs on Windows. Compare the retry behavior with the measured timer wake-ups and review the three proposed approaches. Done means a burst on one key returns rate limit responses rather than a 500; the issue leaves the fix choice open.

Written by the indexing model from the issue text.

Description

rate_limit::attempt answers a burst on one key with a 500 instead of a 429 once the attempts keep losing the compare-and-swap. On Windows that already happens at 300 concurrent attempts, which makes a_burst_of_concurrent_attempts_within_the_limit_all_succeed flaky there; on Linux the same limit should be reached by a larger burst.

Version: 0.x at 6156f0e7

What happens

Bucket::update reads the bucket, compares and swaps, and on a lost swap waits back_off(lost): a random sleep below 1 << lost.min(6) ms, so at most 63 ms. After TRIES = 100 lost swaps it fails with the rate limit `rate_limit:…` kept changing while it was counted, a 500.

On Windows 11 (Rust 1.99.1), a_burst_of_concurrent_attempts_within_the_limit_all_succeed, 300 attempts against Limit::per_minute(1_000), failed in 2 of 9 runs, each time with that error:

Error: Error { status: 500, type: "laravel::error::Message", context: [], error: the rate limit `rate_limit:2fcef4b99cb2a5a83c4f2b5ec8fabde6` kept changing while it was counted, location: Some(Location { file: "crates\\cache\\src\\rate_limit.rs", line: 351, column: 13 }) }

Why

The jitter only spreads the retries if the timer can tell the sleeps apart. On Windows, tokio::time::sleep wakes on the system timer's ~15.6 ms tick. Measured with a current-thread runtime, 20 sleeps each:

requested measured
0 ms 2–15 ms
1, 3, 7 ms 14–16 ms
15 ms 15–31 ms
33 ms 44–47 ms
63 ms 74–78 ms

So the 0–63 ms window has about four distinct wake-ups instead of 64. The losers of a round wake together, read the same value, and all but one lose again. Every lost round costs each loser one of its 100 tries, and the slowest attempts run out.

The same ceiling exists with a fine timer, only higher: with the back-off capped at 64 ms, at most about one swap per millisecond can win for a key. A rough simulation of the loop (tasks waking on the same tick read the same value and one of them wins) puts the cliff on a 1 ms timer between 2,000 and 4,000 concurrent attempts on one key. I haven't measured that on Linux with a real store, where network latency would spread the reads, so please take that number as an estimate. A burst like that is the case a rate limiter is for, such as a login flood, and there it would answer 500s while repeating swaps against the store.

Possible fixes

  1. Let the back-off grow further, for example to 1 << lost.min(10) (about a second). Small change; it removes the Windows failures in the simulation, but the slowest attempt of a burst takes longer on every platform.
  2. Take turns within the process: attempts for the same key in one process wait on a per-key async lock before reading, so only attempts from different processes race the swap. Holds up best, but needs a map of locks that forgets idle keys.
  3. Count with an atomic store operation, as Laravel's RateLimiter does with increment. That gives up the GCRA bucket, so it's probably not what you want.

I'd be glad to send a PR for whichever you prefer.

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.