Search code examples
rabbitmqmessagingsoanosql

Select consumers before publishing a message rabbitmq


I am trying to build a system where I need to select next available and suitable consumer to send a message from a queue (or may be any other solution not using the queue)

Requirements We have multiple publishers/clients who would send objects (images) to process on one side and multiple Analysts who would process them, once processed the publisher should get the corresponding response.

The publishers do not care which Analyst is going to process the data.

Users have a web app where they can map each client/publisher to one or more or all agents, say for instance if Publisher P1 is mapped to Agents A & B, all objects coming from P1 can be processed by Agent A or Agent B. Note: an object can only be processed by one agent only.

Depending on the mapping I should have a middleware which consumes the messages from all publishers and distributes to the agents

Solution 1 My initial thoughts were to have a queue where all publishers post their messages. Another queue where Agents publish message saying they are waiting to process an object.

A middleware picks the message, gets the possible list of agents it can send the message to (from cached database) and go through the agents queue to find the next suitable and available agent and publish the message to that agent.

The issue with this solution is if I have agents queue like a,b,c,d and the message I receive can only be processed by agent b I will be rejecting agents d & c and they would end up at the tail of the queue and I have around 180 agents so they might never be picked or if the next message can only be processed by agent d (for example) we have to reject all the agents to get there

Solution 2 First bit from publishers to middleware is still the same

Have a scaled fast nosql database where agents add a record to notify there availability. Basically a key value pair

The middleware gets config from cache and gets the next available + suitable agent from the nosql database sends message to the agent's queue (through direct exchange) and updates the nosql to set isavailable false ad gets the next message.

Issue with this solution is the db and middleware can become a bottleneck, also if I scale the middleware I will end up in database concurrency issues for example f I have two copies of middleware running and each recieves a message which can be proceesed by Agents A & B and both agents are available. The two middleware copies would query the db and might get A as availble and end up sneding both messages to A while B is still waiting for a message to process.

I will have around 100 publishers and 180 agents to start with.

Any ideas how to improve these solutions or any other feasible solution would be highly appreciated?

Depending on this I also need to figure out how the Agent would send response back to the publisher.

Thank you


Solution

  • Since the total number of senders and receivers are only hundreds, how about to create one queue for each of your senders. Based on your sender receiver mapping, receivers subscribes to the sender queues (update the subscribing on mapping changes). You could configure your receiver to only receive the next message from all the queues it subscribes (in a random way) when it finishes processing one message.