in Coding

Messages vs. Events vs. Commands

In a distributed environment, services must communicate with each other. One of the most popular ways of communication is messaging. Tools like ActiveMQ, RabbitMQ or Kafka are making it easy to exchange messages between systems. But no matter which broker you are using, you must decide which kind of message you want to send.

Messages

Message brokers deal with messages. They are the basic unit of communication and can literally be anything. An ID, a string, an object, a command, an event or whatever. Messages have no special intent.

Having no special intent makes messages generic but also less meaningful. That’s why we can use two more concepts on top of messages.

Events

An event is a message which informs various listeners about something which has happened. It’s send by a producer which doesn’t know and doesn’t care about the consumers of the event.

Example: A typical example would be an online shop. Whenever an order is placed, the shop would publish an OrderSubmittedEvent to inform other systems (e.g. the logistics system) about the new order. However, the shop doesn’t care – or even know – about the consumers. If the logistics system isn’t interested in the events anymore, it just unsubscribes from the list of consumers.

⇒ In the world of JMS, events would be messages send to a topic.
⇒ The dependency goes from the consumer to the producer.

Commands

A command on the order side is much more specific. It’s a one-to-one connection between a producer (who sends the command) and a consumer (who takes and executes the command).

Example: In case of an online shop, such a command might the BillCustomerCommand. After an order is places, the online shop sends this command to the billing system to trigger the invoice.

⇒ In the world of JMS, commands would be messages send to a queue.
⇒ The dependency goes from the producer to the consumer.

The intent

The difference between messages, events and commands lies in their intent. While messages have no special intent at all, events inform about something which has happened and is already completed (in the past). Commands trigger something which should happen (in the future).

More

Best regards,
Thomas

Write a Comment

Comment