在集群模式下安全地使用 pm2 和 node.js
Is using pm2 with node.js in cluster mode safe
所以 pm2 是一个进程管理器,它允许我在同一台服务器上启动我的应用程序的多个实例。它的工作原理是它对所有实例的请求进行负载平衡。
所以我认为这是 Node.js 环境的某种形式的假多线程。然后我想到了以下场景:
What if my Node.js app instances are connected to MongoDB and instance
0 is doing a find request while at the same time instance 1 is
actually inserting into the same collection?
MongoDB 不是单线程的,它有一个线程池。这是否意味着可能存在冲突或线程安全现在与 MongoDB 中的某种形式的互斥锁有关?
这只是一个示例案例。 pm2有没有可能出现这样的问题?
从 MongoDB's docs 开始,它支持并发控制。
Concurrency control allows multiple applications to run concurrently without causing data inconsistency or conflicts
如果需要更新多个文档,可以使用它的$isolated operator
Using the $isolated operator, a write operation that affects multiple documents can prevent other processes from interleaving once the write operation modifies the first document. This ensures that no client sees the changes until the write operation completes or errors out.
请注意 $isolated 不适用于分片集群,但我假设您只使用一个 MongoDB 数据库实例。
我在我的所有节点应用程序中都使用 PM2。就集群模式而言,它是尽可能安全的(至少根据我的经验)。不过,在 PM2 中有一些注意事项需要注意。
注意你的配置文件。无论您将使用什么格式(JSON YAML 等),您都需要了解每个设置的后果。例如使用最大核心等
强烈建议不要使用 watch 参数。它似乎会减慢系统速度,而且我在分配内存时遇到了麻烦。
使用 PM2 Monit 监控服务器上发生的事情,这样您就可以掌握一切。
如果您正在处理需要复杂集群的稳健生产,那么建议您看看其他虽然成本高昂的选项。在一个大型的、多样化的、高使用率的集群系统中,我不会把整个集群的操作交给一个系统。
但是安全吗?就我的经验而言,是的。
所以 pm2 是一个进程管理器,它允许我在同一台服务器上启动我的应用程序的多个实例。它的工作原理是它对所有实例的请求进行负载平衡。
所以我认为这是 Node.js 环境的某种形式的假多线程。然后我想到了以下场景:
What if my Node.js app instances are connected to MongoDB and instance 0 is doing a find request while at the same time instance 1 is actually inserting into the same collection?
MongoDB 不是单线程的,它有一个线程池。这是否意味着可能存在冲突或线程安全现在与 MongoDB 中的某种形式的互斥锁有关?
这只是一个示例案例。 pm2有没有可能出现这样的问题?
从 MongoDB's docs 开始,它支持并发控制。
Concurrency control allows multiple applications to run concurrently without causing data inconsistency or conflicts
如果需要更新多个文档,可以使用它的$isolated operator
Using the $isolated operator, a write operation that affects multiple documents can prevent other processes from interleaving once the write operation modifies the first document. This ensures that no client sees the changes until the write operation completes or errors out.
请注意 $isolated 不适用于分片集群,但我假设您只使用一个 MongoDB 数据库实例。
我在我的所有节点应用程序中都使用 PM2。就集群模式而言,它是尽可能安全的(至少根据我的经验)。不过,在 PM2 中有一些注意事项需要注意。
注意你的配置文件。无论您将使用什么格式(JSON YAML 等),您都需要了解每个设置的后果。例如使用最大核心等
强烈建议不要使用 watch 参数。它似乎会减慢系统速度,而且我在分配内存时遇到了麻烦。
使用 PM2 Monit 监控服务器上发生的事情,这样您就可以掌握一切。
如果您正在处理需要复杂集群的稳健生产,那么建议您看看其他虽然成本高昂的选项。在一个大型的、多样化的、高使用率的集群系统中,我不会把整个集群的操作交给一个系统。
但是安全吗?就我的经验而言,是的。