diff --git a/src/StackExchange.Redis/PhysicalBridge.cs b/src/StackExchange.Redis/PhysicalBridge.cs index 21047efa2..b92576f03 100644 --- a/src/StackExchange.Redis/PhysicalBridge.cs +++ b/src/StackExchange.Redis/PhysicalBridge.cs @@ -730,13 +730,39 @@ internal bool TryEnqueue(List messages, bool isReplica) if (isDisposed) throw new ObjectDisposedException(Name); - if (!IsConnected) + if (!IsConnected || NeedsReconnect) { + // During reconnection (e.g. MOVED-to-same-endpoint), queue messages to the backlog + // so they can be sent once the connection is re-established. + // This matches the behavior of TryWriteAsync/TryWriteSync for individual commands. + if (Multiplexer.RawConfig.BacklogPolicy.QueueWhileDisconnected) + { + foreach (var message in messages) + { + message.SetEnqueued(null); + BacklogEnqueue(message); + } + StartBacklogProcessor(); + return true; + } return false; } var physical = this.physical; - if (physical == null) return false; + if (physical == null) + { + if (Multiplexer.RawConfig.BacklogPolicy.QueueWhileDisconnected) + { + foreach (var message in messages) + { + message.SetEnqueued(null); + BacklogEnqueue(message); + } + StartBacklogProcessor(); + return true; + } + return false; + } foreach (var message in messages) { // deliberately not taking a single lock here; we don't care if diff --git a/src/StackExchange.Redis/RedisBatch.cs b/src/StackExchange.Redis/RedisBatch.cs index beda224af..9863bc018 100644 --- a/src/StackExchange.Redis/RedisBatch.cs +++ b/src/StackExchange.Redis/RedisBatch.cs @@ -25,6 +25,13 @@ public void Execute() foreach (var message in snapshot) { var server = multiplexer.SelectServer(message); + // If no server found and we're allowed to queue while disconnected (e.g. during + // MOVED-triggered reconnection), retry with allowDisconnected to find the server + // so we can queue batch messages to its backlog. + if (server == null && multiplexer.RawConfig.BacklogPolicy.QueueWhileDisconnected) + { + server = multiplexer.ServerSelectionStrategy.Select(message, allowDisconnected: true); + } if (server == null) { FailNoServer(multiplexer, snapshot);