Unhandled error event: Error: read ECONNRESET at TCP.onStreamRead
Problem
Hi, I am getting below error sometimes. Redis connection gets disconnected sometime. Out of 10 I am facing this issue 2 time. It takes time to reconnect the server and api response time increases from 50-80 msec to 1-2 mins. Error : Unhandled error event: Error: read ECONNRESET at TCP.onStreamRead Ioredis client configuration as below: var redisMasterClient = new IORedis({ host: host , connectTimeout: 1000, password: "password, keepAlive : 1000, retryStrategy: function(times) { var delay = Math.min(times * 10, 2000); return delay; }, maxRetriesPerRequest: 1 }); Please help. This is urgent issue on production.
Error Output
error sometimes. Redis connection gets disconnected sometime. Out of 10 I am facing this issue 2 time. It takes time to reconnect the server and api response time increases from 50-80 msec to 1-2 mins.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Enhanced Redis Connection Handling
The ECONNRESET error indicates that the connection to the Redis server was unexpectedly closed. This can occur due to network issues, server overload, or misconfiguration. The current retry strategy and connection settings may not be sufficient to handle transient network issues effectively, leading to increased response times during reconnection attempts.
Awaiting Verification
Be the first to verify this fix
- 1
Increase Connection Timeout
Increase the connectTimeout value to allow more time for establishing a connection, especially under high load.
typescriptconnectTimeout: 5000, - 2
Adjust Retry Strategy
Modify the retryStrategy to include exponential backoff and a maximum number of retries to handle transient errors more gracefully.
typescriptretryStrategy: function(times) { return Math.min(times * 100, 5000); }, - 3
Enable Reconnection on Error
Add an error event listener to handle ECONNRESET and other connection errors, allowing the client to attempt reconnection automatically.
typescriptredisMasterClient.on('error', (err) => { console.error('Redis Client Error', err); redisMasterClient.connect(); }); - 4
Monitor Redis Connection Health
Implement a health check mechanism to monitor the Redis connection and log any issues for further analysis.
typescriptsetInterval(() => { redisMasterClient.ping().catch(err => console.error('Ping failed', err)); }, 10000);
Validation
To confirm the fix worked, monitor the Redis connection logs and API response times over a period of time. The frequency of ECONNRESET errors should decrease, and the response times should stabilize around the original 50-80 ms range.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep