How does a client library handle automatic reconnection if the connection to Redis is unexpectedly lost?
Short answer
Most well designed client libraries automatically detect a dropped connection and attempt to reconnect, often using a retry strategy with increasing delays between attempts, letting your application recover gracefully from temporary network issues or a Redis server restart…
Advertisement
AModel answer
Most well designed client libraries automatically detect a dropped connection and attempt to reconnect, often using a retry strategy with increasing delays between attempts, letting your application recover gracefully from temporary network issues or a Redis server restart without requiring manual intervention in your application code.
// Configuring retry behavior in ioredis
const redis = new Redis({
retryStrategy: (times) => Math.min(times * 50, 2000)
});Real-world example
An application configured with an automatic reconnection strategy recovers seamlessly after a brief network blip, reconnecting to Redis within a couple of seconds without the application ever throwing an unhandled error to its users.
Likely follow-ups
- What happens to commands that were in progress when the connection dropped?
- How do you configure a maximum number of reconnection attempts?
Related topics