The backend of my application is hosted with Elastic Beanstalk, everything works great. I recently decided to configure a cache by creating an ElastiCache (Redis) cluster and I have a big problem around a timeout error. Basically, with each request I make to the backend that goes through the cache, it hangs in the void with no response from the backend, until a timeout error occurs after about thirty seconds.
Here is a simple example of a route that crashes with this timeout:
app.get('/test-redis', async (req, res) => {
try {
await redisClient.set('test', 'value');
const value = await redisClient.get('test');
res.json({ value });
} catch (err) {
console.error('Error interacting with Redis:', err);
res.status(500).send('Error interacting with Redis');
}
});
and here is the Redis configuration for elasticache:
require('dotenv').config();
const Redis = require('ioredis');
const redisClusterEndpoints = [
{ host: '<cluster node 1>', port: 6379 },
{ host: '<cluster node 2>', port: 6379 },
{ host: '<cluster node 3>', port: 6379 },
];
const client = new Redis.Cluster(redisClusterEndpoints, {
tls: {
rejectUnauthorized: true,
},
redisOptions: {
connectTimeout: 10000,
},
retryDelayOnFailover: 1000,
retryDelayOnClusterDown: 1000,
maxRetriesPerRequest: 5,
scaleReads: 'slave',
});
client.on('error', (error) => {
console.error('Redis error:', error);
});
client.on('end', () => {
console.log('Redis connection closed');
});
client.on('reconnecting', (delay) => {
console.log(`Redis reconnecting in ${delay} ms`);
});
module.exports = client;
I have already checked a lot of time my AWS configurations (VPC, Security groups, NAT gateways, subnets…) and it seems good.
Has anyone ever had this kind of problem? Does anyone know how to resolve it?
Oksallz Snzdiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.