Cluster and Fork mode difference in PM2

后端 未结 3 629
孤独总比滥情好
孤独总比滥情好 2020-12-12 13:31

I\'ve searched a lot to figure out this question, but I didn\'t get clear explanation. Is there only one difference thing that clustered app can be scaled out and forked app

3条回答
  •  情深已故
    2020-12-12 13:57

    The main difference between fork_mode and cluster_mode is that it orders pm2 to use either the child_process.fork api or the cluster api.

    What does this means internally?

    Fork mode

    Take the fork mode as a basic process spawning. This allows to change the exec_interpreter, so that you can run a php or a python server with pm2. Yes, the exec_interpreter is the "command" used to start the child process. By default, pm2 will use node so that pm2 start server.js will do something like:

    require('child_process').spawn('node', ['server.js'])
    

    This mode is very useful because it enables a lot of possibilities. For example, you could launch multiple servers on pre-established ports which will then be load-balanced by HAProxy or Nginx.

    Cluster mode

    The cluster will only work with node as it's exec_interpreter because it will access to the nodejs cluster module (eg: isMaster, fork methods etc.). This is great for zero-configuration process management because the process will automatically be forked in multiple instances. For example pm2 start -i 4 server.js will launch 4 instances of server.js and let the cluster module handle load balancing.

提交回复
热议问题