支持 Redis Pub/Sub 上的竞争消费者吗?
我有2个服务。他们都需要订阅同一频道。
这两个服务是负载平衡的。每个服务都运行在多个服务器上。
那么我如何确保每个服务只有 1 个实例消耗该通道的消息。
Redis 支持吗?
谢谢
I have 2 services. Both of them need subscribe to the same channel.
The 2 services are load balanced. Each service runs on multiple servers.
So how can I be sure only 1 instance of each service consume the message of that channel.
Is this supported on Redis?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Pubsub 不会以这种方式工作 - 消息会发送到所有连接的订阅客户端。但是,您可以对其进行设置,以便该频道成为列表更新的通知。这样,所有客户端都会收到该消息,但只有一个客户端可以使用 LPOP 从列表中获取该项目。
Pubsub doesn't work that way - the message goes to all connected subscribed clients. However, you could set it up so that the channel is a notification of an update to a list. That way all clients will get the message, but only one can take the item from the list with LPOP.
另一种方法是在服务实例中使用
B*POP
。如果您有许多客户端针对某个列表运行B*POP
,则每当您LPUSH
时,其中一个客户端都会获取数据,但只有一个。Another approach would be to use
B*POP
from your service instances. If you have lots of clients runningB*POP
against a list, whenever youLPUSH
to it, one of those clients will get the data, but only one.您需要将 Redis Streams 与 XREADGROUP 一起使用,这是 Redis 的一项新功能。
https://redis.io/topics/streams-intro
You need use Redis Streams with XREADGROUP, it's a new feature of Redis.
https://redis.io/topics/streams-intro
我通过编写一个由请求 ID(UUID)键入的值来临时实现这一点。请求者在发布之前写入设置的值,消费者将尝试删除该项目,并且只有成功删除该项目的消费者才会被视为将处理该请求的消费者。我还没有大规模测试过这个,所以它可能无法达到我想要的规模。
I’ve implemented this provisionally by writing a value keyed by the request ID (a UUID). The requester writes the value with set, prior to the publish, and the consumer will attempt to delete this item, and only the consumer who successfully deleted the item will be considered the consumer that will process the request. I haven’t yet tested this at scale, so it may be the case that it doesn’t survive the scale I desire.