My overall question is: Using Redis for PubSub, what happens to messages when publishers push messages into a channel faster than subscribers are able to read them?
For example, let's say I have:
- A simple publisher publishing messages at the rate of 2 msg/sec.
- A simple subscriber reading messages at the rate of 1 msg/sec.
My naive assumption would be the subscriber would only see 50% of the messages published onto Redis. To test this theory, I wrote two scripts:
pub.py
queue = redis.StrictRedis(host='localhost', port=6379, db=0)
channel = queue.pubsub()
for i in range(10):
queue.publish("test", i)
time.sleep(0.5)
sub.py
r = redis.StrictRedis(host='localhost', port=6379, db=0)
p = r.pubsub()
p.subscribe('test')
while True:
message = p.get_message()
if message:
print "Subscriber: %s" % message['data']
time.sleep(1)
Results
- When I ran
sub.py
first, immediately followed bypub.py
, I found thatsub.py
actually displayed all the messages (1-10), one after another with a delay of 1 second in between. My initial assumption was wrong, Redis is queuing messages. More tests needed. - When I ran
pub.py
first, then waited 5 seconds before runningsub.py
, I found thatsub.py
only displayed the second half of the messages (5-10). I would have assumed this originally, but given my previous results, I would have thought messages were queued, which led me to the following conclusion...
Conclusions
- Redis server appears to queue messages for each client, for each channel.
- As long as a client is listening, it doesn't matter how fast it reads messages. As long as it's connected, messages will remain queued for that client, for that channel.
Remaining Questions
- Are these conclusions valid?
- If so, how long will client/channel messages remained queued?
- If so, is there a
redis-cli info
command to see how many messages are queued (for each client/channel)?