Repository navigation
Stagger rate-limited client refills - #519
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this — really thorough writeup and benchmark data, that's more validation than most PRs on this repo bring. Went through the diff. The phase math in rate_limiter.h looks right — multiplication widened to uint64_t before the divide, client_index reduced mod total_clients, and the unit tests cover the wraparound/overflow cases I'd have asked about anyway (the 3999999999/4000000000 case in particular). The event_del/event_add dance in handle_timer_event to swap the persist interval after the first fire is the correct way to change an EV_PERSIST timer's cadence mid-flight, and m_request_rate_phase_pending correctly handles the case where a connection reconnects before its staggered first fire ever happens vs. after. One question: Nitpicking: the Also a nice incidental catch: normalizing the timer into tv_sec/tv_usec via division/modulo instead of the old Nothing else stood out — the change doesn't touch any CLI flags or output/JSON format, is scoped to the rate-limiting path, and the benchmark numbers (latency percentiles dropping while throughput and CPU stay flat or improve) back up the claim well. Good first PR. |
fcostaoliveira
left a comment
There was a problem hiding this comment.
Approving — went through the diff directly: the phase-staggering logic in calculate_rate_limit_phase() is correct (uint64_t-widened multiply before divide, safe on zero interval/zero clients), it's applied consistently through both client::setup_client and cluster_client::create_shard_connection, the phase only affects the first timer fire before handle_timer_event resets to the normal cadence (so it doesn't distort the configured rate long-term), and reconnect semantics are handled correctly per the PR description. Real benchmark data backs the tail-latency claim (p99.99 -69%, CPU -19%, throughput within noise), and the two more aggressive variants were tried and correctly rejected with data. CI is fully green (111/111, 1 skipped). Nice first PR.
rate_limiter_test (#519) and socket_options_test (#541) were added to check_PROGRAMS without matching .gitignore entries, so after `make check` both binaries show up as untracked files. List them next to the other three check programs; the five entries now match check_PROGRAMS in Makefile.am. Also ignore the genhtml report directory that `make code-coverage-capture` writes (memtier_benchmark-<version>-coverage/). The .info file and the gcov artifacts were already covered by *.info / *.gc*. Fixes #623 Co-Authored-By: Claude Opus 5.5 <[email protected]>
rate_limiter_test (#519) and socket_options_test (#541) were added to check_PROGRAMS without matching .gitignore entries, so after `make check` both binaries show up as untracked files. List them next to the other three check programs; the five entries now match check_PROGRAMS in Makefile.am. Also ignore the genhtml report directory that `make code-coverage-capture` writes (memtier_benchmark-<version>-coverage/). The .info file and the gcov artifacts were already covered by *.info / *.gc*. Fixes #623 Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
Problem
Rate limiting currently gives every connection its full quota immediately and arms every refill timer with the same interval. Connections prepared together therefore refill together.
For example, 80 connections at 1,250 requests/sec each receive 25 requests every 20 ms. In steady state that can arrive as 2,000-request bursts every 20 ms even though the reported aggregate rate is exactly 100k requests/sec. The resulting server-side queueing makes latency percentiles describe the benchmark client's synchronized burst pattern as much as the server.
Implementation
Each logical client gets a deterministic phase:
The first timer fires after
refill_interval + phase. In that callback the persistent event is re-armed with the original refill interval, preserving both the configured request rate and the existing steady-state quota behavior.A single client has phase zero and follows the original path. On reconnect, a phase that has not fired yet is retried; after initialization, reconnects resume the ordinary interval without adding another phase delay.
Validation
Commands:
All pass, including the new
rate_limiter_test.Paired Redis 8.10.1 GET benchmark, 30 seconds, 8 threads, 10 connections/thread, pipeline 1, 128-byte values,
--rate-limiting=1250per connection:A one-client check remained at the requested rate: 1,250.05 QPS before and 1,249.98 QPS after.
For comparison, current Valkey
valkey-benchmark --rps 100000against the same Redis instance produced 99,738 QPS and a p99.99 between 0.527 and 0.575 ms. This PR reaches the same tail-latency range without replacing memtier's low-overhead quota/refill design.I also tested increasing refill frequency to 250 Hz. It regressed p99.99 to 1.143 ms because sub-millisecond phases coalesced in the event loop. Interleaving phases within every worker improved p99.99 slightly to 0.527 ms but raised client CPU to 1.471 cores, so neither higher-overhead variant is included here.
Note
Medium Risk
Changes core request-timing and refill behavior for rate-limited runs; incorrect phase math could skew throughput or burst patterns, but scope is isolated and covered by new unit tests.
Overview
Rate-limited logical clients no longer arm identical refill timers, which previously synchronized bursts across connections. Each client gets a deterministic phase via
calculate_rate_limit_phaseinrate_limiter.h(interval × client_index / total_clients), stored onclientand passed into everyshard_connection(including cluster shard connections created later).shard_connectiondelays only the first libevent timer byrefill_interval + phase, then re-arms the persistent timer with the normal interval inhandle_timer_eventso aggregate QPS and per-interval quotas stay the same while refills are spread across the window. Reconnect retries an unused phase once; after steady state, reconnect uses the ordinary interval.Adds
rate_limiter_testtomake checkwith unit tests for edge cases (zero interval/clients, wraparound, wide multiplication).Reviewed by Cursor Bugbot for commit 909f820. Bugbot is set up for automated code reviews on this repo. Configure here.