Skip to content

fix(cluster): reconnect to nodes that restart without slot changes - #2096

Merged
PavelPashov merged 10 commits into
redis:mainfrom
maxbronnikov10:feat/cluster-node-retry-strategy
May 27, 2026
Merged

PavelPashov merged 10 commits into
redis:mainfrom
maxbronnikov10:feat/cluster-node-retry-strategy

Conversation

@maxbronnikov10

@maxbronnikov10 maxbronnikov10 commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes:
#587
#1732

Problem

When a cluster node (typically a replica) restarts without any slot changes,
ioredis permanently removes it from the connection pool and never reconnects.
This happens because retryStrategy is hardcoded to null for every node in
the pool — by design, to force MOVED-based topology refresh. However, MOVED
errors are never triggered when slots don't move, so the node is lost forever.

This causes two downstream issues:

  • scaleReads: "slave" silently drops replicas, degrading read throughput
  • With enableOfflineQueue: false, commands fail with
    "Cluster isn't ready and enableOfflineQueue options is false" because
    getInstanceByKey() returns undefined for the removed node

Common in Kubernetes environments during rolling restarts.

Changes

clusterNodeRetryStrategy — new option (mirrors clusterRetryStrategy)
that controls the retryStrategy for individual cluster node connections.
When set, a node that loses its connection stays in the pool and retries
instead of being permanently removed. If the node is later removed from the
cluster topology via reset() (triggered by MOVED or slotsRefreshInterval),
it is still cleaned up correctly.

new Cluster(nodes, {
  clusterNodeRetryStrategy: (times) => Math.min(100 + times * 2, 2000),
})

Note

Medium Risk
Changes cluster command routing and connection lifecycle for all cluster users; default behavior stays the same unless clusterNodeRetryStrategy is set, but the sendCommand refactor affects failover and offline-queue edge cases.

Overview
Adds clusterNodeRetryStrategy, a per-node retryStrategy for cluster member connections (default null, preserving MOVED-driven topology refresh). When set, restarted replicas can stay in the pool and reconnect instead of being dropped until slots move.

ConnectionPool wires that option into each node Redis instance; Cluster#sendCommand is refactored so commands with enableOfflineQueue: false fail fast with a closed-connection error when the chosen node isn’t ready/wait (enabling failover retries), while subscriber commands can still run when the subscriber is only connecting.

Docs, shared RetryStrategy typing, .gitignore for .history, and functional/unit tests cover reconnect, MOVED to new nodes, and subscribe behavior.

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

Copilot AI review requested due to automatic review settings April 9, 2026 18:59
@jit-ci

jit-ci Bot commented Apr 9, 2026

Copy link
Copy Markdown

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Cluster option to allow per-node reconnection retries so cluster nodes (notably replicas) that restart without slot changes aren’t permanently dropped from the pool—improving resiliency in rolling-restart environments.

Changes:

  • Introduces clusterNodeRetryStrategy (Cluster option) to control retryStrategy for individual node connections in the connection pool.
  • Updates cluster command dispatch to reject commands (when enableOfflineQueue: false) if the chosen node exists but is not ready (e.g., reconnecting), instead of silently queueing on the node’s offline queue.
  • Adds unit + functional tests covering default behavior, node retention vs removal, and offline-queue rejection behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/cluster/ClusterOptions.ts Adds the new clusterNodeRetryStrategy option (and updates typing to allow clusterRetryStrategy: null).
lib/cluster/ConnectionPool.ts Applies clusterNodeRetryStrategy (when provided) as the per-node retryStrategy; otherwise keeps retryStrategy: null.
lib/cluster/index.ts Passes clusterNodeRetryStrategy into the pool and rejects commands to non-ready nodes when cluster offline queue is disabled.
test/unit/clusters/ConnectionPool.ts Unit tests verifying how retryStrategy is set based on clusterNodeRetryStrategy.
test/functional/cluster/node_reconnect.ts Functional tests for node removal vs retention, command rejection with enableOfflineQueue: false, and reconnect after restart.
Comments suppressed due to low confidence (1)

