Skip to content

Commit

Permalink
docs: Event-driven architecture explanation (iluwatar#2917)
Browse files Browse the repository at this point in the history
* update readme

* convert to record
  • Loading branch information
iluwatar authored Apr 14, 2024
1 parent 730907d commit f52f71c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
102 changes: 94 additions & 8 deletions event-driven-architecture/README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,119 @@
---
title: Event Driven Architecture
title: Event-Driven Architecture
category: Architectural
language: en
tag:
- Reactive
- Asynchronous
- Decoupling
- Enterprise patterns
- Event-driven
- Messaging
- Publish/subscribe
- Reactive
- Scalability
---

## Also known as

* Event-Driven System
* Event-Based Architecture

## Intent
Send and notify state changes of your objects to other applications using an Event-driven Architecture.

Event-Driven Architecture (EDA) is designed to orchestrate behavior around the production, detection, consumption of, and reaction to events. This architecture enables highly decoupled, scalable, and dynamic interconnections between event producers and consumers.

## Explanation

### Real-world example

> A real-world example of the Event-Driven Architecture (EDA) pattern is the operation of an air traffic control system. In this system, events such as aircraft entering airspace, changes in weather conditions, and ground vehicle movements trigger specific responses like altering flight paths, scheduling gate assignments, and updating runway usage. This setup allows for highly efficient, responsive, and safe management of airport operations, reflecting EDA's core principles of asynchronous communication and dynamic event handling.
### In plain words

> Event-Driven Architecture is a design pattern where system behavior is dictated by the occurrence of specific events, allowing for dynamic, efficient, and decoupled responses.
### Wikipedia says

> Event-driven architecture (EDA) is a software architecture paradigm concerning the production and detection of events.
### Programmatic Example

The Event-Driven Architecture (EDA) pattern in this module is implemented using several key classes and concepts:

* Event: This is an abstract class that represents an event. It's the base class for all types of events that can occur in the system.
* UserCreatedEvent and UserUpdatedEvent: These are concrete classes that extend the Event class. They represent specific types of events that can occur in the system, namely the creation and updating of a user.
* EventDispatcher: This class is responsible for dispatching events to their respective handlers. It maintains a mapping of event types to handlers.
* UserCreatedEventHandler and UserUpdatedEventHandler: These are the handler classes for the UserCreatedEvent and UserUpdatedEvent respectively. They contain the logic to execute when these events occur.

Here's a simplified code example of how these classes interact:

```java
// Create an EventDispatcher
EventDispatcher dispatcher = new EventDispatcher();

// Register handlers for UserCreatedEvent and UserUpdatedEvent
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());

// Create a User
User user = new User("iluwatar");

// Dispatch UserCreatedEvent
dispatcher.dispatch(new UserCreatedEvent(user));

// Dispatch UserUpdatedEvent
dispatcher.dispatch(new UserUpdatedEvent(user));
```

In this example, the EventDispatcher is created and handlers for UserCreatedEvent and UserUpdatedEvent are registered. Then a User is created and UserCreatedEvent and UserUpdatedEvent are dispatched. When these events are dispatched, the EventDispatcher calls the appropriate handler to handle the event. This is a basic example of an Event-Driven Architecture, where the occurrence of events drives the flow of the program. The system is designed to respond to events as they occur, which allows for a high degree of flexibility and decoupling between components.

## Class diagram
![alt text](./etc/eda.png "Event Driven Architecture")

![Event-Driven Architecture](./etc/eda.png "Event-Driven Architecture")

## Applicability

Use an Event-driven architecture when

* you want to create a loosely coupled system
* you want to build a more responsive system
* you want a system that is easier to extend
* Systems where change detection is crucial.
* Applications that require real-time features and reactive systems.
* Systems needing to efficiently handle high throughput and sporadic loads.
* When integrating with microservices to enhance agility and scalability.

## Real world examples
## Known Uses

* Real-time data processing applications.
* Complex event processing systems in finance, such as stock trading platforms.
* IoT systems for dynamic device and information management.
* Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events)
* Amazon's AWS Lambda, lets you execute code in response to events such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices. (https://aws.amazon.com/lambda)
* MySQL runs triggers based on events such as inserts and update events happening on database tables.

## Consequences

Benefits:

* Scalability: Efficiently processes fluctuating loads with asynchronous processing.
* Flexibility and Agility: New event types and event consumers can be added with minimal impact on existing components.
* Responsiveness: Improves responsiveness by decoupling event processing and state management.

Trade-offs:

* Complexity in Tracking: Can be challenging to debug and track due to loose coupling and asynchronous behaviors.
* Dependency on Messaging Systems: Heavily relies on robust messaging infrastructures.
* Event Consistency: Requires careful design to handle event ordering and consistency.

## Related Patterns

* Microservices Architecture: Often used together with EDA to enhance agility and scalability.
* Publish/Subscribe: A common pattern used within EDA for messaging between event producers and consumers.

## Credits

* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
* [What is an Event-Driven Architecture](https://aws.amazon.com/event-driven-architecture/)
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
* [Patterns of Enterprise Application Architecture](https://amzn.to/3Q3vBki)
* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/49Aljz0)
* [Reactive Messaging Patterns With the Actor Model: Applications and Integration in Scala and Akka](https://amzn.to/3UeoBUa)
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class UserCreatedEventHandler implements Handler<UserCreatedEvent> {

@Override
public void onEvent(UserCreatedEvent event) {
LOGGER.info("User '{}' has been Created!", event.getUser().getUsername());
LOGGER.info("User '{}' has been Created!", event.getUser().username());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public class UserUpdatedEventHandler implements Handler<UserUpdatedEvent> {

@Override
public void onEvent(UserUpdatedEvent event) {
LOGGER.info("User '{}' has been Updated!", event.getUser().getUsername());
LOGGER.info("User '{}' has been Updated!", event.getUser().username());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,4 @@
* This {@link User} class is a basic pojo used to demonstrate user data sent along with the {@link
* UserCreatedEvent} and {@link UserUpdatedEvent} events.
*/
@RequiredArgsConstructor
@Getter
public class User {

private final String username;
}
public record User(String username) {}

0 comments on commit f52f71c

Please sign in to comment.