I use heroku, redis and nodejs with express. After I setup the redis on my node app, I was able to cache data. However, in the Heroku log console, i get continously logs in the following format:
2024-04-21T18:54:09.000000+00:00 app[heroku-redis]: source=REDIS addon=redis-octagonal-55498 sample#active-connections=3 sample#load-avg-1m=3.63 sample#load-avg-5m=3.3 sample#load-avg-15m=3.96 sample#read-iops=0 sample#write-iops=0 sample#memory-total=16070704kB sample#memory-free=8981520kB sample#memory-cached=3846880kB sample#memory-redis=553608bytes sample#hit-rate=0.86207 sample#evicted-keys=0
It doesn’t look like an infinite loop, it is logging these info from 1 minute to another.
But between, I get the following:
2024-04-21T18:55:20.062782+00:00 app[web.1]: Error connecting to Redis: SocketClosedUnexpectedlyError: Socket closed unexpectedly
2024-04-21T18:55:20.062819+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/@redis/client/dist/lib/client/socket.js:194:118)
2024-04-21T18:55:20.062820+00:00 app[web.1]: at Object.onceWrapper (node:events:633:26)
2024-04-21T18:55:20.062820+00:00 app[web.1]: at TLSSocket.emit (node:events:530:35)
2024-04-21T18:55:20.062821+00:00 app[web.1]: at node:net:337:12
2024-04-21T18:55:20.062821+00:00 app[web.1]: at TCP.done (node:_tls_wrap:657:7)
2024-04-21T18:55:20.071389+00:00 app[web.1]: Connected to Redis
The error allows redis to reconnect. I am able to continue using my cache.
I need to mention that this is my first time I use redis, especially in production. The initiation of the redis library is created straight into my express project app.js
file.
import redis from 'redis';
const REDIS_URL = process.env.REDIS_TLS_URL || process.env.REDIS_URL;
export const redisClient = redis.createClient({
url: REDIS_URL,
socket: {
tls: true,
rejectUnauthorized: false
}
});
redisClient.connect();
redisClient.on('connect', () => console.log('Connected to Redis'));
redisClient.on('error', error => console.error('Error connecting to Redis:', error));
Regarding the use , I use the redisClient in controllers.
For example
const cachedTargetMuscles = await redisClient.get('targetMuscles');
Can you please help me find what is wrong?
Many thanks, Daniel