RabbitMQ way of using parallel work?


Hello, first thank you very much for providing such excellent tutorial and clean architecture design. I am learning the micro services with message broker and would be more than interested in how to use message broker to improve the micro services performance in real world cases.

In your Episode-3 where you introduced the publisher and subscribe with command handler way for delivering message between micro services, I research the code and find that it seems to use the “TOPIC” type of messaging with rabbitmq which would be a “single task message queue”, I was wondering if you could better introduce why you choose “topic” message type in this project, and I am more curious if it’s possible to do parallel work/services with rabbitmq.

For example, you may have three instances of Discounts Services running at the same time, meanwhile there would be 10k message coming at busy traffic time, what I wish to happen is to let rabbitmq queue distributes the 10k messages to your three instances of services continuously so we could decrease the workload of one service instance to its 1/3 size.

I am curious if such scenario could be implemented using rabbitmq with .net web api micro services in your projects? In addition, for the docker section, could we setup and start multiply instances of services at same time? So far I cannot think about any ideal solution by using the “topic”/“fanout”/“direct” queue of rabbitmq. However, I think if it’s possible to implement such scenarios will be another good benefit of using micro services architecture.

Sorry about my above complicated information, I really appreciate your tutorial and guide which brought me into a fantastic new world!


I’m also curious about this, it seems like if I have multiple instances of a service subscribed to an event, all of them will pick it up and try to process it? Wouldn’t that be a problem?


If I am not mistaken, RabbitMQ being an implementation of the AMQP protocol requires each ‘consumer’ of the queue send an acknowledge message back to the queue when they pickup a message. This confirmation indicates to the queue that this message has been picked up and handled, and therefore will be removed from the queue. This means that even if you have multiple instances, only one will get the chance to deal a particular message.


I see, does that mean it’s a queue, which allows multiple subscribers to pick unique messages like kafka consumer group?


I am unfortunately not familiar with Kafka but I am pretty sure that in RabbitMq, a message can be consumed only once, and after it is consumed, it is removed from the queue and isn’t accessible anymore.


So what if we need to consume the same event (OrderCompleted for example) both from ProductsService and CustomerService? It seems this is not available in this example or am i missing sth?


Why would you need to consume same event?
In product and order service, their respective transaction is already processed, it’s supposed to be fire and forget and if no compensate handles, you should just assume it went through?


I need to increase number of orders for customer and descrease the stock for product for example. Should we do that in a different way?


You should have an event: add item to order/cart, do ur repo related work, then publish an item added to cart complete event, then product subscribe to item added to cart and reduce stock.

Or to be safer, reduce stock qty first then do cart related work. That way you wouldn’t risk ppl over ordering stocks


What you would like to do is the publish-subscribe pattern.

Event is published to a topic. Topic has many subscriptions, one per each subscriber. The subscribers (eg. ProductsService or CustomerService) receive message on their own subscription, react to it and then remove the event from the subscription.

By contrast, command is usually intended for one type of consumer. It is published to a queue, consumer reads the command from the queue, does the thing intended in the command and removes the command from the queue.

In rabbitmq you can use https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-direct for implementing commands and https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-topic for implementing events.