0

I am building a microservice-architecture and I am looking for a good way to stream events.

Currently I have a service that publishes an event that three other services need to react to in a certain way, however the reaction to this event shall happen only once.

At the moment I am using RabbitMQ and my service publishes three messages in seperate queues, and each subscribing service listens to one queue. So, only one service instance can actually take the message and react to it.

However, I do not like this approach, because if I want to add a new subscriber I have to add a new queue for the publishing service.

I am basically looking for some kind of middleware on an event stream, that lets multiple services listen to one event, but makes sure that only one instance of each service actually reacts to the event.

I haven't found anything yet, so I would appreciate suggestions.

KaffeeKaethe
  • 301
  • 1
  • 3
  • 10
  • The round robin approach is the default one in rabbit mq. Many listeners on a queue but only one consumer. – Robert Moskal May 02 '16 at 18:20
  • Yeah, but I need a sepereate queue for each consuming service. So something happens in Service A and Service B, C and D all need to react to this event. If I use one queue, only one of the services will react, because the message is consumed. If I publish three messages into the same queue, there is a chance that two or three instances of one service consume the message, so the reaction is still inaccurate. So I need three seperate queues, one for each service. If Service E needs to listen to the event as well, I need to make change in Service A, and breaks isolated changes. – KaffeeKaethe May 02 '16 at 18:47
  • (cont.) The change I need to make is to publish the event to a fourth queue that is consumed by Service E. – KaffeeKaethe May 02 '16 at 18:50
  • As @Nicholas Labrot mentioned below, RabbitMQ does provide a decent pub/sub mechanism. You may also want to look at a service bus and event sourcing as a matter of interest :) --- I have an answer on a service bus vs. queue here: http://stackoverflow.com/questions/25953891/why-do-we-need-service-bus-frameworks-like-nservice-bus-masstransit-on-top-of-me/25957608#25957608 – Eben Roux May 04 '16 at 04:49

2 Answers2

1

You may have a look at this page which explains how AMQP model works and thus RabbitMQ.

Your message is published to an Exchange not to Queue. The exchange will route the message to the binded queues according to the Exchange type and to the binded key and routing key.

Generally speaking, publishers do not mind of the routing behind an exchange. It's the consumers concern to create and bind queue to the exchange according to what they want to consume and how.

Thus if your producer publishes its messages to the ExchangeA using for example a direct exchange, Service A/B/C can create and bind their queues to this exchange and they will receive each the published message. If a new service E is spawned, it should create and bind a queue too to this exchange. Thus you do not need to modify the publisher.

Nicolas Labrot
  • 4,017
  • 25
  • 40
  • Thank you very much, I really understood the while concept wrong... Now it's clear and I think that is exactly what I want. Again, thank you very much. – KaffeeKaethe May 03 '16 at 07:29
0

Our suggestion is to use PUB/SUB model with RabbitMQ so that multiple subscribers can listen to single queue and act accordingly. See: https://www.rabbitmq.com/tutorials/tutorial-three-python.html