Competing Consumer on Redis Pub/Sub supported?
I have 2 services开发者_开发百科. Both of them need subscribe to the same channel.
The 2 services are load balanced. Each service runs on multiple servers.
So how can I be sure only 1 instance of each service consume the message of that channel.
Is this supported on Redis?
Thanks
Pubsub doesn't work that way - the message goes to all connected subscribed clients. However, you could set it up so that the channel is a notification of an update to a list. That way all clients will get the message, but only one can take the item from the list with LPOP.
Another approach would be to use B*POP
from your service instances. If you have lots of clients running B*POP
against a list, whenever you LPUSH
to it, one of those clients will get the data, but only one.
You need use Redis Streams with XREADGROUP, it's a new feature of Redis.
https://redis.io/topics/streams-intro
I’ve implemented this provisionally by writing a value keyed by the request ID (a UUID). The requester writes the value with set, prior to the publish, and the consumer will attempt to delete this item, and only the consumer who successfully deleted the item will be considered the consumer that will process the request. I haven’t yet tested this at scale, so it may be the case that it doesn’t survive the scale I desire.
精彩评论