Repository navigation
Give each --retry-on-error retry its own backoff - #700
Conversation
|
🤖 Automated first-pass review. A human maintainer's review is still required before merge. Thanks for this. I went through the diff and the description. The root cause analysis is convincing, and the fix (a due time per request, one timer armed for the earliest due time, and a drain that resends only what is due) looks right to me. Removing
The core fix looks solid. I'd mainly like an answer on the (I'd leave this as a plain comment, not a formal request for changes.) |
c7a181d to
b4ae5c4
Compare
|
Thanks. You were right about the design question, and the premise needs one correction: the first version (24c7bce) did make requests wait longer than their own backoff, not only shorter. The drain timer was armed with the delay of whichever request found it idle, and then resent the whole queue. So a first retry could ride a timer armed by a request on its 3rd-5th retry, and the reverse happened too. Reviewers measured this on 24c7bce:
My Point by point, on b4ae5c4:
|
b4ae5c4 to
53f0fcd
Compare
|
Thanks. Taking your updated points in order, on 53f0fcd (rebased on master b1c46f4):
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 53f0fcd. Configure here.
53f0fcd to
ead0c7a
Compare
The retry backoff was a single value per connection: enqueue_retry() multiplied it by --retry-backoff-factor on every queued retry, of any request, and it was reset only when a retried request later succeeded. A request that gave up after --max-retries left it where it was, so the next request started from there, and requests in flight on the same connection compounded each other's delay. With every SET failing, -n1/-n2/-n3 with --max-retries=5 --retry-backoff-ms=10 --retry-backoff-factor=2 took 1 s / 4 s / 47 s, and a --test-time=8 run was still running at 100 s. The connection's single drain timer also resent the whole retry queue when it fired, at the delay of whichever request had armed it. So with mixed outcomes a first retry could wait for a timer armed by a 4th retry, and a 4th retry could ride a timer armed by a first one. Each queued retry now gets its own due time on the monotonic clock, now + retry-backoff-ms * factor^resends (retry_backoff_delay_ms() in retry_backoff.h). The timer is armed for the earliest due time, brought forward when a request with an earlier due time is queued, and when it fires it resends only the requests that are due, in queue order, and re-arms for the next one. The per-connection value and its reset on success are removed. The accounting, the move to the replay queue on disconnect and the --transaction refusal are unchanged. MOVED/ASK redirects count as resends, so they get the same backoff. Requests now wait for retries in the queue rather than in flight, so the queue fills more often, and fill_pipeline() sent new requests until it was exactly full: requests in flight that then failed found it full and were given up on with retries=0. fill_pipeline() now holds new work while queued retries plus requests in flight reach the queue cap, so a request that fails in flight always has room. --pipeline still counts only requests in flight. The hold starts at --pipeline at the least, so a smaller --max-retry-queue does not cut the pipeline on a healthy server; the queue itself stays capped at --max-retry-queue. When the drain timer fires while the connection cannot send, the due requests stay queued and the timer goes idle. A retry queued after that now keeps the timer at their due time rather than arming it for its own, later one, so they go out as soon as the connection is set up. The factor's growth stops at 60 s, as before, but a --retry-backoff-ms above 60 s is now used as is with any factor: before, a factor > 0 capped it from the second retry on. Fixes #696 Co-Authored-By: Claude Opus 5.5 <[email protected]>
ead0c7a to
63dbab2
Compare
|
Thanks. Point by point, on 63dbab2 (rebased on master 0370514):
Separately, a Cursor finding (overdue retries delayed by a later enqueue after a blocked drain) is fixed in the same push, with a unit test. That reply is inline. |
|
Re the automated review posted at 13:22:09, which crossed with my 13:22:18 reply. The reply covers points 1, 2 and 4. On the rest:
|

