What mechanism(s) does Redis use to keep messages in memory in case of pub-sub ? If no client is subscribed what happens to the messages? Will Redis buffer them ? Is there a way to configure the min. and max. memory allocated per channel ?
Aside from data storage, Redis can be used as a Publisher/Subscriber platform. In this pattern, publishers can issue messages to any number of subscribers on a channel. These messages are fire-and-forget, in that if a message is published and no subscribers exists, the message evaporates and cannot be recovered.
Redis Pub/Sub is the oldest style of messaging pattern supported by Redis and uses a data type called a “channel,” which supports typical pub/sub operations, such as publish and subscribe. It's considered loosely coupled because publishers and subscribers don't know about each other.
Redis and PHP Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.
Maximum Concurrent Connected Clients In Redis 2.6 and newer, this limit is dynamic: by default it is set to 10000 clients, unless otherwise stated by the maxclients directive in redis.
Redis does not keep messages in memory in the Pub/Sub context as you can see in the implementation (x):
Then Redis simply returns how many clients have received the message (keeping in mind that a client may receive a single message multiple times e.g if multiple pattern matches).
If there is no client subscribed, Redis simply returns 0 and the message is not recorded/buffered:
> publish foo test
(integer) 0
(x) basically Redis loops over the list of subscribed clients and sends a reply with the message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With