Cluster and Fork mode difference in PM2

后端 未结 3 607
孤独总比滥情好
孤独总比滥情好 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条回答
  •  -上瘾入骨i
    2020-12-12 14:11

    Documentation and sources are really misleading here.

    Reading up on this in the sources, the only differences seems to be, that they use either node cluster or child_process API. Since cluster uses the latter, you are actually doing the same. There is just a lot more custom stdio passing around happening inn fork_mode. Also cluster can only be communicated with via strings, not objects.

    By default you are using fork_mode. If you pass the the -i [number]-option, you're going into cluster_mode, which you generally aim for w/ pm2.

    Also fork_mode instance probably can't listen on the same port due to EADDRINUSE. cluster_mode can. This way you also can structure you app to run on the same port being automatically load balanced. You have to build apps without state then though e.g. sessions, dbs.

提交回复
热议问题