Redis caching for Mongodb running slow












0















I have the following nodejs code



const redis = require('redis');
const client = redis.createClient();

module.exports = client;


in a file and the following code in another



redis.set(user.id, JSON.stringify(user), function (error) {
if (error) {
logger.log('error', "Redis error: " + error)
}
console.log("Saved to Redis");


The intention is to read the value from redis when deserializing the user which redis does



    redis.get(id, function (error, data) {
if (error) {
logger.log('error', 'Redis error');
}
if (data) {
console.log("Redis");
let redisUser = JSON.parse(data);
done(null, redisUser);
} else {
console.log("Mongo");
userDb.findById(id)
.then(user => done(null, user))
.catch(err => logger.log('error', 'Error when deserializing the user: ' + err))
}
})


However when using Chrome waterfall timing, i find that the time taken by Redis is almost the same to that which MongoDb takes (roughly about 180 - 270 ms) yet i implemented redis as a faster store for faster retrieval. I dont know what the error could be but i suspect that it can be my other code which is for Socket.IO to save sessions to redis using the pubClient and SubClient. The code is on another file as shown below



'use strict';
const config = require('./config')
const redis = require('redis').createClient;
const adapter = require('socket.io-redis');

//Social authentication Logic
require('./auth')();

let ioServer = app => {
app.locals.chatrooms = ;
const server = require('http').Server(app);
const io = require('socket.io')(server);
/** Force socket Io to use web sockets as its default transportation medium
*Implements session affinity on the server
*/
io.set('transports', ['websocket']);
//Interface for sending or publishing data buffers
let pubClient = redis(config.redis.port, config.redis.host, {
auth_pass: config.redis.password
});

/**Interface for subscribing or getting data
*Return buffers is to tell redis to return data in its original data format,
*By default redis will return data as Strings
*/
let subClient = redis(config.redis.port, config.redis.host, {
return_buffers: true,
auth_pass: config.redis.password
});
//Specify a custom adapter to Socket.io
io.adapter(adapter({
pubClient,
subClient
}));
io.use((socket, next) => {
require('./session')(socket.request, {}, next);
})
require('./socket')(io, app);
return server;
}



module.exports = {
router: require('./routes')(),
session: require('./session'),
ioServer,
logger:require('./logger')
}


What could be the possible reason, might i be right that this is the error, and if it is, what are the possible ways of integrating redis to socket.io and to cache. Thanks in advance.










share|improve this question





























    0















    I have the following nodejs code



    const redis = require('redis');
    const client = redis.createClient();

    module.exports = client;


    in a file and the following code in another



    redis.set(user.id, JSON.stringify(user), function (error) {
    if (error) {
    logger.log('error', "Redis error: " + error)
    }
    console.log("Saved to Redis");


    The intention is to read the value from redis when deserializing the user which redis does



        redis.get(id, function (error, data) {
    if (error) {
    logger.log('error', 'Redis error');
    }
    if (data) {
    console.log("Redis");
    let redisUser = JSON.parse(data);
    done(null, redisUser);
    } else {
    console.log("Mongo");
    userDb.findById(id)
    .then(user => done(null, user))
    .catch(err => logger.log('error', 'Error when deserializing the user: ' + err))
    }
    })


    However when using Chrome waterfall timing, i find that the time taken by Redis is almost the same to that which MongoDb takes (roughly about 180 - 270 ms) yet i implemented redis as a faster store for faster retrieval. I dont know what the error could be but i suspect that it can be my other code which is for Socket.IO to save sessions to redis using the pubClient and SubClient. The code is on another file as shown below



    'use strict';
    const config = require('./config')
    const redis = require('redis').createClient;
    const adapter = require('socket.io-redis');

    //Social authentication Logic
    require('./auth')();

    let ioServer = app => {
    app.locals.chatrooms = ;
    const server = require('http').Server(app);
    const io = require('socket.io')(server);
    /** Force socket Io to use web sockets as its default transportation medium
    *Implements session affinity on the server
    */
    io.set('transports', ['websocket']);
    //Interface for sending or publishing data buffers
    let pubClient = redis(config.redis.port, config.redis.host, {
    auth_pass: config.redis.password
    });

    /**Interface for subscribing or getting data
    *Return buffers is to tell redis to return data in its original data format,
    *By default redis will return data as Strings
    */
    let subClient = redis(config.redis.port, config.redis.host, {
    return_buffers: true,
    auth_pass: config.redis.password
    });
    //Specify a custom adapter to Socket.io
    io.adapter(adapter({
    pubClient,
    subClient
    }));
    io.use((socket, next) => {
    require('./session')(socket.request, {}, next);
    })
    require('./socket')(io, app);
    return server;
    }



    module.exports = {
    router: require('./routes')(),
    session: require('./session'),
    ioServer,
    logger:require('./logger')
    }


    What could be the possible reason, might i be right that this is the error, and if it is, what are the possible ways of integrating redis to socket.io and to cache. Thanks in advance.










    share|improve this question



























      0












      0








      0








      I have the following nodejs code



      const redis = require('redis');
      const client = redis.createClient();

      module.exports = client;


      in a file and the following code in another



      redis.set(user.id, JSON.stringify(user), function (error) {
      if (error) {
      logger.log('error', "Redis error: " + error)
      }
      console.log("Saved to Redis");


      The intention is to read the value from redis when deserializing the user which redis does



          redis.get(id, function (error, data) {
      if (error) {
      logger.log('error', 'Redis error');
      }
      if (data) {
      console.log("Redis");
      let redisUser = JSON.parse(data);
      done(null, redisUser);
      } else {
      console.log("Mongo");
      userDb.findById(id)
      .then(user => done(null, user))
      .catch(err => logger.log('error', 'Error when deserializing the user: ' + err))
      }
      })


      However when using Chrome waterfall timing, i find that the time taken by Redis is almost the same to that which MongoDb takes (roughly about 180 - 270 ms) yet i implemented redis as a faster store for faster retrieval. I dont know what the error could be but i suspect that it can be my other code which is for Socket.IO to save sessions to redis using the pubClient and SubClient. The code is on another file as shown below



      'use strict';
      const config = require('./config')
      const redis = require('redis').createClient;
      const adapter = require('socket.io-redis');

      //Social authentication Logic
      require('./auth')();

      let ioServer = app => {
      app.locals.chatrooms = ;
      const server = require('http').Server(app);
      const io = require('socket.io')(server);
      /** Force socket Io to use web sockets as its default transportation medium
      *Implements session affinity on the server
      */
      io.set('transports', ['websocket']);
      //Interface for sending or publishing data buffers
      let pubClient = redis(config.redis.port, config.redis.host, {
      auth_pass: config.redis.password
      });

      /**Interface for subscribing or getting data
      *Return buffers is to tell redis to return data in its original data format,
      *By default redis will return data as Strings
      */
      let subClient = redis(config.redis.port, config.redis.host, {
      return_buffers: true,
      auth_pass: config.redis.password
      });
      //Specify a custom adapter to Socket.io
      io.adapter(adapter({
      pubClient,
      subClient
      }));
      io.use((socket, next) => {
      require('./session')(socket.request, {}, next);
      })
      require('./socket')(io, app);
      return server;
      }



      module.exports = {
      router: require('./routes')(),
      session: require('./session'),
      ioServer,
      logger:require('./logger')
      }


      What could be the possible reason, might i be right that this is the error, and if it is, what are the possible ways of integrating redis to socket.io and to cache. Thanks in advance.










      share|improve this question
















      I have the following nodejs code



      const redis = require('redis');
      const client = redis.createClient();

      module.exports = client;


      in a file and the following code in another



      redis.set(user.id, JSON.stringify(user), function (error) {
      if (error) {
      logger.log('error', "Redis error: " + error)
      }
      console.log("Saved to Redis");


      The intention is to read the value from redis when deserializing the user which redis does



          redis.get(id, function (error, data) {
      if (error) {
      logger.log('error', 'Redis error');
      }
      if (data) {
      console.log("Redis");
      let redisUser = JSON.parse(data);
      done(null, redisUser);
      } else {
      console.log("Mongo");
      userDb.findById(id)
      .then(user => done(null, user))
      .catch(err => logger.log('error', 'Error when deserializing the user: ' + err))
      }
      })


      However when using Chrome waterfall timing, i find that the time taken by Redis is almost the same to that which MongoDb takes (roughly about 180 - 270 ms) yet i implemented redis as a faster store for faster retrieval. I dont know what the error could be but i suspect that it can be my other code which is for Socket.IO to save sessions to redis using the pubClient and SubClient. The code is on another file as shown below



      'use strict';
      const config = require('./config')
      const redis = require('redis').createClient;
      const adapter = require('socket.io-redis');

      //Social authentication Logic
      require('./auth')();

      let ioServer = app => {
      app.locals.chatrooms = ;
      const server = require('http').Server(app);
      const io = require('socket.io')(server);
      /** Force socket Io to use web sockets as its default transportation medium
      *Implements session affinity on the server
      */
      io.set('transports', ['websocket']);
      //Interface for sending or publishing data buffers
      let pubClient = redis(config.redis.port, config.redis.host, {
      auth_pass: config.redis.password
      });

      /**Interface for subscribing or getting data
      *Return buffers is to tell redis to return data in its original data format,
      *By default redis will return data as Strings
      */
      let subClient = redis(config.redis.port, config.redis.host, {
      return_buffers: true,
      auth_pass: config.redis.password
      });
      //Specify a custom adapter to Socket.io
      io.adapter(adapter({
      pubClient,
      subClient
      }));
      io.use((socket, next) => {
      require('./session')(socket.request, {}, next);
      })
      require('./socket')(io, app);
      return server;
      }



      module.exports = {
      router: require('./routes')(),
      session: require('./session'),
      ioServer,
      logger:require('./logger')
      }


      What could be the possible reason, might i be right that this is the error, and if it is, what are the possible ways of integrating redis to socket.io and to cache. Thanks in advance.







      javascript node.js redis






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 10:19









      Neil Lunn

      101k23178187




      101k23178187










      asked Nov 23 '18 at 10:17









      Ike MawiraIke Mawira

      3818




      3818
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444752%2fredis-caching-for-mongodb-running-slow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444752%2fredis-caching-for-mongodb-running-slow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini