Event-driven architecture

Announcing what happened · Part of Architecture

Key idea

Practice

7 questions · about 4 min

Explained step by step

Event-driven architecture

Event-driven architecture (EDA) uses events to connect the parts of a system, almost always through a message broker that publishes and delivers asynchronously. The publisher doesn't know who consumes the event, and consumers don't know each other. It's a way to achieve loose coupling between services. It's like a school bell: it rings 1 time and each of the 500 students knows what to do, without the bell knowing them.

EDA can be implemented in several ways, depending on the case: sagas (for transactions spread across services), pub/sub (to broadcast events), event sourcing (storing events as state) and CQRS (separating reads from writes). They aren't exclusive: one system can use 2 or 3. It's like the ways to get somewhere: car, bus or bike; all 3 get you there, each fits a case.

EDA has advantages: producers and consumers are decoupled, the system is highly scalable and distributed, adding new consumers is easy and teams move faster. Adding a consumer doesn't force touching the producer: it subscribes and that's it. It's like a YouTube channel: adding subscriber number 1,001 changes nothing for the creator.

EDA also has challenges: guaranteeing each event is delivered, handling errors (what if 1 of 5 consumers fails?), the general complexity of the system and processing each event exactly once and in order. It's like a rumor in an office of 100 people: it reaches everyone fast, but it's hard to know if each heard it 1 time and in the right order.

EDA fits several cases: metrics, server and security logs, integrating different systems (for example, one in Java and one in Python) and fanout for parallel processing. Some technologies used are NATS, Apache Kafka, Amazon EventBridge, Amazon SNS and Google PubSub. It's like a building's alarm system: 1 sensor detects smoke and alerts the firefighters, the doorman and all 40 apartments at once.

Event

An event is a piece of data representing a state change in the system. It doesn't say what should happen or how; it only notifies that something changed. When a user takes an action, they trigger an event. For example, «order 1234 created» or «$50 payment approved». It's like a newspaper headline: it tells what happened, and each reader decides what to do with it.

Event router

EDA has 3 components. Event producers publish each event to the router. The event router filters them and pushes them to the right consumers. Consumers use the events to reflect the changes in their part of the system. It's like a company's internal mail: someone drops an envelope (producer), the mail room sorts it (router) and carries it to the 3 areas that need it (consumers).

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Announcing what happened