skip to Main Content

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


  1. According to the doc of node-redis Error Handling, have you try this ?

    client.on("error", function(err) {
      assert(err instanceof Error);
      assert(err instanceof AbortError);
      assert(err instanceof AggregateError);
    
      // The set and get are aggregated in here
      assert.strictEqual(err.errors.length, 2);
      assert.strictEqual(err.code, "NR_CLOSED");
    });
    
    Login or Signup to reply.
  2. First of all, you could listen to the error events in redisClient per redis node

    You MUST listen to error events. If a client doesn’t have at least one error listener registered and an error occurs, that error will be thrown and the Node.js process will exit.

    Also some other events we used to listen as below

    const redisClient = redis.createClient(6379)
    redisClient.on('error', function() {
      return log("Redis connect error!");
    });
    
    redisClient.on('reconnecting', function() {
      return log("Redis reconnecting...");
    });
    
    redisClient.on('connect', function() {
      return log("Redis connect...");
    });
    
    redisClient.on('ready', function() {
      return log("Redis connected! Cache Service is Working...");
    });
    

    On the other hand, my server listens to the ‘unhandledRejection’ event to handle the unhandled rejection to prevent server crashes.

    The ‘unhandledRejection’ event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop

    process.on("unhandledRejection", function(reason, promise) {
      log("unhandledRejection " + promise.toString() + " stack " + JSON.stringify(reason.stack)); 
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search