Repository navigation
Floor the reconnect delay at 0.1 s for fractional --reconnect-backoff-factor - #694
Conversation
A --reconnect-backoff-factor below 1 multiplies the reconnect delay down on every attempt, and only the upper bound (60 s) was clamped. Against a dead server the delay reached 0.00 s after 8 attempts, so memtier reconnected in a tight loop: ~300k reconnect log lines and ~9 s of CPU in 10 s from a single connection. Clamp the delay to 0.1 s after the multiplication in both reconnect scheduling sites. Factor 0 (constant 1 s) and the 60 s cap are unchanged. 0.1 s still reconnects quickly but bounds each connection to about 10 attempts per second. Document the floor and the cap in --help and the man page. Fixes #688 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The closed-TCP-port test only reaches attempt_reconnect(): ECONNREFUSED arrives in the event callback. Add a unix-socket case where redis exits via SHUTDOWN NOSAVE and unlinks its socket, so every reconnect() fails synchronously with ENOENT and goes through the retry path of handle_reconnect_timer_event(). The socket lives under a short mkdtemp in /tmp to stay below the sun_path limit. Also assert a lower bound on elapsed time so the logged delays have to be the delays that were actually armed. Move the floor constant after MEMTIER_BACKOFF_CAP_MS and note that factor 0 keeps the constant 1 s delay while a tiny positive factor settles at the floor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
3987164 to
68987bf
Compare
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks, this is a clean fix. On the question in the description: I think the 0.1 s floor is the right call over rejecting (0, 1). Rejecting would break command lines that work today. With the floor, the only thing that changes for those users is the delay no longer collapsing to 0.00, and nobody wants that tight loop. This is the same reasoning as PR#212, which picked the fix that keeps previously valid input working. 10 ms seems too permissive for the reason you gave: 200 connections hammering a dead server. The clamp is applied after the multiply in both scheduling sites, and the factor 0 path is untouched, so the constant 1 s behavior stays as it was. Good that the second test drives the synchronous Nitpicking: I'd be fine with this going in as is. Whoever lands #666 should double-check the rebase, since it touches the same two functions. |
…#712) A --retry-backoff-factor below 1 shrank each request's retry delay on every resend with no lower bound. With the default unlimited --max-retries, a request the server keeps rejecting (e.g. -OOM) reached a 0 ms delay after ~10 resends and was resent in a tight loop: about 100k-400k resends per second per connection, the opposite of a backoff. retry_backoff_delay_ms() now settles at MEMTIER_RETRY_BACKOFF_MIN_MS (100 ms, the same floor #694 gave reconnects), or at --retry-backoff-ms when that is smaller, so --retry-backoff-ms 0 still means no delay and a small base is never pushed up. Factor 0, factors >= 1 and the 60 s cap are unchanged. --help and the man page say so. Tests: the unit test covers the floor (decay to 100 ms, a base below 100 ms as its own floor, underflow, base 0). A new RLTest makes every SET fail with -OOM (maxmemory 1, noeviction), runs a 400 ms base with factor 0.5 and measures server rejected_calls per stuck request: ~9k-21k/s on master, 10/s with the fix (5-20/s accepted, so a floor at the base, 2.5/s, fails too). It restores maxmemory and maxmemory-policy afterwards. Fixes #712 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…#727) With --retry-on-error, a --retry-backoff-factor below 1 and unlimited --max-retries, the per-request retry delay base_ms * factor^resends decayed toward 0 ms. After about ten resends, a request the server keeps rejecting was resent in a tight loop (hundreds of thousands of retries per second), so the "back off" setting produced the highest possible request rate. retry_backoff_delay_ms() now never returns less than min(base_ms, 100 ms) (MEMTIER_RETRY_BACKOFF_MIN_MS), mirroring the 0.1 s reconnect floor from #694: - --retry-backoff-ms 0 still means no delay; - a base below 100 ms is its own floor, so a factor below 1 keeps the delay constant there; - factor 0, factors of 1 or more and the 60 s cap are unchanged. The floor also applies to MOVED/ASK resends, which go through the same retry queue. --help and the man page describe the floor. Tests: unit checks for the floor, a small base, underflow and base 0; and an RLTest against an always-OOM server that measures resends per stuck request (10/s with the fix; about 9,000-21,000/s on master), standalone and cluster, restoring maxmemory and maxmemory-policy afterwards. Fixes #712 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Decision: issue #688 asked whether to add a floor, reject factors in (0, 1), or only document them. This PR adds a 0.1 s floor on the reconnect delay. Fractional factors stay valid, so existing command lines keep working. Factor 0 (constant 1 s) and the 60 s cap are unchanged. If you would rather reject (0, 1) or use a different floor (10 ms, for example), say so and I will change it.
Problem
With
--reconnect-on-error --reconnect-backoff-factor=0.5, the delay to a dead server halves on every attempt: 0.50, 0.25, 0.12, ..., and thenin 0.00 secondsfrom attempt 8 on. After that memtier reconnects in a tight loop. On master 6eedafb, one connection wrote 303,915 reconnect lines (303,908 of them0.00) and used 1.92 s user + 6.91 s sys in 10 s.Root cause
Both reconnect scheduling sites in
shard_connection.cpp(attempt_reconnectand the retry path ofhandle_reconnect_timer_event) dom_current_backoff_delay *= reconnect_backoff_factorand then clamp only the upper bound,MEMTIER_BACKOFF_CAP_SEC. Nothing sets a lower bound.Fix
MEMTIER_RECONNECT_MIN_DELAY_SEC = 0.1next to the cap. Both multiply sites clamp to it right after the existing upper-bound clamp. The change stays local to the multiply, so it should compose with End a --test-time run at its deadline while reconnecting to a dead server #666, which changes reconnect scheduling in the same functions.--helpandmemtier_benchmark.1now say that values below 1 shrink the delay "but never below 0.1 seconds. The delay is capped at 60 seconds." I changed only that entry and checked it againsthelp2manoutput. The README does not document this option, so it is unchanged.Test evidence
I added two tests to
tests/test_cli_validation_reconnect_backoff.py, one for each floor site. Both use--reconnect-backoff-factor=0.5 --max-reconnect-attempts=12.test_reconnect_backoff_fraction_has_delay_floorconnects to a closed TCP port. ECONNREFUSED arrives in the event callback, so every attempt goes throughattempt_reconnect(). The test checks:test_reconnect_backoff_fraction_floor_on_failed_reconnectstarts its own redis on a unix socket and sends--command='SHUTDOWN NOSAVE'. Redis exits and unlinks the socket, so everyreconnect()fails synchronously with ENOENT. That goes through the retry path ofhandle_reconnect_timer_event()("Reconnection attempt N failed, retrying in X seconds"). The test checks:The socket is created under a short
mkdtempin /tmp, because RLTest log dirs can exceed the 108-bytesun_pathlimit. The test skips when unix sockets orredis-serverare not available.Results for the whole file (12 tests) with each binary:
[0.5, 0.25, 0.12, 0.06, 0.03, 0.02, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0]; unix delays[0.25, 0.12, 0.06, 0.03, 0.02, 0.01, 0.0, ...]delay.tv_usec = 0inattempt_reconnectUnlimited attempts, 10 s (
timeout 10 ... --reconnect-backoff-factor=0.5 --test-time=5):0.00delays, 0.00 s user + 0.02 s sysWith factor
1e-300(from the #668 review), every delay is now0.10. The existingtest_reconnect_backoff_zero_keeps_one_second_delaystill passes, so factor 0 still waits 1.00 s.Not in this PR: the run still outlives
--test-timewhile reconnecting (rc 124 undertimeout). That is what #666 fixes.Fixes #688
Affected versions
2.3.0 through 2.5.2 (latest) and master.
🤖 Generated with Claude Code
Note
Low Risk
Localized change to reconnect backoff timing and docs/tests; no auth, data, or API surface impact.
Overview
Adds a 0.1 second floor on reconnect delays when
--reconnect-backoff-factoris below 1, so fractional factors can still shrink the wait but no longer drive it to zero and spin against a dead server (fixes #688).shard_connection.cppintroducesMEMTIER_RECONNECT_MIN_DELAY_SECand clampsm_current_backoff_delayafter each multiply in both reconnect scheduling paths (initial error reconnect and failed reconnect retry), alongside the existing 60 s cap.--helpandmemtier_benchmark.1now document the floor and cap.A new integration test runs memtier against a closed port with factor
0.5and asserts delays follow0.5 → 0.25 → 0.12then stay at0.1.Reviewed by Cursor Bugbot for commit 3987164. Bugbot is set up for automated code reviews on this repo. Configure here.