Josh Wilson
Josh Wilson

Reputation: 3745

Jedis Many Subscribers

I'm building a push server in java and am planning on using Redis PubSub to queue up messages to send to the client.

Right now my implementation has a redis subscriber per device. So when a device comes online it'll subscribe to a redis queue for it's device.

Does this scale ok/is there a better way? I'll have thousands of subscribers.

Upvotes: 2

Views: 742

Answers (1)

Carl Zulauf
Carl Zulauf

Reputation: 39558

A subscriber per device should work fine. I think the scaling questions mostly revolve around what exactly you are publishing to each subscriber and where the messages actually live. You might also need to consider running multiple redis instances at some point, possibly managed by Redis Sentinel, to ensure high availability. Redis Sentinel will handle promoting one of your slave redis instances to master if master becomes unavailable, and then promoting the original master back to master when it comes back and has caught up.

If the messages are unique to each subscriber, then sending the messages to each subscriber seems like a good approach. Be aware that pub/sub in redis doesn't offer durability, so if I subscriber disconnects or crashes or whatever and comes back, all messages sent since he last subscribed are not available to him. If you need durability, then messages should probably go to a LIST for each subscriber, and what gets published on the channel should just be a notification to the client that new message(s) are available. The subscriber can then pop any messages off the LIST at their leisure until the LIST is empty. This process would repeat any time a notification is received on the channel.

If a single message is being broadcast to all or many of your clients, then you should probably think about storing the messages themselves in a redis key or list or something, and publish on each affected client's channel that a new message is available and where (what key) to read it. There are a number of strategies you could use to track which subscriber has read which messages and delete old messages read by everyone.

Upvotes: 2

Related Questions