Event-driven architecture may seem like an abstract concept, but its foundation is very simple. Let’s consider a simple example. You’re at a stoplight. The light turns green (the event), so you drive forward (the reaction).
Until the traffic light signaled it was okay to proceed, you did not react. Your reaction is dictated by logic: traffic laws, your destination, traffic conditions ahead of your car. This is the essence of an event-driven system.
So, what is event-driven architecture? To answer this question, we must first understand events.
- What is event-driven architecture?
- Event-Driven Architecture Use Cases
- Event-Driven Architecture Patterns
- Benefits of Event-Driven Architecture
- Challenges of Event-Driven Architecture
- Event-driven architecture powers agile businesses.
What does it mean to be event driven?
Events are actions that occur within or outside an organization, such as customer purchases, inventory updates, security breaches, or application state changes.
Events drive reactions within organizations, so companies build their infrastructure around capturing and reacting to these time-sensitive or mission-critical events. In the digital age, IT systems record, process, and respond to events based on preset logic.
Events in system design share common characteristics:
- They are a record that something has happened.
- They are immutable — they can’t be changed or deleted.
- They can be persisted indefinitely — events can be stored and accessed forever.
- They can be consumed an unlimited number of times — there is no limit to the number of times an event can be processed by a service.
Events in event-driven architectures are notifications confirming an action, which can be a simple confirmation or include additional data for downstream systems.
For example, a purchase notification can include the customer’s ID, product ID, purchase total, purchase date and time, and other metrics that a CRM uses to create a profile of the customer and predict future products they might need.
Because it’s the event notification or message that’s being handled in the system, event-driven architecture is sometimes referred to as message-driven architecture.
What is event-driven architecture?
Event-driven architecture (EDA) is a system design practice built to record, transmit, and process events through a decoupled architecture. This means that systems don’t need to know about each other to share information and accomplish tasks.
Decoupling is a key advantage of EDA over other data-sharing methods like APIs. With APIs, applications need to know dependencies and parameters, send a properly formatted request, and wait for a response. This creates a request-response model for data transfer.
The event router acts like a WiFi router, distributing data in real-time to the devices that need it. Consumers receive the event to process without requesting it, and services work independently, processing events asynchronously.
The flow of event data between the event producers, event router, and event consumers is known as an event channel.
This video from A Dev Story reviews event-driven architecture and its benefits and challenges (which are covered in later sections):
Event-Driven Architecture Use Cases
Organizations leverage EDA when designing their systems to achieve many different use cases. The following are the most common:
- Data replication: One event can be shared between multiple services that need to copy its data into their databases.
- Parallel processing: Multiple processes can be triggered by one event to execute asynchronously of each other.
- Real-time monitoring: Systems can generate events for changes to their state so an organization can scan for anomalies and suspicious activities.
- Interoperability: Events can be persisted and propagated regardless of the code services are written in.
- Redundancy: If a service is down, events can be persisted in the router until the service is available to consume the event.
- Microservices: EDA is commonly paired with microservices to efficiently share information between decoupled systems at scale.
Now let’s look at an example of EDA in action.
Event-Driven Architecture Example
To review, events are actions, and EDA is how an organization defines the reaction to the event.
For our example, let’s look at how an online store might adopt an event-driven architecture. On an eCommerce site, a customer submitting payment information is a common and critical action. The UI generates the “Payment Submitted” event with credit card details, and the event router knows to route the notification to the payment system based on the event’s metadata.
Once the payment system processes the event, the event router updates the website UI with the status of the payment. If the payment fails, the UI allows the customer to update the payment details. If the payment is successful, the UI displays the final order details and expected delivery date.
The architecture works in this example because the event router is configured to facilitate the flow of information. Neither the front-end website nor the back-end services know their counterparts exist. They are simply subscribed to “Payment Submitted” and “Payment Processed” events, and the event router maintains these event channels.
This video provides another example of EDA in action for a healthcare system and uses this scenario to review EDA’s components:
Event-Driven Architecture Patterns
An EDA pattern designates how event producers, routers, and consumers work together. There are two common patterns in EDA architecture: publisher/subscriber and event streaming. The following sections expand on both these designs.
In a publisher/subscriber architecture (sometimes shortened to pub/sub), the event router tracks subscriptions from consumers to event channels. When an event is published, the router ensures the subscriber receives it.
Events can’t be replayed, so if a subscriber joins an event stream after an event is generated, they can’t retroactively access it. The consumer will receive new events from that point forward.
Event Streaming Architecture
Event streaming moves past the subscription-based pattern. In this approach, events are written to a log, and all consumers can read the log to identify relevant events. Consumers can read from any part of the stream, so they can replay past events.
Event Processing Patterns
In addition to different models, there are also unique approaches to processing the events once they reach a consumer.
- Simple event processing: Consumers process each event as it is received, such as in the previous eCommerce site example.
- Complex event processing: Consumers process a series of events to detect patterns in the event data.
- Event stream processing: Data streaming platforms ingest events and create a pipeline to stream processors that transform and consume the data.
This video from CodeOpinion reviews some advanced EDA models:
Benefits of Event-Driven Architecture
EDA offers many benefits for organizations seeking to improve the responsiveness and flexibility of their tech stack.
Services no longer need to know other services exist to work together. They only need to know about events, and the broker delegates the right events to the right systems. Decoupling makes EDA a natural fit for connecting microservices.
Events can’t be changed once created, so they can be shared with multiple services without the risk of one service modifying or removing information that other services would then consume.
In traditional models, if Service 2 is down, then Service 1’s request to Service 2 will bounce, causing errors. With events, the broker can persist the event until Service 2 is back online to receive the event. This avoids a single point of failure and improves durability.
One event triggers multiple actions in different consumers, improving performance. Decoupling simplifies the process of adding, updating, or removing event producers and consumers, allowing for quick adjustments to meet new requirements or demand.
EDA executes as events occur, supporting real-time or near real-time responses to mission-critical interactions like purchases and live chat messages. This agility ensures that customers are always provided with the support and information as soon as they need it.
The event router’s role as middleware for the flow of information simplifies the auditing and compliance process. You can set permissions for data access from this central point for the entire organization. Plus, because events are immutable, their data can never be modified or removed, creating a reliable audit trail.
Challenges of Event-Driven Architecture
Though EDA offers many advantages, some of these gains come with tradeoffs, including the following.
The middle man between producers and consumers in EDA may cause performance decreases, resulting in longer execution times for consumers.
There is a tradeoff between transmitting events with all the data and sending slimmer notifications that require consumers to query for additional data. This can result in longer event transmission times and the need for additional logic to ensure consumers obtain any missing data.
Two services may process and react to an event at different times based on how long it takes to execute their tasks and other factors. This means that not all reactions to an event will occur at the same time, but eventually all services will have finished processing the event. This effect is known as eventual consistency.
Decoupling eliminates the need for dependency tracking and other obstacles that hinder microservices architecture. However, the tradeoff is that it becomes more challenging to track event statuses between multiple applications compared to direct dependencies with clearly defined relationships.
Event-driven architecture powers agile businesses.
EDA is a powerful approach to system design that introduces new efficiencies and flexibility for organizations looking to quickly address the events that matter to their business. It enables parallel execution, data replication, interoperability, and other use cases with ease. EDA is a central method for achieving an event-driven organization where actions trigger immediate responses, increasing agility and scaling to meet customer needs.