I have a webapp deployed in azure, and I´ve also configured a azure redis cache, but I am not being able to make my cache work. My nextjs frontend app is hosted in vercel.
This is my redis client in my nodejs app:
const redis = require("redis");
const dotenv = require('dotenv');
dotenv.config({ path: './.env' });
// Variables de entorno para la caché
const cacheHostName = process.env.AZURE_CACHE_FOR_REDIS_HOST_NAME;
const cachePassword = process.env.AZURE_CACHE_FOR_REDIS_ACCESS_KEY;
if (!cacheHostName) throw new Error("AZURE_CACHE_FOR_REDIS_HOST_NAME is empty");
if (!cachePassword) throw new Error("AZURE_CACHE_FOR_REDIS_ACCESS_KEY is empty");
exports.connect = async () => {
let client;
if (process.env.NODE_ENV === 'production') {
client = redis.createClient({
// Configuración de Redis para TLS en producción
url: `redis://${cachePassword}@${cacheHostName}:6380`,
tls: {
rejectUnauthorized: true // Asegúrate de que este valor sea verdadero para requerir SSL
}
});
} else {
// Configuración local de Redis para pruebas
client = redis.createClient({
host: 'localhost',
port: 6379
});
}
client.on('connect', () => {
console.log('Cliente Redis conectado al servidor');
});
client.on('error', (err) => {
console.error('El cliente Redis no pudo conectarse al servidor:', err);
});
client.on('end', () => {
console.log('La conexión del cliente Redis se cerró');
});
await client.connect();
return client;
};
My SSL port in the redis service is disabled and my SSL port is 6380, and the TLS version is 1.2
Everytime I make a request through postman or through my hosted app, the request stays in an infinite Sending request...
.
I have also tried with rejectUnauthorized: false
and no changes were shown.
It seems that my redis client is connecting and failing connection in an inifinite loop:
Cliente Redis connected to the server
The Redis Client was not able to connect to the server: Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:218:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
El cliente Redis no pudo conectarse al servidor: Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:218:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
Cliente Redis connected to the server
The Redis Client was not able to connect to the server: Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:218:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
The Redis Client was not able to connect to the server: Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:218:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
Cliente Redis connected to the server
This is how I am handling redis in my controller method.
const { User } = require('../models');
const asyncHandler = require('../middlewares/async');
const { connect } = require('../redis/redis')
//@route GET api/users/
//@desc Get all users
//@access Private
exports.getUsers = asyncHandler(async (req, res, next) => {
const redis = await connect();
const key = 'users';
const redisUsers = await redis.get(key);
if( redisUsers ) {
console.log('Users From Redis')
return res.status(200).json({
success: true,
data: JSON.parse(redisUsers)
})
}
console.log('Users From DB')
const users = await User.findAll();
await redis.set(key, JSON.stringify(users));
return res.status(200).json({
success: true,
data: users
})
});
In my local environment if I connect to redis to port 6379 and to host localhost
it works fine, but as soon as I try connecting to the service of redis in azure it fails.
In the network tab of the browser when I make a request going to a view that displays a list of users I get the request for long time with no status code and finally I get a CORS error but CORS is enabled from azure. Finally the response from my frontend is of type text/x-component
instead of json.
Note: this error only occurs in requests that include redis in my server