lib/cluster/ConnectionPool.ts:86

  • clusterNodeRetryStrategy is being passed into defaults(..., this.redisOptions, ...), which means it will also be copied onto the per-node Redis instance options object (as an unknown option). Consider stripping clusterNodeRetryStrategy out before passing options to new Redis(...) to avoid leaking Cluster-only configuration into Redis options and reduce confusion when inspecting redis.options.
  createRedisFromOptions(node: RedisOptions, readOnly: boolean) {
    const redis = new Redis(
        defaults(
            {
              // By default, never try to reconnect when a node is lost,
              // instead, waiting for a `MOVED` error and fetching slots again.
              // When `clusterNodeRetryStrategy` is set, use it to allow
              // reconnection (e.g. for replica nodes that restart without
              // any slot changes).
              retryStrategy:
                typeof this.redisOptions.clusterNodeRetryStrategy === "function"
                  ? this.redisOptions.clusterNodeRetryStrategy
                  : null,
              // Offline queue should be enabled so that
              // we don't need to wait for the `ready` event
              // before sending commands to the node.
              enableOfflineQueue: true,
              readOnly: readOnly,
            },
            node,
            this.redisOptions,
            { lazyConnect: true }
        )

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/cluster/ClusterOptions.ts Resolved
Comment thread lib/cluster/index.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/cluster/index.ts Resolved
Comment thread lib/cluster/index.ts Resolved
@maxbronnikov10

Copy link
Copy Markdown
Contributor Author

@PavelPashov hello! can u take a look on PR please?

Comment thread lib/cluster/index.ts Resolved
Comment thread lib/cluster/index.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/cluster/index.ts Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 31ebcd1. Configure here.

Comment thread lib/cluster/index.ts Resolved
@PavelPashov

Copy link
Copy Markdown
Contributor

Thanks for the PR. I will review it and follow up once I have gone through it.

@PavelPashov

Copy link
Copy Markdown
Contributor

run sharded pub sub tests

@uglide

uglide commented Apr 11, 2026

Copy link
Copy Markdown
Contributor
Testcase Errors Failures Skipped Total
Root Suite 0 0 0 0
Sharded Pub/Sub E2E - Basic 0 0 0 4
Sharded Pub/Sub E2E - Connection Lifecycle 0 0 0 3
Sharded Pub/Sub E2E - Failure Recovery Multiple Subscribers 0 0 0 4
Sharded Pub/Sub E2E - Failure Recovery Single Subscriber 0 0 0 4

---- Details for maintainers

1 similar comment
@uglide

uglide commented Apr 11, 2026

Copy link
Copy Markdown
Contributor
Testcase Errors Failures Skipped Total
Root Suite 0 0 0 0
Sharded Pub/Sub E2E - Basic 0 0 0 4
Sharded Pub/Sub E2E - Connection Lifecycle 0 0 0 3
Sharded Pub/Sub E2E - Failure Recovery Multiple Subscribers 0 0 0 4
Sharded Pub/Sub E2E - Failure Recovery Single Subscriber 0 0 0 4

---- Details for maintainers

@uglide

uglide commented Apr 11, 2026

Copy link
Copy Markdown
Contributor
Testcase Errors Failures Skipped Total
Root Suite 0 0 0 0
Sharded Pub/Sub E2E - Basic 0 0 0 4
Sharded Pub/Sub E2E - Connection Lifecycle 0 0 0 3
Sharded Pub/Sub E2E - Failure Recovery Multiple Subscribers 0 3 0 4
Sharded Pub/Sub E2E - Failure Recovery Single Subscriber 0 2 0 4

---- Details for maintainers

@maxbronnikov10

Copy link
Copy Markdown
Contributor Author

Hello! @uglide Thanks for running the tests.

I believe these tests were already flaky before my changes — I've checked and confirmed that this PR doesn't touch any of the related logic.

However, I'm happy to investigate the issue and submit a fix in a separate PR. @PavelPashov what do you think?

@maxbronnikov10

Copy link
Copy Markdown
Contributor Author

@PavelPashov Hi! Sry for ping. What do u think about tests?

Comment thread lib/cluster/index.ts Outdated
Comment on lines +692 to +695
if (!_this.options.enableOfflineQueue && redis.status !== "ready" && redis.status !== "wait") {
command.reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we skip this guard for subscriber and sharded-subscriber connections? Those Redis instances manage their own offline queues, but this check runs before redis.sendCommand(), so commands can be rejected here instead of being queued by the subscriber connection.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i will try do it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check now pls, also add tests

@PavelPashov

Copy link
Copy Markdown
Contributor

@PavelPashov Hi! Sry for ping. What do u think about tests?

These tests are for internal use only, and they appear to pass in most runs, so the failures are likely intermittent.

@maxbronnikov10
maxbronnikov10 force-pushed the feat/cluster-node-retry-strategy branch from 0e31bf3 to a166917 Compare May 27, 2026 12:37

@PavelPashov PavelPashov left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution, this looks good. Finally can you please add the new clusterNodeRetryStrategy in the README cluster options section

When a cluster node (typically a replica) restarts without any slot changes,
ioredis permanently removes it from the connection pool and never reconnects.
This happens because  is hardcoded to  for every node in
the pool — by design, to force MOVED-based topology refresh. However, MOVED
errors are never triggered when slots don't move, so the node is lost forever.
@maxbronnikov10
maxbronnikov10 force-pushed the feat/cluster-node-retry-strategy branch from e51511d to 7a0ecaf Compare May 27, 2026 13:28
@maxbronnikov10

Copy link
Copy Markdown
Contributor Author

Rebased from master

@PavelPashov
PavelPashov merged commit c84b2ee into redis:main May 27, 2026
19 checks passed
@maxbronnikov10

Copy link
Copy Markdown
Contributor Author

@PavelPashov Hello! can i ask for a release this PR please?

github-actions Bot pushed a commit that referenced this pull request Jun 4, 2026
## [5.11.1](v5.11.0...v5.11.1) (2026-06-04)

### Bug Fixes

* **cluster:** reconnect to nodes that restart without slot changes ([#2096](#2096)) ([c84b2ee](c84b2ee))
* parse protocol-relative Redis URLs as TCP connections ([#2125](#2125)) ([131ee24](131ee24))
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 5.11.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

PavelPashov pushed a commit that referenced this pull request Nov 6, 2026
…en offline queue is disabled (#2277)

Since #2096, Cluster#sendCommand rejects a command with
enableOfflineQueue: false when its node connection is not ready/wait.
For commands of a pipeline this happens before the batch binds a
connection, so the batch is split: the commands that reached a
connection stay in its commandQueue but are never written, because
Pipeline flushes only after every command has written. The batch never
settles, and later replies on that connection go to the wrong commands.

Skip the check for pipeline commands (stream is set), so the batch goes
out as a whole on the connection it binds.

Fixes #2276
github-actions Bot pushed a commit that referenced this pull request Nov 23, 2026
# [6.1.0](v6.0.0...v6.1.0) (2026-11-23)

### Bug Fixes

* accept field arrays in hmget/hmgetBuffer types ([#2167](#2167)) ([c0cd66c](c0cd66c))
* align auto-pipeline callbacks for dynamic commands ([#2180](#2180)) ([7479cab](7479cab))
* apply the legacy reply shape to uppercase command names ([#2252](#2252)) ([8ecb423](8ecb423))
* clean up Sentinel failover subscriptions after quit ([#2198](#2198)) ([727edfa](727edfa))
* close the monitor connection when monitor() rejects ([#2247](#2247)) ([3cfe93f](3cfe93f))
* cluster stuck in close ([#2217](#2217)) ([a5fad1d](a5fad1d))
* **cluster:** bucket autopipelined MSET objects by their first key ([#2207](#2207)) ([b4e3237](b4e3237))
* **cluster:** do not reject pipeline commands for connecting nodes when offline queue is disabled ([#2277](#2277)) ([71516ca](71516ca)), closes [#2096](#2096) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [#2276](#2276)
* **cluster:** do not throw when auto-pipelining meets an uncached slot ([#2233](#2233)) ([e04bb49](e04bb49))
* **cluster:** don't fall back to a replica when no primary is left ([#2275](#2275)) ([c353e34](c353e34))
* **cluster:** emit sharded subscriber connect errors silently ([#2241](#2241)) ([00c9a22](00c9a22))
* **cluster:** end when quit() or disconnect() is called while reconnecting ([#2240](#2240)) ([8526785](8526785))
* **cluster:** hash autopipelined Buffer keys by their bytes ([#2208](#2208)) ([a8378bd](a8378bd)), closes [Command#getSlot](https://github.com/Command/issues/getSlot)
* **cluster:** hash lone surrogates as the UTF-8 Node sends ([#2242](#2242)) ([cfa464d](cfa464d))
* **cluster:** keep forwarding sharded messages after reconnecting from end ([#2251](#2251)) ([326e987](326e987))
* **cluster:** map every slot when stringNumbers is enabled ([#2255](#2255)) ([8ce4115](8ce4115))
* **cluster:** match Buffer shard channels when unsubscribing ([#2203](#2203)) ([d997d01](d997d01))
* **cluster:** pass the retry delay to the reconnecting event ([#2291](#2291)) ([ed50383](ed50383))
* **cluster:** resend only the redirected command in a pipeline ([#2214](#2214)) ([46b3191](46b3191)), closes [#2075](#2075) [Pipeline#exec](https://github.com/Pipeline/issues/exec) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [Pipeline#exec](https://github.com/Pipeline/issues/exec) [Cluster#sendCommand](https://github.com/Cluster/issues/sendCommand) [Pipeline#exec](https://github.com/Pipeline/issues/exec)
* **cluster:** stay ended when nodes removed by disconnect() end late ([#2237](#2237)) ([61896e5](61896e5)), closes [#2217](#2217)
* **cluster:** stop refreshing slots when disconnect() is called during a refresh ([#2249](#2249)) ([80501ed](80501ed))
* **cluster:** support zero-argument sunsubscribe with sharded subscribers ([#2137](#2137)) ([13ff050](13ff050)), closes [#2133](#2133)
* **cluster:** vendor cluster-key-slot to fix empty hash tag slot bug ([#2172](#2172)) ([b479b76](b479b76))
* correct Sentinel retry ordering after node connection failures ([#2246](#2246)) ([a10e0fa](a10e0fa))
* decode integer replies up to MAX_SAFE_INTEGER exactly ([#2164](#2164)) ([28940b5](28940b5))
* decode RESP3 attribute replies instead of failing the connection ([#2213](#2213)) ([6a8cb3e](6a8cb3e)), closes [#decodeNestedType](https://github.com/redis/ioredis/issues/decodeNestedType) [#decodeMapAsObject](https://github.com/redis/ioredis/issues/decodeMapAsObject) [#decodeMapKey](https://github.com/redis/ioredis/issues/decodeMapKey)
* decode RESP3 doubles with Number ([#2150](#2150)) ([6fb5a10](6fb5a10))
* end when quit() or disconnect() is called while reconnecting ([#2245](#2245)) ([81bc16a](81bc16a))
* handle rejection from the db-restoring select on reconnectOnError ([#2229](#2229)) ([1b744ca](1b744ca))
* handle rejections from auto-resubscribe and select on reconnect ([#2187](#2187)) ([de2fbdf](de2fbdf))
* inherit connectTimeout for cluster subscribers ([#2220](#2220)) ([db57c2d](db57c2d))
* keep String.prototype in fast mode after defining VerbatimString ([#2206](#2206)) ([90e1261](90e1261))
* keep subscriber mode while other channel kinds remain subscribed ([#2196](#2196)) ([eb97beb](eb97beb))
* keep the exec error out of its own previousErrors ([#2218](#2218)) ([2c602cf](2c602cf))
* keep the selected db for commands queued while reconnecting ([#2235](#2235)) ([9f90154](9f90154))
* preserve empty keys and match patterns in scan streams ([#2199](#2199)) ([b169c3a](b169c3a))
* redact credentials from the command attached to errors ([#2231](#2231)) ([a26f8de](a26f8de)), closes [#1713](#1713)
* reject unfulfilled commands dropped on reconnect ([#2194](#2194)) ([d95d05a](d95d05a)), closes [#2193](#2193)
* reset retry attempts when connect() is called after giving up ([#2204](#2204)) ([b597303](b597303)), closes [#1463](#1463)
* reset the manual close state when connect() is called after end ([#2250](#2250)) ([7713c4b](7713c4b))
* resolve argument-less (p)unsubscribe once its own kind is empty ([#2238](#2238)) ([280812b](280812b))
* restore the db selected by a non-lowercase SELECT after reconnecting ([#2248](#2248)) ([400b774](400b774))
* **sentinel:** handle errors on failover detector connections ([#2257](#2257)) ([09bcda5](09bcda5))
* **sentinel:** retry unreachable sentinels in a loop instead of recursing ([#2270](#2270)) ([6bee8d2](6bee8d2))
* **types:** accept documented TLS profile options ([#2195](#2195)) ([56a7fb8](56a7fb8)), closes [#1626](#1626)
* **types:** add event overloads to Cluster interface ([#2201](#2201)) ([3996eaf](3996eaf)), closes [#2191](#2191)
* **types:** add smessage and smessageBuffer event overloads ([#2191](#2191)) ([ee48f4c](ee48f4c)), closes [#2190](#2190)
* **types:** allow number for zrange stop argument ([#2163](#2163)) ([932f4c2](932f4c2))

### Features

* add BITRANGEINTARRAY command ([#2279](#2279)) ([b141399](b141399))
* add set member expiration commands ([#2261](#2261)) ([5efba6b](5efba6b)), closes [redis/redis#15938](redis/redis#15938)
* bless commands ([#2209](#2209)) ([9181ee0](9181ee0))
* **cluster:** allow configuring node role for Pub/Sub subscriber ([#2176](#2176)) ([6b9130c](6b9130c))
* sharded subscriber health check ([#2268](#2268)) ([1b25ffb](1b25ffb))
* smart client handoffs ([#2165](#2165)) ([d8ae3cb](d8ae3cb))
* sorted set move commands ([#2288](#2288)) ([2f4f09d](2f4f09d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants