A command asks one receiver to do something. An event says that something already happened.
Command, for example SendInvoice or ReserveStock:
- It is named as an order, with a verb first.
- It has exactly one handler.
- The sender expects it to be done, so the receiver can reject it.
- It usually goes to a point-to-point queue, where one consumer takes each message.
Event, for example OrderPlaced or PaymentFailed:
- It is named in the past tense, because it states a fact.
- The publisher does not know or care who listens.
- It can have zero, one or many subscribers.
- It goes to a topic or a fanout exchange, so every interested service gets its own copy.
The choice affects coupling, which means how much one service depends on another. With a command, the sender depends on the receiver being able to do the job. With events, you can add new consumers without changing the publisher. But the event's schema becomes a contract that many teams rely on, so it is hard to change.
- A command has an order-style name, one handler, and can be rejected.
- An event has a past-tense name, states a fact, and has zero or many subscribers.
- Commands go through point-to-point queues. Events go through pub/sub.
- Events decouple publishers, but their schema becomes a shared contract.
Read more
- Command message (https://www.enterpriseintegrationpatterns.com/patterns/messaging/CommandMessage.html)enterpriseintegrationpatterns.com
- Event message (https://www.enterpriseintegrationpatterns.com/patterns/messaging/EventMessage.html)enterpriseintegrationpatterns.com