Skip to content

Floor the reconnect delay at 0.1 s for fractional --reconnect-backoff-factor - #694

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix-688-reconnect-delay-floor
Oct 16, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix-688-reconnect-delay-floor

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

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 then in 0.00 seconds from attempt 8 on. After that memtier reconnects in a tight loop. On master 6eedafb, one connection wrote 303,915 reconnect lines (303,908 of them 0.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_reconnect and the retry path of handle_reconnect_timer_event) do m_current_backoff_delay *= reconnect_backoff_factor and then clamp only the upper bound, MEMTIER_BACKOFF_CAP_SEC. Nothing sets a lower bound.

Fix

  • Add MEMTIER_RECONNECT_MIN_DELAY_SEC = 0.1 next 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.
  • Why 0.1 s: it is 10x shorter than the default 1 s, so fast reconnects are still possible. It also limits each connection to about 10 attempts per second. With a 10 ms floor, 200 connections could still make about 20k connects per second against a dead server.
  • --help and memtier_benchmark.1 now 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 against help2man output. 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_floor connects to a closed TCP port. ECONNREFUSED arrives in the event callback, so every attempt goes through attempt_reconnect(). The test checks:

    • 11 or 12 delays are logged;
    • every delay is >= 0.1;
    • the delays start 0.5, 0.25, 0.12 and are exactly 0.1 after that;
    • the run takes at least 1.5 s, so the logged delays must be the ones actually armed. The expected timers add up to about 1.78 s, and a correct run takes about 2.0 s.
  • test_reconnect_backoff_fraction_floor_on_failed_reconnect starts its own redis on a unix socket and sends --command='SHUTDOWN NOSAVE'. Redis exits and unlinks the socket, so every reconnect() fails synchronously with ENOENT. That goes through the retry path of handle_reconnect_timer_event() ("Reconnection attempt N failed, retrying in X seconds"). The test checks:

    • at least 10 "retrying in" delays are logged;
    • every one is >= 0.1;
    • they are exactly 0.1 from the third one on.

    The socket is created under a short mkdtemp in /tmp, because RLTest log dirs can exceed the 108-byte sun_path limit. The test skips when unix sockets or redis-server are not available.

Results for the whole file (12 tests) with each binary:

binary result
origin/master 0062e56 2 failures. TCP delays [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, ...]
mutant: retry-path floor removed 1 failure: the unix test, delays reach 0.0
mutant: delay.tv_usec = 0 in attempt_reconnect 1 failure: the TCP test, elapsed 1.00 s < 1.5 s
this PR 12/12 pass. The two new tests passed 5 of 5 repeated runs.

Unlimited attempts, 10 s (timeout 10 ... --reconnect-backoff-factor=0.5 --test-time=5):

  • master: 303,915 reconnect lines, 1.92 s user + 6.91 s sys
  • this PR: 94 lines (about 10/s), 0 0.00 delays, 0.00 s user + 0.02 s sys

With factor 1e-300 (from the #668 review), every delay is now 0.10. The existing test_reconnect_backoff_zero_keeps_one_second_delay still passes, so factor 0 still waits 1.00 s.

Not in this PR: the run still outlives --test-time while reconnecting (rc 124 under timeout). 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-factor is 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.cpp introduces MEMTIER_RECONNECT_MIN_DELAY_SEC and clamps m_current_backoff_delay after each multiply in both reconnect scheduling paths (initial error reconnect and failed reconnect retry), alongside the existing 60 s cap. --help and memtier_benchmark.1 now document the floor and cap.

A new integration test runs memtier against a closed port with factor 0.5 and asserts delays follow 0.5 → 0.25 → 0.12 then stay at 0.1.

Reviewed by Cursor Bugbot for commit 3987164. Bugbot is set up for automated code reviews on this repo. Configure here.

fcostaoliveira and others added 2 commits October 16, 2026 10:54
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]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-688-reconnect-delay-floor branch from 3987164 to 68987bf Compare October 16, 2026 09:59
@github-actions

Copy link
Copy Markdown

🤖 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 reconnect() failure path through a unix socket. Otherwise the closed-port test would only cover attempt_reconnect(). The elapsed-time lower bound is also a nice touch, since it shows the logged delays are the ones that were actually armed.

Nitpicking: delays[:3] == [0.5, 0.25, 0.12] relies on %.2f rounding 0.125 down to "0.12". 0.125 is exact in binary and glibc/macOS round half to even, so I think it's fine. It's just a little subtle if someone later changes the log format.

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.

@fcostaoliveira
fcostaoliveira merged commit 1d13ff5 into master Oct 16, 2026
129 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…#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]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…#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]>
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.

A fractional --reconnect-backoff-factor turns reconnects to a dead server into a busy loop (no delay floor)

1 participant