Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Messaging Between Services Events vs Commands

I am trying to understand different methods used in messaging between services.

Let us say that I have a scenario where I need first service to notify the other that a user has asked for product creation, and the second service should receive this message, create a product and then respond telling the first service that a product has been created.

I am thinking that commands along with request/respond suits this scenario because the first service will need to address another specific service and will wait for feedback.

My understanding is that:

Events vs commands:

Events:

  • provide loose coupling between the services.
  • perform publishing to all queues and the services interested in such message will pick it.

Commands:

  • perform sending to a specific queue hence only the services that receive using that queue will consume it.

Request/Respond vs Publish/Subsribe:

Request/Respond:

In Request/Respond the first service requests from the other to perform an operation and waits until respond is returned from the later.

Publish/Subscribe:

First service just publish a message and continue processing without waiting for feedback or response.

Now I started to design the messaging system using RabbitMQ along with Masstransit saga (Masstransit.Automatonymous) which seems to follow Events with publish/subscribe methods.

My Question is:

can I use commands with publishing or events with request/respond?

Is my understanding correct? and can sagas be used with request/response?

like image 317
Yahya Hussein Avatar asked Jan 29 '23 04:01

Yahya Hussein


1 Answers

In general, your understanding is correct. However, I will summarise here too:

  • Events are used in pub/sub. Messages are published, and all subscribers get them. Publisher does not know how many subscribers will get the event, if any.
  • Commands are sent to a known address. There is only one subscriber, which will get this message. This is used for fire and forget.
  • Responses are also sent to a specific endpoint, with additional metadata like response address. So the consumer can do what it needs to do and send a reply back. This is done asynchronously, but the sender waits for response.

MassTransit sagas with Automatonymous support any type of message processing. You need to map all messages that saga consumes as state machine events, but these can be both commands and events - technically it doesn't matter. Sagas can publish and send messages, also can send requests and wait for replies.

As you question about publishing commands and using events for request-response. Techicanlly, MassTransit has no distinction in message types. Everything you publish is an event. Stuff you send can be a command or it can be something else, but this is not an event. When you use request-response, you have to send to a specific endpoint, so definitely this is not an event.

like image 145
Alexey Zimarev Avatar answered Feb 06 '23 12:02

Alexey Zimarev