by BehindJava

What is the difference between an Event and a Message in Microservices

Home » microservices » What is the difference between an Event and a Message in Microservices

In this tutorial, we are going to learn about the differences between an Event and a Message in detail.

In order to understand this clearly. Let’s talk about the Event and Message in terms of object-oriented programming.

  • Assume that there is a base or super class called a message, where it got two subclasses called Event and Command. When you hear a message in the context of microservices, it means the commands, which are used interchangeably. event command
  • So an Event is something that has already happened in the past and therefore the order of the Events cannot be changed. Example: World War I to cannot happen after the World War II.
  • Commands are changeable, which means order and priority can be changed and commands can be sent to the other microservices in two ways.
  • Either, you can make point to point API call. For example, HTTP post call to another microservice, or you can deliver your message to a message broker and other microservices and subscribe to that message broker to receive the commands.
  • Events are handled via event streaming platforms such as Apache Kafka and Active MQ, which is very common amongst java developers and Rabbit MQ is popular amongst the .net developers. There is something new that is quite going to take off the above message brokers, i.e., Solace.

To see this in more detail, let’s take an example and see how the message and events work. example

  • Imagine that you have three microservices in which one of them is payment service followed by communication service and order service.
  • When the user pays by credit card. We raise an event to the Apache Kafka, which is an event streaming platform. Payment received is an event that has already happened, and then order service subscribes to that event and receives the event.
  • Then order service wants to send an invoice to the customer because it has processed the orders and now wants to email the customer and say, Hey, this is your invoice.
  • For that, it has to send a message to the communication service. So sending invoice is not happened yet, and we want that to happen by sending the message to solace in this example.
  • And then, communication service also subscribes to that channel in Solace and receives that message and then sends that invoice to the customer.