简体   繁体   English

Redis pub sub适用于多个生产者和多个消费者

[英]Redis pub sub for multiple producers and multiple consumers

Lets say there are N producers and M users which subscribes to these N producers. 可以说有N个生产者和M个用户订阅了这N个生产者。 Here N producer produce N different types of messages eg 在这里,N个生产者产生N种不同类型的消息,例如

producer1 produces  messageType1, 
producer2 produces  messageType2,
producer3 produces  messageType3,
.
.
. 
producerN produces  messageTypeN.

M users can subscribe to these messages. M个用户可以订阅这些消息。 One user can subscribe to multiple types of messages. 一个用户可以订阅多种消息。 Eg 例如

user1 consumes (messageType1, messageType2, messageType10)
user2 consumes (messageType14, messageType5)
.
.
userM consumes (messageType21, messageType22, messageType23, .... messageTypeN)

Users may consume same or distinct message types. 用户可能使用相同或不同的消息类型。 My questions is how to design this scenario. 我的问题是如何设计这种情况。 It looks like pub sub pattern. 它看起来像pub子模式。 For this scenario, do I have to create channels per user in redis. 对于这种情况,我是否必须在Redis中为每个用户创建频道。 If yes, there is a limitation on number of redis channel one can create (10K). 如果是,则可创建的Redis通道数有限制(10K)。 In that case how to handle millions of user? 在那种情况下,如何处理数百万用户? Any help would be appreciated. 任何帮助,将不胜感激。

In the pub/sub scenario, you should create channels per producer . 在pub / sub方案中,您应该为每个生产者创建频道。 Each user subscribes channels of the corresponding producers. 每个用户订阅相应生产者的频道。

User Side 用户端

// user1
subscribe producer1 producer2
// user2
subscribe producer2

Producer Side 生产方

// producer1
publish producer1 message1
// producer2
publish producer2 message2

The limitation is NOT the number of channels you can create, but the number of client connections . 限制不是您可以创建的通道数,而是客户端连接数 You CANNOT have millions of users connect to a single Redis instance at the same time. 您不能同时有数百万个用户连接到单个Redis实例。

A possible solution 可能的解决方案

In order to achieve that, you have to create multiple Redis instances, and shard users into shardings. 为了实现这一点,您必须创建多个Redis实例,并将用户分片到分片中。 Each Redis instance creates a full list of producers, and handles connections from only one sharding of users. 每个Redis实例都会创建完整的生产者列表,并仅处理来自一个用户分片的连接。

When producing messages, you can publish the message on the corresponding channel of each Redis instances, so that users who subscribed the channel, can receive the message. 生成消息时,可以在每个Redis实例的相应通道上发布消息,以便订阅该通道的用户可以接收消息。

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

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