What’s the most important part of Event-Driven Architecture?
Tags: architecture, eventdriven, systemdesign, programming
Published: 2024-02-21 06:25:26

Events are the most important part of **Event Driven Architecture**_(EDA)._

In application that has the implementation **EDA,** different components communicate by _sending_ and _receiving events._

**But what exactly is an event?**

In simple terms this is something that has occurred.

However, in detail it is a significant **_occurrence_** or **_happening_** within a system or a domain that is of interest to the participants in the system. Moreover, events can represent a wide range of activities, changes, or notifications that have an impact on the **state** of the system. These events are often used to **_trigger_** or **_communicate_** between different components or services in a decoupled and asynchronous manner.


![eda-pic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twfrums6rnmrcelrhhe7.png)


**Examples:**

- A customer signing up for your application
- A payment is received for an order
- A failed authentication attempt
- A transaction completed
- Account balance updated
- A user logged in
- A post published

There are mainly three different patterns I have come across while dealing with events.

1. - Event Notifications
2. - Event-Based State Transfer
3. - Event Sourcing

**[1] Event Notifications**

In this case, the event just notifies that something has occurred.

The event carries the absolute minimum state. For example, just the ID of an entity like a new order. Minimal data is sent over the network.

In some cases, the receiving components may record the information for auditing purposes. But others may call the source component to fetch additional information about the event.

**_Notification-type events_** are general-purpose in nature and suitable for broadcasting requirements.


![event-notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug0pmxa1vrr40sax0jz2.png)


**[2] Event-Based State Transfer**

This is more like the asynchronous relative of REST.

Instead of REST’s on-demand _**pull**_ model, event-based state transfer follows a **_push_** approach.

Data (id + information) is sent out to be consumed by any components. Some components may create their own local cached copies of the data while others may take some other action based on the data received.

**_The advantage is that the consumer need not call back the source component for additional information._**

The entire information is right there in the event payload.


![event-based-state-transfer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je68j7wgfd4l8daochqu.png)


**[3] Event Sourcing**

This is pattern not about event transmission but event storage.

The state of an application is determined by a sequence of events rather than by the current state of the data. In other words, instead of storing the current state of an entity, you store a series of events that represent state transitions. These events are then used to reconstruct the current state whenever needed.

According to **Martin Fowler,** 

> The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.

Changes to an entity are not captured as irreversible modifications but are stored as events in an event store.

These changes can be read and processed to recreate the final state of an entity whenever required.


![event-sourcing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d7muztb5ghqtd44yuz6.png)


In conclusion, event driven architecture **(EDA)** offers diverse communication patterns like event notifications, state transfer, and event sourcing. As of this, each serve distinct needs in data transmission and storage. More-significantly, selecting the right pattern depends on system complexity and scalability.