Redis PubSub 订阅机制是如何工作的? [英] How does Redis PubSub subscribe mechanism works?

查看:54
本文介绍了Redis PubSub 订阅机制是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个发布-订阅基础设施,让每个订阅者都可以收听多个(比如 10 万个)频道.

I want to create a Publish-Subscribe infrastructure in which every subscriber will listen to multiple (say 100k) channels.

我认为为此目的使用 Redis PubSub,但我不确定订阅数千个频道是否是这里的最佳实践.为了回答这个问题,我想知道 Redis 中的订阅机制在后台是如何工作的.

I think to use Redis PubSub for that purpose but I'm not sure if subscribing to thousands of channels is the best practice here. To answer this I want to know how subscribing mechanism in Redis works in the background.

另一种选择是为每个订阅者创建一个频道,并在两者之间放置一些组件,这将获取所有消息并将其发布到相关频道.

Another option is to create a channel per subscriber and put some component in between, that will get all messages and publish it to relevant channels.

还有其他想法吗?

推荐答案

Salvatore/creator of Redis 在这里回答了这个问题:https://groups.google.com/forum/#!topic/redis-db/R09u__3Jzfk

Salvatore/creator of Redis has answered this here: https://groups.google.com/forum/#!topic/redis-db/R09u__3Jzfk

All the complexity on the end is on the PUBLISH command, that performs
an amount of work that is proportional to:

a) The number of clients receiving the message.
b) The number of clients subscribed to a pattern, even if they'll not
match the message.

This means that if you have N clients subscribed to 100000 different
channels, everything will be super fast.

If you have instead 10000 clients subscribed to the same channel,
PUBLISH commands against this channel will be slow, and take maybe a
few milliseconds (not sure about the actual time taken). Since we have
to send the same message to everybody.

这篇关于Redis PubSub 订阅机制是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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