CodePark/ questions / Message design, schemas & versioning
START PRACTICINGPRACTICE
Backend question · Message queues & streaming › Message design, schemas & versioning

What is the difference between an event and a command message? How should each be named and routed?

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.