Decisions for maintainers:
retry-backoff-ms * factor^resends, whereresendsis the number of times that request has already been resent. That holds however many other requests share the connection's retry queue. The 60 s cap limits only the factor's growth: a--retry-backoff-msabove 60 s is used as is.--pipelinestill counts only requests in flight.--max-retry-queuestays the hard limit on the queue. The hold starts atmax(cap, --pipeline), so a smaller--max-retry-queuecannot cut the pipeline on a healthy server. The guarantee then holds only for--max-retry-queue >= --pipeline, which includes the auto cap.Problem
With
--retry-on-errorand--retry-backoff-factor> 1, the retry backoff keeps growing across requests. Setup: every SET fails (maxmemory 1,noeviction), flags-t1 -c1 --ratio=1:0 --retry-on-error --max-retries=5 --retry-backoff-ms=10 --retry-backoff-factor=2 --connection-stage-timeout=0. Then-n1/-n2/-n3take 1 s / 4 s / 47 s, and a--test-time=8run was still running at 100 s.Root cause
There are two problems, both caused by keeping the retry state per connection instead of per request.
enqueue_retry()multipliedm_current_retry_backoff_msby the factor on every queued retry, from any request.reset_retry_backoff()ran only when a retried request later succeeded. A request that gave up after--max-retriesleft the value inflated for the next request.Fix
retry_backoff.h: new header-onlyretry_backoff_delay_ms(base_ms, factor, resends), which computesbase_ms * factor^resends.base_ms == 0gives 0, so0 * infnever happens.enqueue_retry(): gives each queued request its ownm_retry_due = now + delay. The connection keeps one timer, armed for the earliest due time (m_retry_timer_due). It is brought forward when a request with an earlier due time is queued, so a request sitting at the 60 s cap with--max-retries=-1no longer holds the others back.handle_retry_drain_event(): resends only the requests that are due, with 1 ms slack for timer/wall-clock skew. It keeps the rest queued in their original order and re-arms the timer for the next due time.m_current_retry_backoff_ms,reset_retry_backoff()and its two callers inclient.cpp.fill_pipeline()): new work used to be held only once the retry queue was exactly full. Requests already in flight that then failed found it full, andenqueue_retry()gave them up withretries=0even with--max-retries=4. Per-request backoffs keep requests in the queue longer, which made this about 10x more frequent. Nowfill_pipeline()holds new requests whilequeued retries + requests in flight >= cap(auto capmax(pipeline*4, 64), or--max-retry-queue), so every request in flight has a free slot.enqueue_retry()still refuses when the queue is full. That can still happen for a MOVED/ASK redirect arriving from another connection.--pipelineis unchanged: queued retries still do not count against it.--max-retry-queuekeeps its meaning:retry_queue_full()still uses the user's value as the hard limit. Only the hold inretry_queue_holds_new_work()usesmax(cap, --pipeline). Otherwise requests in flight alone would reach a small cap and cut the pipeline on a healthy server.--pipeline=64 --max-retry-queue=4against a healthy redis (-t1 -c1 --ratio=1:1, 4 s, pinned to one CPU): 53f0fcd made 94-113k ops/sec; this branch makes 455-457k, and master makes 451-499k.--max-retry-queuebelow--pipeline: requests in flight beyond the cap have no guaranteed slot and can be given up early, as on master. With every SET failing,--pipeline=64 --max-retry-queue=4 --max-retries=3 -n640gives up 632 of 640 atretries=0. With--max-retry-queue=64it gives up none early.--max-retry-queuehelp text and man page used to say "the pipeline stops accepting new work when full". They now say what happens.enqueue_retry()used to arm the timer for its own, later due time.fill_pipeline()skips the drain while the timer is pending, so the overdue requests waited out the new request's backoff. Now, when the timer is idle and requests are already queued,m_retry_timer_duestill holds their earliest due time, and enqueue keeps the timer armed for that earlier time.CLOCK_MONOTONIC, like the existing check incluster_client.cpp, so a wall-clock step cannot stall or release queued retries. The old code passed only relative delays to the timer. The 1 ms slack in the drain only saves a timer wakeup.disconnect()moving the retry queue into the replay queue and cancelling the timer;--transactionrequests are still refused.m_retries), so they get the same backoff as any other retry.--retry-backoff-ms=120000 --retry-backoff-factor=1now waits 120 s on every retry. On master the base was used for the first retry, but a factor > 0 capped later retries at 60 s. With factor 0 nothing changes.--retry-backoff-factorhelp text and man page now say what happens. The README does not document the retry flags.Test evidence
test_retry_backoff_with_mixed_failures(new). A fake RESP server answersSET memtier-Nwith -TRYAGAIN the firstN % 5times. Run: 40 keys,--pipeline=8 --rate-limiting=20,--max-retries=4 --retry-backoff-ms=20 --retry-backoff-factor=4. The test checks that every gap between resends of a key is within[expected - 3 ms, expected * 1.25 + 25 ms]of that key's own backoff. Requests that fail while a 1.28 s timer is pending also exercise the early re-arm.test_retry_backoff_is_per_request(changed). Now-n200instead of-n3, because-n3fits in one batch. It asserts:errors 200,retries 1000;retries=5;test_requests_in_flight_have_room_to_retry(new). A fake server fails about 60% of attempts, deterministically (crc32 of key and attempt). The run is-n600 --pipeline=8 --max-retries=4, factor 2, base 10 ms. The test asserts that no failed-keys row hasretries < 4, and that the logged keys are exactly the keys whose 5 attempts all failed.test_small_max_retry_queue_keeps_pipeline_depth(new). A fake server holds its replies until 64 SETs are waiting (or 0.5 s), with--pipeline=64 --max-retry-queue=4, no errors. It asserts that 64 requests were in flight at once. Result: 53f0fcd fails 2/2; this branch passes.tests/unit/retry_drain_test(new,make check). A real client and connection over a Unix socket:Result: it fails without the enqueue change ("overdue retry sent within 500 ms of connecting"); it passes 6/6 with it.
Sanitizer tolerance in the mixed test. TSAN CI cells failed on the same two gaps every time (memtier-19 resend 2, memtier-21 resend 1). I reproduced this locally with
./configure --enable-thread-sanitizerandsetarch -R. The cause is not an ordering bug in this PR. A TSAN build stalls the worker for ~200 ms about 1 s into the run, and every send due in that window is late, including first sends (memtier-22's first send moved from 1050 to 1219 ms). The same stall shows up with--retry-on-erroroff: 192 ms on this branch's TSAN build, and 225 and 244 ms on an origin/master TSAN build. The normal build has no stall (largest gap between first sends 50 ms). So only whenASAN_OPTIONS/TSAN_OPTIONS/UBSAN_OPTIONSis set does the test widen the upper bound toexpected*1.5 + 100 msand allow 10% of gaps (8 of 80) beyond it. The lower bound stays strict, and non-sanitizer builds keep the strict window with no outliers. With the sanitizer window, 24c7bce still fails with 32-60 of 80 gaps off. Local TSAN results:test_retry_on_error.py: 27/27 pass, no ThreadSanitizer reports.tests/unit/retry_backoff_test(new,make check). Covers factor 0/1/0.5/2, base 0, underflow, the 60 s cap, overflow to inf (DBL_MAX, 4e9 resends) and bases above 60 s.--max-retry-queue<--pipeline)The three tests also pass under load: 9 at a time, all pinned to 2 CPUs with
taskset -c 0,1, over 2 rounds (18/18). The review's test lens reports 35/35 for the mixed test across normal, taskset and ASan builds.Failed-keys rows logged with retries left, same fake server,
-n600 --max-retries=4. 53 keys are expected to fail all 5 attempts:All runs took about 1 s, so holding new work did not cost throughput here.
Resend gaps in the mixed scenario, in ms (median, with min-max in brackets):
Manual repro with the issue's flags on this branch:
-n3: 1.0 s, average latency 311 ms;-n200: 2.0 s, average latency 313 ms;--test-time=5: 6.0 s.In each run, errors equal the request count and retries are 5x that. The ~1 s floor comes from the stats tick.
Drain cost: each drain walks the queue once, which is O(queue) per drain, and that queue is bounded by the cap. Measured with the worst case for it: every SET failing,
--pipeline=1024(cap 4096),--retry-backoff-ms=1, unlimited retries, 5 s on one CPU.Other suites, all run on this branch (63dbab2 or an earlier head):
test_retry_on_error.py: 27/27 with--env ossand 27/27 with--env oss-clusteron 63dbab2; 27/27 on a TSAN build (ead0c7a);make check9/9;test_reconnections.py13/13,test_test_time_stalled_server.py4/4,test_cluster_slots_refresh_overlap.py1/1,test_cluster_readonly_drop.py3/3,test_cluster_redirect_stats.py9/9;test_cluster_failover_retry.py3/3,test_cluster_ask_asking.py2/2,test_cluster_failed_keys.py7/7;test_cli_validation_retry_backoff.py6/6;make check: 8/8.Fixes #696
Affected versions
2.4.0 through 2.5.2 (latest) and master (code). The stall is confirmed only on master.
enqueue_retry(), the reset that only runs on success, and the timer that drains the whole queue. The first tag containing it is 2.4.0.🤖 Generated with Claude Code