Skip to content

Commit f52f71c

Browse files
authored
docs: Event-driven architecture explanation (iluwatar#2917)
* update readme * convert to record
1 parent 730907d commit f52f71c

File tree

4 files changed

+97
-16
lines changed

4 files changed

+97
-16
lines changed

event-driven-architecture/README.md

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,119 @@
11
---
2-
title: Event Driven Architecture
2+
title: Event-Driven Architecture
33
category: Architectural
44
language: en
55
tag:
6-
- Reactive
6+
- Asynchronous
7+
- Decoupling
8+
- Enterprise patterns
9+
- Event-driven
10+
- Messaging
11+
- Publish/subscribe
12+
- Reactive
13+
- Scalability
714
---
815

16+
## Also known as
17+
18+
* Event-Driven System
19+
* Event-Based Architecture
20+
921
## Intent
10-
Send and notify state changes of your objects to other applications using an Event-driven Architecture.
22+
23+
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.
24+
25+
## Explanation
26+
27+
### Real-world example
28+
29+
> 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.
30+
31+
### In plain words
32+
33+
> 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.
34+
35+
### Wikipedia says
36+
37+
> Event-driven architecture (EDA) is a software architecture paradigm concerning the production and detection of events.
38+
39+
### Programmatic Example
40+
41+
The Event-Driven Architecture (EDA) pattern in this module is implemented using several key classes and concepts:
42+
43+
* 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.
44+
* 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.
45+
* EventDispatcher: This class is responsible for dispatching events to their respective handlers. It maintains a mapping of event types to handlers.
46+
* UserCreatedEventHandler and UserUpdatedEventHandler: These are the handler classes for the UserCreatedEvent and UserUpdatedEvent respectively. They contain the logic to execute when these events occur.
47+
48+
Here's a simplified code example of how these classes interact:
49+
50+
```java
51+
// Create an EventDispatcher
52+
EventDispatcher dispatcher = new EventDispatcher();
53+
54+
// Register handlers for UserCreatedEvent and UserUpdatedEvent
55+
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
56+
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
57+
58+
// Create a User
59+
User user = new User("iluwatar");
60+
61+
// Dispatch UserCreatedEvent
62+
dispatcher.dispatch(new UserCreatedEvent(user));
63+
64+
// Dispatch UserUpdatedEvent
65+
dispatcher.dispatch(new UserUpdatedEvent(user));
66+
```
67+
68+
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.
1169

1270
## Class diagram
13-
![alt text](./etc/eda.png "Event Driven Architecture")
71+
72+
![Event-Driven Architecture](./etc/eda.png "Event-Driven Architecture")
1473

1574
## Applicability
75+
1676
Use an Event-driven architecture when
1777

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

22-
## Real world examples
83+
## Known Uses
2384

85+
* Real-time data processing applications.
86+
* Complex event processing systems in finance, such as stock trading platforms.
87+
* IoT systems for dynamic device and information management.
2488
* Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events)
2589
* 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)
2690
* MySQL runs triggers based on events such as inserts and update events happening on database tables.
2791

92+
## Consequences
93+
94+
Benefits:
95+
96+
* Scalability: Efficiently processes fluctuating loads with asynchronous processing.
97+
* Flexibility and Agility: New event types and event consumers can be added with minimal impact on existing components.
98+
* Responsiveness: Improves responsiveness by decoupling event processing and state management.
99+
100+
Trade-offs:
101+
102+
* Complexity in Tracking: Can be challenging to debug and track due to loose coupling and asynchronous behaviors.
103+
* Dependency on Messaging Systems: Heavily relies on robust messaging infrastructures.
104+
* Event Consistency: Requires careful design to handle event ordering and consistency.
105+
106+
## Related Patterns
107+
108+
* Microservices Architecture: Often used together with EDA to enhance agility and scalability.
109+
* Publish/Subscribe: A common pattern used within EDA for messaging between event producers and consumers.
110+
28111
## Credits
29112

30113
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
31114
* [What is an Event-Driven Architecture](https://aws.amazon.com/event-driven-architecture/)
32115
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
33116
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
117+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3Q3vBki)
118+
* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/49Aljz0)
119+
* [Reactive Messaging Patterns With the Actor Model: Applications and Integration in Scala and Akka](https://amzn.to/3UeoBUa)

event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserCreatedEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class UserCreatedEventHandler implements Handler<UserCreatedEvent> {
3636

3737
@Override
3838
public void onEvent(UserCreatedEvent event) {
39-
LOGGER.info("User '{}' has been Created!", event.getUser().getUsername());
39+
LOGGER.info("User '{}' has been Created!", event.getUser().username());
4040
}
4141

4242
}

event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserUpdatedEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public class UserUpdatedEventHandler implements Handler<UserUpdatedEvent> {
3636

3737
@Override
3838
public void onEvent(UserUpdatedEvent event) {
39-
LOGGER.info("User '{}' has been Updated!", event.getUser().getUsername());
39+
LOGGER.info("User '{}' has been Updated!", event.getUser().username());
4040
}
4141
}

event-driven-architecture/src/main/java/com/iluwatar/eda/model/User.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,4 @@
3333
* This {@link User} class is a basic pojo used to demonstrate user data sent along with the {@link
3434
* UserCreatedEvent} and {@link UserUpdatedEvent} events.
3535
*/
36-
@RequiredArgsConstructor
37-
@Getter
38-
public class User {
39-
40-
private final String username;
41-
}
36+
public record User(String username) {}

0 commit comments

Comments
 (0)