Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node & Redis: Redis Clients

Assume you have multiple db's in Redis you would like to insert and/or remove data from. You have a flow like;

  • Insert data to DB #1
  • After the first insertion's callback do some stuff and insert data to DB #2
  • After the second insertion's callback do some stuff again and finally insert data to DB #3

I use one variable called redisClient that is basically created as;

redisClient = redis.createClient();

And while selecting a new DB, I use select command with the extra pre-caution callback, so my select command is like;

redisClient.select(1, function(err) {
  //Some programming logic (Insertion, deletion and stuff)
  redisClient.select(2, function(err) {
    //Do some additional programming logic (Insertion, deletion and stuff)
  }
});

However things get constantly mix. I would like to note that the redisClient variable has been assigned only once and later on used throughout the entire application. Now I was wondering, how reasonable would it be to use seperate redisClients for each DB I have in Redis. So it would be like;

redisClientForDB1 = redis.createClient();
redisClientForDB2 = redis.createClient();
redisClientForDB3 = redis.createClient();

I was wondering would it be reasonable, or would it be a right way for an application that will receive 4K requests per sec and about to go to the production mode. What issues this model might face?

like image 565
Ali Avatar asked Dec 20 '12 11:12

Ali


People also ask

What is node used for?

Node allows developers to write JavaScript code that runs directly in a computer process itself instead of in a browser. Node can, therefore, be used to write server-side applications with access to the operating system, file system, and everything else required to build fully-functional applications.

Why is it called node?

Understand Why It Is Called Node It's called Node because it is used to build simple single-process blocks called nodes. These nodes can be organized with good networking protocols for communication with each other and be scaled up to build large distributed programs.

What is an example node?

Examples of nodes include bridges, switches, hubs, and modems to other computers, printers, and servers. One of the most common forms of a node is a host computer; often referred to as an Internet node. 2. In graph theory, a node is a unit of data on a graph, connected to other nodes by edges.

What the heck is node?

Node is JavaScript, but as a server-side language. This is possible because of V8, Chromium's JavaScript engine, which can run on its own, outside the confines of the browser. Node and browser-based JavaScript can be very different, and have different capabilities, though both are JavaScript at their core.


1 Answers

Just as Carl Zulauf said, it's best to open 3 different connections (one per DB):

redisClient = {
  DB1: redis.createClient(),
  DB2: redis.createClient(),
  DB3: redis.createClient()
};

It's best to open all connections once during the initialization of your server:

async.parallel([
  DB1.select.bind(DB1, 1),
  DB2.select.bind(DB2, 2),
  DB3.select.bind(DB3, 3)
], next);

So, after you created redisClient object and initialized it you can use it to handle all your redis operations.

If you'll use redis this way node will open 3 (and only 3) connections per node process.


N.B. It's also a good idea to put it all into one node module:

module.exports = {
  DB1: redis.createClient(),
  DB2: redis.createClient(),
  DB3: redis.createClient(),
  init: function(next) {
    var select = redis.RedisClient.prototype.select;
    require('async').parallel([
      select.bind(this.DB1, 1),
      select.bind(this.DB2, 2),
      select.bind(this.DB3, 3)
    ], next);
  }
};

Then you'll be able to initialize all your redis connections by calling init function once (because node caching require calls):

require('./lib/my_redis').init(function(err) {
  if (err) throw err;
  server.listen();
});

Then when require('./lib/my_redis').DB1.set('key','val') will be called in any of your modules DB1 will be already initialized.

like image 156
Leonid Beschastny Avatar answered Oct 02 '22 03:10

Leonid Beschastny