Skip to content

fix(monitor-cluster): end-to-end backpressure for staged monitor replay - #399

Merged
fcostaoliveira merged 5 commits into
redis:masterfrom
filipecosta90:fix/monitor-cluster-backpressure
May 29, 2026
Merged

fcostaoliveira merged 5 commits into
redis:masterfrom
filipecosta90:fix/monitor-cluster-backpressure

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented May 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Replaying a MONITOR capture against an OSS cluster (--monitor-input + --command __monitor_line@__ + --cluster-mode) shows latency that grows monotonically over the run — p99 climbs 3s → 6s → 40s+ — while p50 stays at true server latency (~1–2ms) and throughput overshoots the rate target by ~2×. The pathology is identical across RS6/RS7/RS722P, which is the tell that it's client-side, not server.

Root cause — no end-to-end backpressure

The design is route-then-stage:

  1. Each shard connection reads a monitor line, computes the key's slot, and (when the key belongs to another shard) pushes the command into that shard's staged queue m_staged_monitor_commands[target] and wakes it via schedule_fill().
  2. Staging never grows the routing connection's own m_pipeline. push_req() — the only place that grows m_pipeline, bumps m_pending_resp, and decrements the rate counter — fires only when a command is actually sent (at drain time, on the target). So fill_pipeline's m_pipeline->size() < pipeline gate never throttles the producer.
  3. The routing side keeps selecting and fanning out commands (bounded only by the rate limiter), while each target drains at most ~pipeline-per-RTT. The staged queues grow without bound (up to the 1M cap).
  4. Latency is measured from selection (enqueue_time), so reported latency = queue-residence + RTT → the monotonic p99 climb.

Fix

Couple production to drain by capping the global end-to-end in-flight count — staged + sent-awaiting-response, which equals m_reqs_generated - m_reqs_processed — at pipeline * connection_count in cluster_client::hold_pipeline. A connection that still has its own staged or pooled commands to drain is never held, so draining (and forward progress) is never blocked; only pure producers pause until responses bring the backlog back under budget. Scoped to --monitor-input runs, so the SET/GET and arbitrary-command cluster paths are unaffected.

Validation

Against a local 3-node OSS cluster (1 thread, 4 clients, pipeline=1, 4000 reqs/client, identical workload):

metric no fix with fix
avg latency 25.41 ms 0.227 ms
p50 19.33 ms 0.199 ms
p99 86.53 ms 0.663 ms
p99.9 92.16 ms 1.135 ms

No monotonic climb with the fix. (Absolute numbers are loopback; over a real network the no-fix tail reaches the tens of seconds described above.)

Test plan

  • tests/test_monitor_cluster_backpressure.py — replays a multi-slot monitor capture against an OSS cluster and asserts p99 stays bounded (<50ms ceiling, >25× margin) and the run completes with non-zero ops. Runs in the OSS-CLUSTER CI jobs.
  • Standalone suites unaffected (change is cluster-only): test_monitor_input.py passes.

🤖 Generated with Claude Code


Note

Medium Risk
Touches cluster pipeline scheduling for monitor-input only, but incorrect backpressure could stall producers or skew throughput/latency under cluster replay.

Overview
Adds end-to-end backpressure for --monitor-input cluster replay when commands are route-then-staged across shards. In cluster_client::hold_pipeline, pure producer connections (no local staged or pooled work) pause when global in-flight work—m_reqs_generated - m_reqs_processed—reaches pipeline × connection_count, so staging cannot outrun drain and p99 latency is not dominated by queue residence. The check is scoped to monitor-input runs; connections still draining their own queues are never held. Unsigned underflow on the in-flight subtraction is clamped when --retry-on-error can process without a matching generate bump.

Adds tests/test_monitor_cluster_backpressure.py: OSS cluster replay with cross-shard keys asserts p99 < 50ms and a smaller sanity run completes with non-zero ops.

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

…eplay

In cluster mode, --monitor-input + `--command __monitor_line@__` routes each
selected command to the slot-owning shard by pushing it into that shard's
staged queue (m_staged_monitor_commands) and waking it via schedule_fill().
Staging never grows the routing connection's own m_pipeline, so fill_pipeline's
`m_pipeline->size() < pipeline` gate cannot throttle the producer: the routing
side keeps selecting and fanning out commands while each target drains at most
~pipeline-per-RTT. The staged queues grow without bound, so reported latency
(measured from selection) climbs monotonically as queue residence dominates the
tail (p99 3s -> 6s -> 40s+ over a real network) while p50 stays at true server
latency, and throughput overshoots the rate target.

Couple production to drain by capping the global end-to-end in-flight count --
staged + sent-awaiting-response == (m_reqs_generated - m_reqs_processed) -- at
pipeline * connection_count in cluster_client::hold_pipeline. A connection that
still has its own staged or pooled commands to drain is never held, so forward
progress is never blocked; only pure producers pause until responses bring the
backlog back under budget. Scoped to --monitor-input runs so the SET/GET and
arbitrary-command cluster paths are unaffected.

Verified against a local 3-node cluster (1 thread, 4 clients, pipeline=1,
4000 reqs/client): p99 dropped from 86.5ms to 0.66ms and average latency from
25.4ms to 0.23ms for an identical workload, with no monotonic climb.

Adds tests/test_monitor_cluster_backpressure.py asserting p99 stays bounded
when replaying a multi-slot monitor capture against an OSS cluster.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
fcostaoliveira and others added 4 commits May 28, 2026 18:34
Review flagged that with --monitor-input + --retry-on-error a
redirected/replayed request can be counted in m_reqs_processed more than once
without a matching m_reqs_generated bump, so the unsigned
(m_reqs_generated - m_reqs_processed) could underflow to a huge value and wedge
the producer permanently. Clamp to 0 when processed >= generated. Defensive:
the gate already only pauses pure-producer connections, but this removes any
chance of a wrapped in-flight count stalling the run.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit 893006e into redis:master May 29, 2026
65 checks passed
@fcostaoliveira fcostaoliveira added this to the 2.4 milestone Jun 1, 2026
fcostaoliveira added a commit that referenced this pull request Jun 2, 2026
… sites (#437)

PR #399 introduced a >= clamp at hold_pipeline; the three sibling sites
(connect_shard_connection, handle_cluster_slots, handle_moved) subtract
empty_staged.size() with no such guard. Today the invariant -- entries
in empty_staged are pre-decrement -- holds, but a refactor could break
it silently. Matching the pattern makes the invariant explicit and
future-proofs against subtle underflow to 2^64.

Refs: 2.4 review #12 (Review 1 + Review 16).

Co-authored-by: Claude Sonnet 4.6 <[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.

2 participants