to your account
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.
Please help. This is urgent issue on production.
const client = new Redis(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
Can try if it helps at your side
quanteladio, Pyxl-Matheus, andre-albuquerque, OliverwengFiltered, matija2209, merklefruit, yo444htmtmt, negreanucalinadrian, CodeDevOption, denbite, and 13 more reacted with thumbs up emoji
quanteladio, Pyxl-Matheus, andre-albuquerque, OliverwengFiltered, negreanucalinadrian, agrattan0820, ParthBhavsar, pedroaraujo1952, KoyalkarAditya, a-farid, and mu6m reacted with hooray emoji
quanteladio, Pyxl-Matheus, andre-albuquerque, OliverwengFiltered, 0xGabi, ChiranjitSaha3013115, salva-morean, agrattan0820, manuelsanchez2, ParthBhavsar, and 3 more reacted with heart emoji
quanteladio, Pyxl-Matheus, OliverwengFiltered, agrattan0820, programandoconro, pedroaraujo1952, and mu6m reacted with rocket emoji
All reactions
I resolved my issue by using the following option
const client = new Redis(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
Can try if it helps at your side
i dont understand whats the relationship with this attribute.
maybe use setInterval
send ping
to redisserver is temp solution.
finding the root cause and fixing it is the best solution
The right way to do this is to handle the error gracefully. The problem seems to be the IORedis client's behaviour of timed disconnect when inactive but it has an auto reconnect mechanism that will keep trying to reconnect.
Use Node.js Event Emitter to handle it. View all events with client.eventNames()
or see the references below. One can simply log out the error
, reconnecting
and connect
events like this to understand the behaviour described above that IORedis is doing in the background. Consequently, this also handles the irritating error message. Hope this helps!
import Redis from 'ioredis';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../.env') });
const client = new Redis({
port: Number(process.env.SESSION_STORE_PORT) || 6379,
host: process.env.SESSION_STORE_HOSTNAME,
});
// Listen to 'error' events to the Redis connection
client.on('error', error => {
if (error.code === 'ECONNRESET') {
console.log('Connection to Redis Session Store timed out.');
} else if (error.code === 'ECONNREFUSED') {
console.log('Connection to Redis Session Store refused!');
} else console.log(error);
});
// Listen to 'reconnecting' event to Redis
client.on('reconnecting', err => {
if (client.status === 'reconnecting')
console.log('Reconnecting to Redis Session Store...');
else console.log('Error reconnecting to Redis Session Store.');
});
// Listen to the 'connect' event to Redis
client.on('connect', err => {
if (!err) console.log('Connected to Redis Session Store!');
});
export default client;
References:
IORedis - Connection Events
IORedis - Options available for new Redis([port], [host], options)
Unhandled error event: Error: write EPIPE, Unhandled error event: Error: write ECONNRESET
#1638
Hello, you don't instance radis, when your application starts. you'll instance when you use the radis.
Exemple here:
try {
await redis.set("test", "test")
} catch (err) {
console.log("error", err)
} finally {
redis.quit() // stop connection here, and init other when you need
Hello, you don't instance radis, when your application starts. you'll instance when you use the radis.
Exemple here:
try {
await redis.set("test", "test")
} catch (err) {
console.log("error", err)
} finally {
redis.quit() // stop connection here, and init other when you need
This is okay only when your application is having low throughput into redis. For a medium to higher throughput application, why would you want to open and close connection for every R/W operation and increase your latency per operation? In most cases, you want to keep it open and gracefully handle the error then, unless there's a security reason why you should not or if it's infrequent use.
bjekovicn, 26A0D355-AA5F-4621-8975-9184E8DF8C23, duongtn27, phillippelevidad, PuffMeow, Vittorio-QA, and TheUniqueMathemagician reacted with thumbs up emoji
frhn-xo, fadilmalik-salam, PuffMeow, and TheUniqueMathemagician reacted with confused emoji
All reactions
The right way to do this is to handle the error gracefully. The problem seems to be the IORedis client's behaviour of timed disconnect when inactive but it has an auto reconnect mechanism that will keep trying to reconnect.
Use Node.js Event Emitter to handle it. View all events with client.eventNames()
or see the references below. One can simply log out the error
, reconnecting
and connect
events like this to understand the behaviour described above that IORedis is doing in the background. Consequently, this also handles the irritating error message. Hope this helps!
import Redis from 'ioredis';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../.env') });
const client = new Redis({
port: Number(process.env.SESSION_STORE_PORT) || 6379,
host: process.env.SESSION_STORE_HOSTNAME,
});
// Listen to 'error' events to the Redis connection
client.on('error', error => {
if (error.code === 'ECONNRESET') {
console.log('Connection to Redis Session Store timed out.');
} else if (error.code === 'ECONNREFUSED') {
console.log('Connection to Redis Session Store refused!');
} else console.log(error);
});
// Listen to 'reconnecting' event to Redis
client.on('reconnecting', err => {
if (client.status === 'reconnecting')
console.log('Reconnecting to Redis Session Store...');
else console.log('Error reconnecting to Redis Session Store.');
});
// Listen to the 'connect' event to Redis
client.on('connect', err => {
if (!err) console.log('Connected to Redis Session Store!');
});
export default client;
References:
IORedis - Connection Events
IORedis - Options available for new Redis([port], [host], options)
@babeingineer try this
In my case : it is not working : in development mode (http)
so using this helped,
const client = new Redis(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
but in production : remove this