简体   繁体   English

Redis发布/订阅缩放

[英]Redis Pub/Sub Scaling

I've been rewriting my nodejs game app to read memory from redis so I could ultimately scale it if it were to ever grow large. 我已经重写我nodejs游戏的应用程序从存储器中读取redis ,所以我可以最终扩展它,如果它是永远变大。 But, I stopped because I feel like I am going about it the wrong way. 但是,我停下了脚步,因为我觉得自己走错路了。 For example: 例如:

在此处输入图片说明

Server 1 is on port 9300 Server 1位于端口9300上

Server 2 is on port 9301 Server 2在端口9301上

Now, let's say a player from the Server 1 wants to send a private message to a player that is on Server 2 . 现在,假设Server 1的播放器想要向Server 2上的播放器发送private message

What I currently do 我目前正在做什么

Server 2 will send a publish signal to redis, and Server 1 will catch that signal as well, and if that user is on that server, it will send them a notification along with the message. Server 2将向redis发送一个发布信号, Server 1也将捕获该信号,如果该用户在该服务器上,它将向他们发送通知以及消息。

Some questions 一些问题

1) Wouldn't it be more appropiate to just have Server 2 send a message to Server 1 without publishing to redis ? 1) 仅将Server 2发送消息到Server 1而不发布到redis是否更redis

2) Server 2 doesn't keep a track of all the connected clients that Server 1 has, so it wouldn't be possible. 2) Server 2不会跟踪Server 1拥有的所有已连接客户端,因此这是不可能的。 Unless, I keep track of all connected clients on every server aswell? 除非,我还要跟踪每台服务器上所有已连接的客户端? That would require the client to connect to multiple servers on each visit. 这将要求客户端每次访问都连接到多台服务器。

3) Let's say I have 10 servers. 3)假设我有10台服务器。 A user on Server 5 wants to send a private message to a user on Server 1 . Server 5上的用户希望向Server 1上的用户发送私人消息。 If I send a pub signal through redis , Servers 4,6,7,8,9,2,3 will all receive that signal as well... which is unneeded? 如果我通过redis发送发布信号,则Servers 4,6,7,8,9,2,3也会全部收到该信号...这是不需要的吗? Is that when Peer to Peer connections come into play? 是点对点连接发挥作用吗? Or is that the extra bandwidth that is required for scaling and I'm overthinking everything? 还是扩展所需的额外带宽,而我却过分考虑了一切?

Perhaps you could change concept of your app by adding RabbitMQ instead of redis pub/sub. 也许您可以通过添加RabbitMQ而不是redis pub / sub来更改应用程序的概念。 RMQ would allow smarter message routing. RMQ将允许更智能的消息路由。

Basicly each user can listen its own messages: 基本上每个用户都可以收听自己的消息:

  • User connects to server (1-x) 用户连接到服务器(1-x)
  • Server subscribes to RMQ exchange messages with user routing key 服务器使用用户路由密钥订阅RMQ交换消息
  • When user publishes private message it's sent to provided exchange with specific user routing key 当用户发布私人消息时,它会发送到具有特定用户路由键的提供的交换中

Still that concept will hit the limit if you get large number of users. 如果您拥有大量用户,那么该概念仍然会达到极限。 Connection count to RMQ server will grow intensivly. 与RMQ服务器的连接数将激增。 In that case you can scale RMQ or change connection concept: 在这种情况下,您可以扩展RMQ或更改连接概念:

  • Save all user connection info to redis 将所有用户连接信息保存到Redis
  • When user sends private message to some other user first find on which server instance user is 当用户向其他用户发送私人消息时,首先查找该用户在哪个服务器实例上
  • Send message only to specific server instance (fetched from user connection information) 仅将消息发送到特定的服务器实例(从用户连接信息中获取)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM