node-redis: Socket closed unexpectedly
Hi mates,
Redis often throw error like above Socket closed unexpectedly, could we turn socket connection off from redis?
Thanks,
Thu
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 42 (1 by maintainers)
I solved the problem with pingInterval:
const redisClient = createClient({ url: process.env.REDIS_URL, pingInterval: 10000, });
@leibale i have a same problem, i was update node-redis to 4.1.0
`var redis = require(“redis”); const fs = require(‘fs’);
const redisClient = redis.createClient({ url:
rediss://api3-redis-master.api3-pro.svc.cluster.local:6379, socket: { tls: true, rejectUnauthorized: false, ca: fs.readFileSync(‘ks8-ca-cert.crt’, encoding=‘ascii’), servername: “api3-redis-master.api3-pro.svc.cluster.local” }, legacyMode: true });redisClient.on(‘error’, function(err){ console.log(‘Redis Client Error’, err); }); redisClient.on(‘connect’, () => { console.log(
Redis connected); });(async () => { // Connect to redis server await redisClient.connect(); redisClient.get(‘test_tls’, function(err, result){ console.log(err, result); }); })();`
After 30 minutes when starting nodejs start, redis gives error and reconnects itself
SocketClosedUnexpectedlyError: Socket closed unexpectedly at TLSSocket.<anonymous> (/data/www/public_html/cloud_api3/node_modules/@redis/client/dist/lib/client/socket.js:156:118) at Object.onceWrapper (node:events:646:26) at TLSSocket.emit (node:events:538:35) at node:net:687:12 at TCP.done (node:_tls_wrap:580:7)
You should check for client timeout in redis server config => https://redis.io/docs/reference/clients/#client-timeouts
@diegothucao 4.1.0 will be released in a few days
Great chatting with you @leibale and thanks for your help. Below is a summary of the issue and how to fix it.
The issue only happens when subscribing to a channel that receives a lot of data. In my case, this was 100 MB every second.
When this happens, redis closes the underlying connection with the following error:
redis replication scheduled to be closed ASAP for overcoming of output buffer limits.In node-redis version 3, the connection is closed but no errors are logged. There is an auto-reconnect due to which the issue is never noticed.
In node-redis version 4, the connection is closed and errors logged.
The fix is as simple as increasing the client-output-buffer-limit on redis. I’ve set it to unlimited as values up to 512 MB didn’t work for me.
redis-cli -p 6379 config set client-output-buffer-limit "pubsub 0 0 60"You might need to increase the maxmemory too, I did not need this.
redis-cli -p 6379 config set maxmemory "1gb"Persist the configuration to file if needed.
redis-cli -p 6379 config rewriteI have a multi-server, multi-process application and I’m using redis all over the place. The only instances of redis clients that throw this error are where I’m connecting to the same redis server while using worker threads ie. Connection exists in the main thread and worker thread and the error is constantly thrown every minute. Not an issue when using version 3.