We were using StackExchange.Redis, but the connection kept dropping unexpectedly, making it impossible to reconnect without restarting the application. After checking GitHub Issues, we found that many others had encountered this same problem.


abortConnect is true by default

This is the biggest pitfall. Once the connection drops, it won't automatically reconnect. All requests will immediately fail with No connection is available to service this operation, and this error will persist until you restart the app.

file

Simply add abortConnect=false to your connection string:

localhost:6379,abortConnect=false

Disconnection Cannot Be Re-established

Even with abortConnect=false, there is still a possibility that connections will drop and fail to reconnect under high load. In Issue #168, someone mentioned resolving this by writing a wrapper.

file

Microsoft's official recommendation is to implement a custom ForceReconnect method that manually recreates the ConnectionMultiplexer when a RedisConnectionException is detected.

15-Minute Delay on Reconnection for Linux

Issue #1848: After a Redis node restarts on Linux, clients must wait 15 minutes before reconnecting. The cause is that the default value of the kernel parameter tcp_retries2 is too large, causing the TCP layer to continuously retry without giving up.

file

Simply reduce the value:

sysctl -w net.ipv4.tcp_retries2=5

You can't change it inside Docker; you need to modify the host machine.

Reuse ConnectionMultiplexer

ConnectionMultiplexer is designed as a singleton. You shouldn't call Connect() for every request, or the number of connections will explode. Just register it as a singleton:

services.AddSingleton<IConnectionMultiplexer>(
    ConnectionMultiplexer.Connect("localhost:6379,abortConnect=false"));
This content is automatically translated to English. View Original