Understanding event-driven architecture
Event-driven architecture is a software architecture that uses events in order to be able to communicate between decoupled services. It is a pattern for designing applications that are loosely coupled and is used in modern applications built with microservices. When consumers are listening to an event, which could be a status change or an update, event producers are not able to know which event consumers are listening to and do not even know the consequences of its occurrence.
In an event-driven architecture, we have the following three key components:
- Event producers: These generate a stream of events
- Event routers: These manage event delivery between producers and consumers
- Event consumers: These listen to the events
The following diagram illustrates these components:
Figure 1.1 – Event-driven architecture
The source of an event is triggered by internal or external inputs. They can be generated by either a user (by clicking or using keyboard input, for example), an external source (such as a sensor output), or through a system (such as loading a program).
In an event-driven architecture, we can use event streaming or a publisher/subscriber model. But what is the difference between event streaming and a publisher/subscriber model?
- Publisher/subscriber model: This provides a framework that enables message exchanges between publishers and subscribers. This pattern involves the publisher and the subscriber and depends on a message broker that reroutes messages from the publisher to the subscriber.
- Event streaming: When a stream of events is published to a broker, the clients are able to subscribe to the stream and join at any time; they have access to them and can consume multiple preferred streams, and they are able to read from any part and advance their position. The events are always written in a log file.
Event-driven architectures are recommended to improve agility and move quickly. They are used in modern applications, mainly microservices, or in any application that includes several decoupled components. When adopting an event-driven architecture, you may need to rethink how you view your application design.
In this section, we have explored event-driven architecture and the different key components.