How does Redis PubSub subscribe mechanism works?

2019-08-17 02:03发布

问题:

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

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.

Any other Idea?

回答1:

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.