Event-driven architecture
An order is placed, and four services react without being called. Seven steps follow one event from producer to consumers, and show what that freedom costs.
In 30 seconds
In an event-driven system, services do not call each other directly. When something happens, a service publishes an event: a small record of a fact, such as “order 7 was placed”.
A broker delivers the event to every service that subscribed. The producer does not know who they are and does not wait for them, so parts can be added, slowed or restarted independently.
The cost: for a moment different parts disagree, and following one action across many services is harder.
Event-driven architecture, step by step
Simplified: one event type, one broker, invented timings. Real brokers differ in how long they keep events and in what order they deliver them, and most can deliver an event more than once.
- Step 1: The direct-call way. A shop’s orders service has work for three others when an order comes in: reserve stock, send a confirmation email, arrange shipping. Calling each one directly means it must know all three, wait for each answer, and cope when one is down. Here shipping is down, so the customer’s order fails.
- Step 2: An event is a fact. An event is a small record of something that has happened: “order 7 was placed at 12:00:03”. It is named in the past tense and never changes afterwards, because the past does not change. It is not a command. It does not tell anyone what to do; it only states a fact that others may care about.
- Step 3: Producers and consumers. The service where the thing happened is the producer. It publishes the event to a broker, a piece of software whose only job is to pass events on. Services that care about this kind of event subscribe to it; they are the consumers. The broker delivers a copy to each one, and each does its own part.
- Step 4: Loose coupling. The producer knows only the broker. It has no idea who is listening, how many there are or what they do with the event. So a new consumer, such as an analytics service that counts sales, can subscribe without one line of the orders service changing. This independence is called loose coupling.
- Step 5: Asynchronous flow. Publishing takes a moment, and then the producer is finished: it answers the customer after 50 milliseconds without waiting for anyone. Consumers work through events at their own pace. If the email service is down, its events wait in the broker and are handled when it comes back, so the order still goes through.
- Step 6: Eventual consistency. Because consumers react a little later, the parts of the system briefly disagree. Right after the order, the orders service says “order 7 placed” while inventory still shows 5 in stock; 0.2 seconds later it shows 4. The system is consistent eventually, not instantly, and everything built on it must tolerate that gap.
- Step 7: Harder to debug. A direct call works or fails where you can see it. With events, one action is spread over several services, each with its own log, and no single place shows the whole path. Events can also arrive late or twice. Finding why order 7 got no email means searching every log for its id, so events carry ids and teams add tracing tools.
Try it yourself
Learn this first
Learn next
- Publish/subscribe patternOne message goes to a topic, and every subscriber gets its own copy. Beginner
- Event sourcingA bank balance rebuilt by replaying the list of everything that happened to it. Intermediate
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
Spotted a mistake on this page?