Quite new to RabbitMQ and I'm trying to see if I can achieve what I need with it.
I am looking for the Worker Queues pattern but with one caveat. I want to have only a single worker running concurrently per routing key.
An example for clarification:
If i send the following messages with routing keys by order: a
, a
, b
, c
, I want to have only 3 workers running concurrently. When the first a
message is received a worker picks it up and handles it.
When the next a
message is received and the previous a
message is still handled (not acknowledged) the new a
message should wait in queue. When the b
and c
messages are received they each get a worker handling them. When the first a
message is acknowledged any worker can pick up the next a
message.
Would that pattern be possible using RabbitMQ in a natural way (without writing any application code on my side to handle the locking and stuff...)
Edit:
Another clarification. All workers can and should handle all messages, and I don't want to have a queue per Worker as I want to share the load between them, and the Publisher doesn't know which Worker should process the message. But I do want to make sure that no 2 Workers are working on messages sharing the same key at the same time.
For example, if I have a Publisher publishing messages with a userId
field, I want to make sure no 2 Workers are handling messages with the same userId
at the same time.
Edit 2
Expanding on the userId
example. Let's say I have a single Publisher and 3 Workers. The publisher publishes messages like these: { userId: 1, text: 'Hello' }
, with varying userId
s. My 3 Workers all do the same thing to this messages, so I can have any of them handle the messages coming in. But what I'm trying to achieve is to have only a single worker processing a message from a certain user at the same time. If a Worker has received a message with userId
1 and is still processing it, and another message with userId
1 is received I want to make sure no other Worker picks up that message. But other messages coming in with different userId
s should be processed by other available Workers.
userId
s are not known beforehand, and the publisher doesn't know how many workers are or anything specific about them, he just wants to schedule the messages for processing.