如何扩展 gRPC 发布/订阅聊天服务?

发布于 2025-01-20 06:54:40 字数 702 浏览 2 评论 0原文

我正在研究如何与GRPC进行聊天服务。我注意到大多数示例将所有订户的连接存储在列表数据结构中。当聊天室收到一条新消息时,服务器将循环浏览该列表并发送新消息。

将订阅者存储在源代码的变量订户

我的问题是,当订户编号长大并将所有订户存储在哈希图中似乎是一个坏主意,因为这会花费太多的内存?我试图将这些连接存储在redis中,但是可能不可能由于连接无法序列化。

我的想法是当我在Kubernetes中部署多个POD实例时。每个聊天服务都变得独立。当订户分发在不同的服务器上时,我该如何正确获取订户?

I'm studying how to make a chat service with gRPC. I notice that most of the examples store all the subscriber's connections into a List data structure. When the chatroom got a new message, the server will loop through that List and send new messages.

Stores subscribers in source code's variable subscribers.

My question is that when the subscriber number grows up and storing all the subscribers in the HashMap seems like a bad idea because that will cost too much memory? I have tried to store those connections in Redis but it might be impossible due to the connection is not serializable.

My Idea is when I deploy multiple pod instances in Kubernetes. Each chat service becomes independent. How could I get the subscribers correctly when they are distributed on different servers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-01-27 06:54:40

以下是我思考的几个方面:

  • 任何聊天系统在聊天室大小方面都有一些限制。您想支持多少订阅者?如果您想支持数十(甚至数百)个订阅者,我相信它可以很好地处理内存中的订阅者列表,因为它比将订阅者列表存储在外部缓存中要快得多
  • 。您可以使用 IO 优化的线程池并行执行此操作,而不是在该列表上循环。
  • 在 Pod 中部署聊天服务是一个好主意,您需要水平扩展的能力,但您还需要在服务前面有一个智能网关,将来自给定用户的请求路由到存储其内容的 Pod信息(如果订户列表存储在内存中)。否则,如果此信息存储在外部(在缓存中),则您的聊天服务可以是完全无状态的。

Here are a couple of aspects that I'm thinking:

  • Any chat system has some limits in terms of chatroom size. How many subscribers do you want to support? If you want to support tens (or even hundreds) of subscribers, I believe it works well with an in-memory list of subscribers, since it's much faster than having the subscriber's list in an external cache
  • When you need to send a message to the entire list of subscribers, instead of looping on that list, you can do that in parallel, using an IO-optimized thread-pool.
  • Deploying your chat-service in a pod is a good idea, you need the ability to scale horizontally, but you also need a smart-gateway in front of your service, to route the requests from a given user, to the pod that stores its information (if the subscribers list is stored in memory). Otherwise, if this info is stored externally (in a cache), your chat service can be fully stateless.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文