The goal of this article is to provide information to developers who are considering microservice integration using Event Driven Architecture and Messaging.
In a monolithic application, software components invoke one another using language-level methods or function calls, and the objects are typically running within the same process on the same server.
In a microservices architecture, components are distributed and can be run on multiple processes across multiple servers. They communicate with one another across a network using an inter-service communications protocol. There are several factors to consider when selecting an inter-service protocol. Messaging is one that can provide many advantages, but it's not always appropriate for every App.
The selection of an inter-service communication protocol should be made with consideration of the service's goals and use cases. They are classified as either synchronous or asynchronous and by the number of receivers (single or multiple).
The first distinction between inter-service communication protocol types is whether they are synchronous or asynchronous.
In synchronous communication, the client sends a request and waits for a response from the service. The client code can only continue its task after it receives the server response. The protocol (HTTP/HTTPS) is synchronous.
If possible, avoid depending on synchronous communication (request/response) between multiple microservices, including for queries. Having HTTP dependencies between microservices not only makes your microservices not autonomous but also their performance is impacted if one of the services in the chain is impaired.
It's considered an anti-pattern when a "chain" of HTTP requests is created between microservices while serving a client request. This causes the client response to be delayed and dependent on the performance of all microservices in the chain. A better implementation would be to use asynchronous communication using Messaging in which the client request is served right away.
In asynchronous message communication, the client or message sender usually doesn't wait for a response. It just sends the message to a message queue or message broker. This enforces a microservice's autonomy.
The second distinction between inter-service communication protocol types is if they are communicating to a single receiver or multiple.
In a Single Receiver design each request must be processed by exactly one receiver or service. An example of this communication is the Command pattern.
In a Multiple Receiver design, each request can be processed by zero to multiple receivers. This type of communication must be asynchronous. An example is the publish/subscribe mechanism used in patterns like Event-driven architecture. This is based on an event-bus interface or message broker when propagating data updates between multiple microservices through events; it's usually implemented through a service bus or message broker by using topics and subscriptions.
A microservice-based application will often use a combination of these communication methods. The most common type is single-receiver communication with a synchronous protocol like HTTP/HTTPS when invoking a regular Web API HTTP service. Microservices however, typically use messaging protocols for asynchronous communication between microservices.
Events are records of something that has happened, a change in state. They are immutable (they cannot be changed or deleted), and they are ordered in the sequence of their creation.
Event-driven architecture refers to a system of loosely coupled microservices that exchange information between each other through the production and consumption of events. Interested systems can be notified of these state changes by subscribing to published events and then acting on the information using their business logic. This is referred to as the Pub Sub Pattern.
The main benefits of event-driven systems are asynchronous behavior and loosely coupled structures.
Benefits of the Event-Driven Microservice Architecture:
In the Domain-driven design (DDD) approach to software development, the business domain is carefully modeled in software and evolved over time, independently of the infrastructure that makes the system work. DDD is used to define the different bounded contexts that represent the various business processes that the application needs to perform.
These contexts are then joined together with events, creating a unidirectional dependency graph that decouples each bounded context from those that arise downstream, to create rich event-streaming business applications.
Enterprise Application Integration (EAI) is the implementation of technologies that facilitate communication between enterprise applications and microservices. EAI establishes a middleware framework that helps data flow freely between services without significant changes to the applications themselves.
Enterprise Messaging Systems (EMS) are the Middleware that facilitates the exchange of messages between microservices. Referred to as Message-Oriented Middleware (MOM), it's the infrastructure supporting sending and receiving messages between distributed services.
EAI architecture is made up of the following components.
Messaging systems have two primary architecture designs, Message Broker and Message Bus. The Message Bus architecture is an advanced way of implementing EAI.
The first inter-service communications solutions incorporated all the functionality required for integration into a central hub called the "broker". A key point in this model is the broker contains the logic.
In a broker approach, a central piece of software (the "message broker") resides in the middle of the network, and provides all message transformation, routing, and any other inter-application functionality. All communication between applications must flow through the hub, allowing the hub to maintain data concurrency for the entire network. It's referred to as a hub-and-spoke architecture.
Direct Communication - creates a direct information pipeline between the event generator and the interested parties. Communications involve only the sender, broker, and the designated receivers. As a state change occurs, those parties can almost immediately know that it happened.
Complex functions - can be achieved with chained Event Brokers. For example, by using a log-based Event Broker, events can be "replayed" as part of a historical log.
Broker Disadvantages
Bus architecture represents an evolution of the broker hub-and-spoke model. It uses a defined set of standards to govern the flow of data between applications and implies a common protocol spoken and understood by all participants.
A Message Bus architecture is where the endpoints contain the logic instead of the Broker (or Bus in this case). The Apps use a shared (common) set of interfaces and are responsible for preparing the message, so it complies with the type of messages the rest of the apps connected to the bus expect.
The term Enterprise Service Bus (ESB) refers to a communication bus between different applications. Each application is enabled to talk to the bus, which in turn allows the sharing of data and communications between the connected applications.
Tip: The terms Broker and Bus are often used interchangeably and there are many interpretations.
Thanks & Happy Reading!