Photo by Matthew Henry from Burst
Before going into the benefits of Event-Driven Architecture (EDA) in the eCommerce world, let’s take some time to understand what EDA is all about.
Event-Driven Architecture (EDA)
Event-Driven Architecture (EDA) combines decoupled components referred to as producers and consumers capable of processing events asynchronously through a mediator called the broker.
Core concepts of Event-Driven Architecture
- The Event
Everything that is part of the Event-Driven Architecture is concentrated on events. The event is the core unit of the EDA. Any process like user creating an account, HTTP request rate-limiting or query to the database can be called an event.
- The event record
An event record for every event needs to be captured, including information like request ID, header, original IP address, query statement, timestamp, event type, etc. The event’s data is formatted in several ways, such as JSON, string etc.
- Producer and Consumer
In Event-Driven Architecture, there will be several services. Producers and consumers act like two primary actors who deal with the events. Producers work when an event occurs, prepare the event record and send it to a managed persistent capturer. On the other end, consumers keep on polling for standard types of events to do the required processing on those events.
Above, we have mentioned that the producer sends the events to a managed persistent capturer. Stream is the organized persistent and sequential event records. The producer will produce the event records to the streams whenever an event occurs. Consumer polls on the stream to watch whether such events are present.
- Message Broker
While producer and consumer are an integral part of the Event-Driven Architecture, we also need a mediator to help the event stream’s persistence and availability. A message broker is responsible for storing and delivering the events to their customer.
Benefits and Tradeoffs of Event-Based Architecture
- The microservices can be deployed and maintained independently since the producer service is unaware of its consumer service and vice versa. It is a prime requirement to develop loosely coupled services.
- Multiple services can consume the same event. Hence, the producer just needs to publish the correct event to the event stream without bothering the consumer.
- Asynchronous nature helps different services to the consumer the events according to their processing capability.
- If a service goes down while the records are getting published by the producer, it can go back and point to the events not yet consumed once the service is up.
- One cannot have a proper understanding of the flow of events serially
- Managing distributed Event-Based Systems could be complex. If an exception occurred in one service, it could be challenging to roll back the process.
Apache Kafka is one of the highly demanded open-source message brokers that enables the application to respond to events.
World of eCommerce and Event-Driven Architecture (EDA)
To meet the high demand of ordering online from eCommerce applications, the real-time responsiveness and highly scalable needs to be modernized and innovative.
Assume someone ordered a few items and is now waiting to be delivered to their address. The impatient customer will keep looking at the door or window while waiting for the delivery. The customer will keep on doing this till the bell rings. We can transform this example into an event-driven system where customers or the service will keep polling for the delivery status.
Use-case to understand the need for Event-Driven architecture
Consider a customer who added multiple items to his basket and then moved forward to the checkout page and filled his address and payment information to place his order on the website. Once the payment for the items ordered is completed, the order is confirmed, and shipment service gets initiated.
To understand how event-driven architecture works, let’s go deep down on the flow from ordering items from the cart to the delivery at the address.
Steps from the checkout of items to delivery of the items
- When the customer check-out the items present in the basket, the first event is to check the inventory for the availability of the items.
- When the items are confirmed, the customer is then forwarded for entering the address and payment details to initiate payment.
- Once the payment is successful, the order service will notify the inventory, trigger a confirmation email for the order and imitate the shipping service to start the shipping process.
- The shipping process includes several stages such as preparation of shipment, dispatching the shipment and delivery of the shipment to the address.
- Once the shipment is delivered, an event is passed to the order service to confirm the delivery. Order service then calls the email service to send a confirmation mail to the user about the delivery.
In the above diagram for an eCommerce application, we can understand the workflow that comes into action through different services and events. Some services will create events while the other services consume the event records. In some scenarios, a service consumes an event and then produces a separate event after performing some action. Events are associated with topics.
No single micro-service owns the process of events. Each service in the Event-Driven System is responsible for a specific task on the events. The services only work on the events they consume and then produce the action results. Due to the loose coupling of the services, you can easily update or replace the services to handle more events without disturbing the workflow.
In this article, we got an overview of how Event-Driven Systems works and what makes Event-Driven Architecture so promising to meet high real-time processing of events with its asynchronous processing and highly decoupled systems. The Event-Driven Systems can help meet the high demand of users and real-time analytics.
Apart from eCommerce, Event-Driven Systems are extensively used in finance, healthcare, supply chain, IoT etc.
Introduction to Event-Driven Architecture was originally published in Walmart Global Tech Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.
Article Link: Introduction to Event-Driven Architecture | by Dinesh Panda | Walmart Global Tech Blog | Mar, 2022 | Medium