1

I have Service A that publishes an event to RabbitMQ. I have two instances of Service B that will use the event to write to its database. I have two instances of Service C that will send the event data back to a client via websockets.

Only one instance of Service B should handle the event so data isn't written to the database twice.

All instances of Service C should receive the event as any one of them could have the websocket connection that needs to get the data.

How do I have some services handle events round-robin and some instances handle events as broadcast, do I need to publish the same event to two different exchange types?

3
  • 1
    have a fan out exchange. B creates a single work queue and all Bs subscribe to that queue. C creates a new queue per instance. Commented Jul 9, 2023 at 14:19
  • Thanks, that was the answer I needed to get me on the right track 👍 Commented Jul 10, 2023 at 20:12
  • let me type it up as an answer Commented Jul 11, 2023 at 7:46

1 Answer 1

1

With Rabbit MQ you send messages to exchanges and route them to queues defined by the consumers.

So in your case you can publish your messages to a "fan out" exchange, which will send a copy of the message to all attached queues.

Your consumers then connect, defining their queue. So B can define a single queue which all Bs share and will pick up messages from round robin and C can use a random name to define a new queue for each instance of the service. Because the exchange is "fan out" all of these new queues, and hence each instance of C will get a copy of the message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.