How to debug Node + Socket.io CPU Issues

6,822

I've got exactly the same problem, sad that noone has any suggestions for us...

here is my problem explained node.js, mongodb, redis, on ubuntu performance degradation in production, RAM is free, CPU 100%

Share:
6,822

Related videos on Youtube

user14873402
Author by

user14873402

Newcastle University Information Systems w/ Business Graduate. Learning Android Development. Web Developer at Jaywing, Sheffield SOreadytohelp

Updated on September 18, 2022

Comments

  • user14873402
    user14873402 over 1 year

    We are running a Node Socket.io server with Express 3. The server is monitored using Forever. The service is running well, but the CPU grows throughout the day, until it reaches 90%+ and then suddenly drops back down to ~20%, as shown in the graphs below. I believe that the drop is caused by Forever restarting the app.

    CPU Behaviour

    What I would like to know is;

    • What are the typical factors that could cause a Node.js app to behave like this?
    • What tools / methods are available to debug memory leaks / cpu hogging in node apps?

    I think it could be something to do with Socket.io not cleaning up resources once users have disconnected, although the docs say that Socket.io will manage this automatically.

    Any help would be greatly appreciated, this issue is making managing our server very difficult. Please let me know if this question would be better suited on StackOverflow.

    Update: After more research, it appears that the CPU does not directly correlate with the number of connections. Our critical mass seems to be around ~1500 concurrent connections split up like so:

    • xhr-polling: 767
    • websocket: 692
    • jsonppolling: 80

    Sometimes we could be at 100% CPU with only 500 connections, other times its 1500 connections. I'm aware that the rate of messages sent has a big affect, however the rate is fairly consistent.

    • toblerpwn
      toblerpwn almost 11 years
      did you ever solve this problem, Steve? i'm in the same boat.
    • user14873402
      user14873402 almost 11 years
      Unfortunately not completely. I've made lots of tweaks to my code to optimise it, e.g. not storing large objects, getting rid of bloated code etc. Been able to get my connections a lot higher, but the CPU graph still does very weird things every now and then. I've also been upgrading Socket.io and Node to the latest versions, seems to help.
    • toblerpwn
      toblerpwn almost 11 years
      right on, good to know about the versions (THANKS!). did you check out the answer below? it looks like @user172220 solved this problem by refactoring how socket.io and redis work together, but the method to his answer is not entirely clear for a relative JS novice like me.