|
4 | 4 |
|
5 | 5 | ### Event-driven architectures
|
6 | 6 |
|
7 |
| -An Event-Driven Architecture (EDA) uses events to trigger and communicate between services and is common in modern applications built with microservices. An event is a change in state, or an update, like adding a shopping item in a cart on an e-commerce website. |
| 7 | +#### What is an event-driven architecture |
| 8 | + |
| 9 | +Event-Driven Architectures (EDAs) are a paradigm that promotes the production, consumption and reaction to events. |
| 10 | + |
| 11 | +This architectural pattern may be applied by the design and implementation of applications and systems that transmit events amongst loosely coupled software components and services. |
| 12 | + |
| 13 | +An event-driven system typically consists of event emitters (or agents), event consumers (or sinks), and event channels. |
| 14 | + |
| 15 | +- Producers (or publishers) are responsible for detecting, gathering and transferring events |
| 16 | + - Are not aware of consumers |
| 17 | + - Are not aware of how the events are consumed |
| 18 | +- Consumers (or subscribers) react to the events as soon as they are produced |
| 19 | + - The reaction can be self-contained or it can be a composition of processes or components |
| 20 | +- Event channels are conduits in which events are transmited from emitters to consumers |
| 21 | + |
| 22 | +**Note** Producer and Consumer role is not exclusive. In other words, the same client or application can be producer and consumer at the same time. |
| 23 | + |
| 24 | + |
| 25 | +In most cases, EDAs are broker-centric, as seen in the diagram below. |
8 | 26 |
|
9 | 27 | 
|
10 | 28 |
|
11 |
| -In most cases, EDAs are broker-centric, as seen in the diagram above. There are some new concepts in that diagram, so let's go through them now. |
| 29 | +#### Problem statement |
| 30 | + |
| 31 | +Typically, the architectural landscape of a big company grows in complexity and as a result of that it is possible to end up with a bunch of direct connections between a myriad of different components or modules. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +By using streaming patterns, it is possible to get a much cleaner architecture |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +It is important to take into account that EDAs are not a silver bullet, and there are situations in which this kind of architectures might not fit very well. |
| 41 | + |
| 42 | +One example is systems that heavily rely on transactional operations... of course it might be possible to use EDA but most probably the complexity of the resulting architecture would be too high. |
| 43 | + |
| 44 | +Also, it is important to note that it is possible to mix request-driven and event-driven protocols in the same system. For example, |
| 45 | + |
| 46 | +- Online services that interact directly with a user fits better into the synchronous communication but they also can generate events into Kafka. |
| 47 | +- On the other hand, offline services (billing, fulfillment, etc) are typically built purely with events. |
| 48 | + |
| 49 | +#### Kafka as the heard of EDAs |
| 50 | + |
| 51 | +There are several technologies to implement event-driven architectures, but this section is going to focus on the predominant technology on this subject : Apache Kafka. |
| 52 | + |
| 53 | +**Apache Kafka** can be considered as a Streaming Platform which relies on the several concepts: |
| 54 | + |
| 55 | +- Super high-performance, scalable, highly-available cluster of brokers |
| 56 | + - Availability |
| 57 | + - Replication of partitions across different brokers |
| 58 | + - Scalability |
| 59 | + - Partitions |
| 60 | + - Ability to rebalance partitions across consumers automatically when adding/removing them |
| 61 | + - Performance |
| 62 | + - Partitioned, replayable log (collection of messages appended sequentially to a file) |
| 63 | + - Data copied directly from disk buffer to network buffer (zero copy) without even being imported to the JVM |
| 64 | + - Extreme throughput by using the concept of consumer group and |
| 65 | + - Security |
| 66 | + - Secure encrypted connections using TLS client certificates |
| 67 | + - Multi-tenant management through quotas/acls |
| 68 | + - Client APIs on different programming languages : Go, Scala, Python, REST, JAVA, ... |
| 69 | + - Stream processing APIs (currently Kafka Streams and ksqlDB) |
| 70 | + - Ecosystem of connectors to pull/push data from/to Kafka |
| 71 | + - Clean-up processes for storage optimization |
| 72 | + - Retention periods |
| 73 | + - Compacted topics |
| 74 | + |
| 75 | +### Basic terminology |
| 76 | + |
| 77 | +#### Events |
| 78 | + |
| 79 | +An event is both a fact and a notification, something that already happened in the real world. |
| 80 | + |
| 81 | +- No expectation on any future action |
| 82 | +- Includes information about a status change that just happened |
| 83 | +- Travels in one direction and it never expects a response (fire and forget) |
| 84 | +- Very useful when... |
| 85 | + - Loose coupling is important |
| 86 | + - When the same piece of information is used by several services |
| 87 | + - When data needs to be replicated across application |
| 88 | + |
| 89 | +A message in general is any interaction between an emitter and a receiver to exchange information. This implies that any event can be considered a messages but not the other way around. |
| 90 | + |
| 91 | +#### Commands |
| 92 | + |
| 93 | +A command is a special type of message which represents just an action, something that will change the state of a given system. |
| 94 | + |
| 95 | +- Typically synchronous |
| 96 | +- There is a clear expectation about a state change that needs to take place in the future |
| 97 | +- When returning a response indicate completion |
| 98 | +- Optionally they can include a result in the response |
| 99 | +- Very common to see them in orchestration components |
| 100 | + |
| 101 | +#### Query |
| 102 | + |
| 103 | +It is a special type of message which represents a request to look something up. |
12 | 104 |
|
13 |
| -In Event-Driven Architecture (EDA), an application must be a producer, a consumer, or both. Applications must also use the protocols the server supports if they wish to connect and exchange messages. |
| 105 | +- They are always free of side effects (leaves the system unchanged) |
| 106 | +- They always require a response (with the requested data) |
14 | 107 |
|
15 |
| -### Messages and events |
| 108 | +#### Coupling |
| 109 | + |
| 110 | +The term coupling can be understood as the impact that a change in one component will have on other components. In the end, it is related to the amount of things that a given component shares with others. The more is shared, the more tight is the coupling. |
| 111 | + |
| 112 | +**Note** A tighter coupling is not necessarily a bad thing, it depends on the situation. It will be necessary to assess the tradeoff between provide as much information as possible and to avoid having to change several components as a result of something changing in other component. |
| 113 | + |
| 114 | +The coupling of a single component is actually a function of these factors: |
| 115 | + |
| 116 | +- Information exposed (Interface surface area) |
| 117 | +- Number of users |
| 118 | +- Operational stability and performance |
| 119 | +- Frequency of change |
| 120 | + |
| 121 | +Messaging helps bulding loosely coupled services because it moves pure data from a highly coupled location (the source) and puts it into a loosely coupled location (the subscriber). |
| 122 | + |
| 123 | +Any operations that need to be performed on the data are done in each subscriber and never at the source. This way, messaging technologies (like Kafka) take most of the operational issues off the table. |
| 124 | + |
| 125 | +All business systems in larger organizations need a base level of essential data coupling. In other words, functional couplings are optional, but core data couplings are essential. |
| 126 | + |
| 127 | +#### Bounded context |
| 128 | + |
| 129 | +A bounded context is a small group of services that share the same domain model, are usually deployed together and collaborate closely. |
| 130 | + |
| 131 | +It is possible to put an analogy here with a hierarchic organization inside a company : |
| 132 | + |
| 133 | +- Different departments are loosely coupled |
| 134 | +- Inside departments there will be a lot more interactions across services and the coupling will be tighter |
| 135 | + |
| 136 | +One of the big ideas of Domain-Driven Design (DDD) was to create boundaries around areas of a business domain and model them separately. So within the same bounded context the domain model is shared and everything is available for everyone there. |
| 137 | + |
| 138 | +However, different bounded contexts don't share the same model and if they need to interact they will do it through more restricted interfaces. |
| 139 | + |
| 140 | +#### Stream processing |
| 141 | + |
| 142 | +It can be understood as the capability of processing data directly as it is produced or received (hence, in real-time or near to real-time). |
| 143 | + |
| 144 | +[Review] |
16 | 145 |
|
17 | 146 | A message carries information from one application to another, while an event is a message that provides details of something that has already occurred. One important aspect to note is that depending on the type of information a message contains, it can fall under an event, query, or command.
|
18 | 147 |
|
19 |
| -Overall, events are messages but not all messages are events. |
| 148 | +Overall, events are messages but not all messages are events. |
| 149 | + |
| 150 | +### Using events in an EDA |
| 151 | + |
| 152 | +There are several ways to use events in a EDA: |
| 153 | + |
| 154 | +- Events as notifications |
| 155 | +- Events to replicate data |
| 156 | + |
| 157 | + |
| 158 | +#### Events as notifications |
| 159 | + |
| 160 | +When a system uses events as notifications it becomes a pluggable system. The producers have no knowledge about the consumers and they don't really care about them, instead every consumer can decide if it is interested in the information included in the event. |
| 161 | + |
| 162 | +This way, the number of consumers can be increased (or reduced) without changing anything on the producer side. |
| 163 | + |
| 164 | +This pluggability becomes increasily important as systems get more complex. |
| 165 | + |
| 166 | +#### Events to replicate data |
| 167 | + |
| 168 | +When events are used to replicate data across services, they include all the necessary information for the target system to keep it locally so that it can be queried with no external interactions. |
| 169 | + |
| 170 | +This is usually called event-carried state transfer which in the end is a form of data integration. |
| 171 | + |
| 172 | +The benefits are similar to the ones implied by the usage of a cache system |
| 173 | + |
| 174 | +- Better isolation and autonomy, as the data stays under service's control |
| 175 | +- Faster data access, as the data is local (particularly important when combining data from different services in different geographies) |
| 176 | +- Offline data availability |
0 commit comments