how to keep variables that share all node processes in node cluster?

前端 未结 6 1939
离开以前
离开以前 2021-02-02 12:17

It seems like all the node woker processes are working as if it is executing a new copy of the same application. But would like to keep some variables that are shared by all nod

6条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 12:29

    I think the whole idea of cluster is having instances that can run independently on different cpus. Sharing memory (a global variable) that they both can access and change introduces more complexity (locks, etc) and makes these instances depend on one another.

    An outside database would be a good solution to this since it takes care of all th data access problems, but it most likely lowers performance.

    Messaging is a better idea. You can hold local instances of a var in your clusters. When a cluster updates the value, send a message to the rest of them and update the value. This is great because it's async and nonblocking but again your value update won't reflect immediately.

    How about this: store vars on a db and everytime there's a value change notify the instances. They can store the new values in local vars and make db calls only when it's needed

提交回复
热议问题