In modern software architecture world, use of Event Driven Architecture is not new. EDA is a design pattern where system components communicate through events. EDA uses Events for triggering and communicating between decoupled services. Hence we can certainly say that EDA is mainly driven by Events. However, Events might not always be the optimal solution. Before I delve into when to use Events and when to use Messages in EDA, let’s first understand the primary difference between them.
EVENTS Vs MESSAGES
Events Event is something you are advertising that has already taken place. In lame words, Event is something which has happened in past. e.g. Placed an order, Payment done successfully, Order placed, Inventory updated, etc
1. Red boxes showing use of Events in EDA exampleAbove diagram illustrates Events generated by each Event Producer (Microservice)
You can figure out with above diagram that Event is used more in pub/sub pattern, where publisher publishes the Event and interested services subscribes to it. Typically topics, fanout exchanges, streams or notification services are used. Here message channel is owned by the sender.
Messages Message is more of command or query. Its more like — I need you to do something for me Or I need some information from you.
2. Blue boxes showing use of Messages in EDA examplee.g. Apply payment for this order, give me a payment type,etc. Messages are typically used in point to point communication using Queues, direct exchange, messaging services. Here message channel is owned by receiver.
Now that we know about Events and Messages in deeper, question is
When should we use Events Vs Messages in EDA?
As shown in above diagram 1, although all calls are fire and forget, Order service needs to know whether payment was applied or not. In such scenario, we can use Queue for point to point messaging between Payment service and Order Service. Then Payment service can notify Order service that whether payment was successful or not. OR Order service can subscribe to Payment Applied Event of Payment service. Hence here Message/point to point communication is not required and hence we should stick to Events.
Now if we look at diagram 2, we control the order in which services are getting invoked i.e. Firstly the Payment service, then Inventory service and finally Notification service. Hence when we want to control the order in which services are invoked or need to have tight control over ordering of events, we should use Messages.
I hope this short article helped you to deep dive in EDA.
For more details, please refer to this amazing website — https://www.developertoarchitect.com/
For any questions or suggestions, please comment below!