Node.js 多服务器集群:如何在多个节点集群中共享对象 [英] Node.js multi-server cluster: how to share object in several nodes cluster

查看:42
本文介绍了Node.js 多服务器集群:如何在多个节点集群中共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个聊天室应用程序创建一个 node.js 服务器集群以支持高并发.我需要能够在所有节点之间共享信息.我试图找出保持所有服务器同步的最佳方式.我希望共享对象具有尽可能大的灵活性,因为我计划在未来添加更多功能.

I want to create a cluster of node.js server in order to support high concurrency, for a chat rooms application. I need to be able to share information between all nodes. I am trying to find out what would be the best way to keep all the servers in-sync. I want as much flexibility as possible in the shared object, as I plan to add more features in the future.

到目前为止,我想到了 2 个解决方案:

So far, I have 2 solutions in mind:

  1. 订阅 NoSQL 密钥(例如 redis 发布订阅)
  2. 节点使用套接字相互更新.
  1. Subscribe to NoSQL key (for example redis publish-subscribe)
  2. Nodes update each other using sockets.

哪个更好?还有其他想法吗?

Which is better? Any other ideas?

推荐答案

Redis 很好,因为它独立于您的节点应用程序并且相当容易扩展.您还可以将它用于 pub/sub 之外的许多内容,例如在您的节点服务器之间共享基本数据结构(散列、排序集、列表、字符串),以帮助它们以这种方式保持同步.从理论上讲,您可以将给定房间中的所有聊天保存为一个排序集,其中您的键是某个聊天对象的 json 表示(类似于 {'user':'some_user','msg':'some_msg'} 并且您的分数是时间戳,因此很容易按时间拉取对话).Redis 速度极快,其数据结构高度优化,因此单个服务器可以处理很多很多用户.

Redis is nice because it's independent of your node app and fairly easy to scale. You can also use it for a lot of stuff outside of pub/sub as well, such as sharing basic data structures (hashes, sorted sets, lists, strings) between your node servers to help keep them in sync this way as well. Theoretically, you could save all chats in a given room as a sorted set where your key is a json representation of some chat object (something like {'user':'some_user','msg':'some_msg'} and your score is the timestamp, so it's very easy to pull conversations by time). Redis is extremely fast, and its data structures are highly optimized, so a single server can handle many, many users.

我们在生产环境中有一个类似的设置,一个 Redis 服务器处理 100 万用户(每分钟大约 10k 次点击插入和 20k 次读取),并且 CPU 使用率很少在非 CPU 密集型的情况下超过 5%盒子.

We have a similar setup in production with one Redis server handling 1 million users (about 10k hits inserts and 20k reads from a sorted set per minute), and the CPU usage rarely gets above 5% on a non-CPU-heavy box.

这篇关于Node.js 多服务器集群:如何在多个节点集群中共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