Node.js CPU load balancing

后端 未结 3 1774
时光说笑
时光说笑 2021-02-10 03:09

I created test with JMeter to test performance of Ghost blogging platform. Ghost written in Node.js and was installed in cloud server with 1Gb RAM, 1 CPU.

I noticed aft

3条回答
  •  庸人自扰
    2021-02-10 04:02

    Node.js is famously single-threaded (see this answer): a single node process will only use one core (see this answer for a more in-depth look), which is why you see that your program fully uses one core, and that all other cores are idle.

    The usual solution is to use the cluster core module of Node, which helps you launch a cluster of Node processes to handle the load, by allowing you to create child processes that all share the same server ports.

    However, you can't really use this without fixing Ghost's code. An option is to use pm2, which can wrap a node program, by using the cluster module for you. For instance, with four cores:

    $ pm2 start app.js -i 4
    

    In theory this should work, except if Ghost relies on some global variables (that can't be shared by every process).

提交回复
热议问题