Skip to content

Commit 2fd5297

Browse files
MagicalLas하원호
andauthored
Adjusting disconnectedBehavior Option to Prevent Timeout During Redis Shutdown (#2894)
We often need to shut down Redis for maintenance, such as version upgrades. During these times, requests do not fail immediately but instead experience timeouts, increasing application latency. This issue can be resolved by adjusting some options. Currently, our Redis client options are configured as follows: ```java ClientOptions options = ClientOptions.builder() .autoReconnect(true) .disconnectedBehavior(DisconnectedBehavior.REJECT_COMMANDS) ``` The DisconnectedBehavior.REJECT_COMMANDS option appears to cancel commands when the connection is lost. However, if autoReconnect is not set to false, commands in the CommandHandler.stack are not canceled but are placed into the disconnectedBuffer. Therefore, ongoing commands are not rejected if autoReconnect is true, even with the client option modified. For services heavily relying on Redis, latency is crucial. Additionally, we want to avoid writing custom code for reconnections by using the auto-reconnect feature. Adjusting the autoReconnect option can solve this issue immediately, but it would require significant changes to implement automatic reconnection. Proposal We propose that the condition for canceling commands in the CommandHandler.stack should be based solely on rejectCommandsWhileDisconnected and not combined with autoReconnect. I've submitted a PR with a simple code change. Please let me know your thoughts. === Additionally our applications, to maintain low latency and avoid queuing many requests in the buffer when the connection is down, we have configured the client to execute commands only when the connection is active. As a result, only a few requests in the CommandHandler.stack encounter timeouts, but this still negatively impacts the application. Co-authored-by: 하원호 <[email protected]>
1 parent c648fff commit 2fd5297

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/main/java/io/lettuce/core/protocol/DefaultEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ public void notifyDrainQueuedCommands(HasQueuedCommands queuedCommands) {
689689
cancelCommands("Connection closed", queuedCommands.drainQueue(), it -> it.completeExceptionally(lazy.get()));
690690
cancelCommands("Connection closed", drainCommands(), it -> it.completeExceptionally(lazy.get()));
691691
return;
692-
} else if (reliability == Reliability.AT_MOST_ONCE && rejectCommandsWhileDisconnected) {
692+
} else if (rejectCommandsWhileDisconnected) {
693693

694694
Lazy<RedisException> lazy = Lazy.of(() -> new RedisException("Connection disconnected"));
695695
cancelCommands("Connection disconnected", queuedCommands.drainQueue(), it -> it.completeExceptionally(lazy.get()));

0 commit comments

Comments
 (0)