4

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?

Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114

1 Answers1

8

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.

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • Thank you for clarification, "saga consumes as state machine events, but these can be both commands and events" does this mean that what I define in saga as Event type can be an event or command according to how I send the message? – Yahya Hussein Nov 22 '17 at 09:44
  • Yes, from a consumer perspective (and Saga is just a bunch of consumers) there is no difference between events and commands. It is just a message. The separation is logical. – Alexey Zimarev Nov 22 '17 at 12:57
  • I see, thanks again. have you come across a sample project with saga and commands? – Yahya Hussein Nov 22 '17 at 13:00
  • This is the sample we have https://github.com/MassTransit/Sample-ShoppingWeb/blob/master/src/CartTracking/ShoppingCartStateMachine.cs but again, events are just messages. Saga doesn't see any difference. – Alexey Zimarev Nov 22 '17 at 13:53
  • public Event ItemAdded { get; private set; } does this mean that when CartItemAdded is raised itemAdded will be too? and how is this sample applying request/response?.. sorry for asking too many questions – Yahya Hussein Nov 22 '17 at 13:57
  • 1
    Automatonymous event is different from a message. The line you are citing is bingind the `CartItemAdded` message to the `ItemAdded` state machine event. – Alexey Zimarev Nov 22 '17 at 18:17