After reading many stack overflow erros im still unable to solve this issue. I have setup a redis server to cache the data from my node js server , it works fine as long as the redis server is running but as soon as i close the redis server the node server crashes. i want the server to keep working even if the redis server is down
heres my code
import Product from '../models/productModel.js'
import User from '../models/userModel.js'
import slugify from 'slugify'
import redis from 'redis'
import mongoose from 'mongoose'
//connection to redis
const redisClient = redis.createClient(6379) //cause for error when redis server is down
export const listAll = async (req, res) => {
try {
const products = await Product.find({})
.limit(parseInt(req.params.count))
.populate('category')
.populate('subs')
.sort([['createdAt', 'desc']])
.exec()
if (products !== null) {
redisClient.setex(req.params.count, 60, JSON.stringify(products))
}
res.json(products)
} catch (err) {
console.log(err)
}
}
here im getting an ECONNREFUSED
error when im stopping the redis server. Is there a way for my node server to keep going even when the redis server is stopped because redis servver is just a supporting mechanism and if cache data is unavailable ill anyway get data from db
is there a way to check if redisClient
is undefined or null that i could set to check if the redis server is down and then later i can use it to make the request appropriately
2
Answers
According to the doc of node-redis Error Handling, have you try this ?
First of all, you could listen to the
error
events inredisClient
per redis nodeAlso some other events we used to listen as below
On the other hand, my server listens to the ‘unhandledRejection’ event to handle the unhandled rejection to prevent server crashes.