Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU consumption #41

Closed
peteruithoven opened this issue Nov 7, 2014 · 6 comments
Closed

CPU consumption #41

peteruithoven opened this issue Nov 7, 2014 · 6 comments

Comments

@peteruithoven
Copy link

I'm developing a project where we have multiple Node.js instances and we use socket.io-redis to broadcast to all clients of all instances. This works but it seems to consume lot's of CPU.
I've done a couple of tests:

  • When I start 4 instances and I broadcast messages in all instances the cpu of all go to 100%.
  • When I start 4 instances and I broadcast messages in 1 instance the cpu of that instance is 1 to 3% and all the other instances use 100%.
  • When I don't use socket.io-redis, start 4 instances and broadcast messages in 1 instance the cpu of that instance is 1 to 3% and all the others are 0%.

I'm using a "stresstest" where I create 100 namespaces with each 2 clients and where one client broadcasts something to the other client, every second (with {state: 'mystate'} as data).
(I'm using PM2 to start multiple instances and it's monitor functionality to monitor the cpu)
I've used the debug module to see what's going on on the other instances when I use socket.io-redis and I see it's basicly lot's of socket.io-redis ignore different namespace.

I think socket.io-redis isn't optimized for "lot's" of namespaces?

@peteruithoven
Copy link
Author

I switched to using my own redis-pubsubber.
I use it by creating a channel per namespace.
Now the cpu's don't go above to 5% in the above tests.

@RWOverdijk
Copy link

👍

@mguihal
Copy link

mguihal commented Nov 25, 2014

Hello,

I have the same issue in a project in which many namespaces are used with multiple Node.js instances.

@peteruithoven Could you explain how did you switch from socket.io-redis to your redis-pubsubber package in a multi-instances Socket.io app ?

@peteruithoven
Copy link
Author

This is basicly how I do it:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redisPubSub = require('redis-pubsubber')('myproject',6379,'localhost');

redisPubSub.on('error',function(err) {
  console.log("myproject namespaces redis error: ",err);
});

createNSP("mynamespace");

function createNSP(nspName) {
  var nsp = io.of("/"+nspName);
  // create pub/sub channel for this namespace
  var eventsChannel = redisPubSub.createChannel(nspName);

  nsp.on('connection', function(socket){
    // receive pub/sub events
    function onEventsMessage() {
      // emit incomming pub/sub broadcast to this socket
      socket.emit.apply(socket,arguments);
      // do something 
    }
    // make all new socket connections listen to pub/sub messages
    eventsChannel.on("message", onEventsMessage);

    socket.on("eventToBroadcast",function() {
      // broadcast event using pub/sub 
      // using apply to forward all arguments
      eventsChannel.publish.apply(eventsChannel,arguments);
    });

    socket.on('disconnect', function(){
      eventsChannel.removeListener("message",onEventsMessage);
    });
  }
}

To forward all incomming messages I also use the following trick:
socketio/socket.io#1715 (comment)

But, as you can see, this is not at all a drop in replacement.
It's probably better to improve socket.io-redis on the long run, by for example checking if we can make the "channels" namespace specific.
Actually... checking the source, you might be able to do this with the key option:

key: the name of the key to pub/sub events on as prefix (socket.io)

But I'm not sure if you can do this per namespace (instead of socket.io globally)

@koszta
Copy link

koszta commented Dec 10, 2014

The same goes for the rooms, even if there is no client joined to them the socket.io server will still receive all the messages for that room from redis.

It is because socket.io-redis subscribes for all messages with the prefix (socket.io by default). With the current design it is needed, because messages are sent to channels prefix + '#' + clientId, no namespace or room in the channel name.

So messages from different namespaces will be received from redis.
The debug message says exactly this, socket.io-redis ignore different namespace, so it gets discarded on the socket.io server, instead of simply not subscribing to them on redis.

sub.psubscribe(prefix + '#*', function(err){
  if (err) self.emit('error', err);
});

You can test it with this repo.
https://github.com/koszta/socket.io-perf

In pull request #46 I changed the channel names to prefix + '#' + namespance + '#' [+ room + '#'], with this socket.io servers can subscribe to the necessary channels only.

@objectiveSee
Copy link
Contributor

This issue has been resolved in version 1.0.0, which includes #46 as mentioned in the comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants