1

I have a redis instance that publishes messages via different topics. Instead of implementing a complex heartbeat mechanism (complex because the instance would stop publishing messages after some time if they are not consumed), is there a way to check whether pubs are consumed by anyone?

For example, instance RedisServer publishes messages to topic1 and topic2. RedisClient1 subscribes to topic1 and RedisClient2 subscribes to topic2. When RedisClient2 for whatever reason stops consuming messages of topic2 then I want RedisServer to know about it and decide when to stop publishing messages to topic2. The discontinuation of topic2 consumption is unpredictable hence I am not able to inform RedisServer of the discontinuation/unsubscription.

I thought if there was a way for a redis instance to know whether messages of a certain topic are consumed or not then that would be very helpful information.

Any idea whether that is possible?

Matt
  • 7,004
  • 11
  • 71
  • 117

2 Answers2

3

Given you are using a recent-enough version of redis (> 2.8.0) these two commands may help you:

PUBSUB CHANNELS [pattern]

Which lists the currently active channels ( = channel having at least one subscriber) matching the pattern.

PUBSUB NUMSUB [chan1 ... chanN]

Which returns the number of subscribers for the specified channels (doesn't work for patterns however).

Note: Both solutions won't enable you to determine if a message was truely processed! If you need to know about completion of tasks (if your messages are triggering something), then I would recommend searching for a full blown job queue (for example Resque, if you want to stick with Redis)


Edit: Here's the Redis doc. for all of the above: http://redis.io/commands/pubsub

pysco68
  • 1,136
  • 9
  • 15
  • Perfect, exactly what I was looking for (I just need to know whether there is any subscriber). Thanks @pysco68. For those wondering here the implementation provided by Stackexchange.Redis : http://stackoverflow.com/questions/26362640/stackexchange-redis-get-the-count-of-channel-subscriptions-i-e-pubsub-numsub – Matt Feb 02 '15 at 09:30
3

You can also use the result of PUBLISH. It will give you the number of subscribers that received the message: http://redis.io/commands/publish

This way you don't need to poll the PUBSUB command, just do your "stop publishing" messages logic after you publish a message.

At most you publish one message with no one subscribing.

  • I was not aware of that, this is even better and easier to handle. Would you know whether the count includes those that subscribe to a pattern that matches the topic to which the Publish was sent? – Matt Feb 02 '15 at 10:01
  • It sould include pattern subscriptions. But you still would need to poll the methods from my answer from time to time in order to know when you can "publish" again (it this is required) – pysco68 Feb 02 '15 at 10:13
  • Btw. as you seem to use Stackexchange.Redis, it's Publish method returns the number of subscribers – pysco68 Feb 02 '15 at 10:15
  • @pysco68, my understanding is that this is what João Parreira is saying, that the Publish method returns the number subscribers. – Matt Feb 02 '15 at 10:20