Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 65 ยท Asynchronous Systems

Event-Driven Architecture

Learn event-driven architecture: events versus commands, choreography versus orchestration, event types, the outbox pattern, and when request-driven design is better.

In a request-driven design, one service calls another and waits for the answer: "reserve stock", "charge card", "send email". In an event-driven architecture (EDA), a service instead announces that something happened, such as OrderPlaced, and any service that cares reacts on its own. The announcer does not know who listens, and does not wait.

This style powers e-commerce checkouts, ride matching, IoT pipelines and many microservice systems. It makes services loosely coupled and easy to extend, and it makes flow harder to follow. This lesson explains the vocabulary, the two ways of coordinating a workflow and the traps to avoid.

Events, commands and queries

Three kinds of messages look similar but mean different things:

  • A command tells one service to do something: ChargeCard. It is addressed, and it can fail or be refused.
  • A query asks for data: GetOrder. It changes nothing.
  • An event states a fact in the past tense: PaymentCaptured. It cannot be refused, because it already happened. Anyone may react.

Name events as past-tense facts about your own domain (OrderShipped), not as instructions for others (SendShippingEmail). The producer should not care what happens next.

Event shapes

There are two common ways to put data in an event:

  • Event notification: small, only identifies what happened ({"type": "OrderPlaced", "orderId": 42}). Consumers call back for details. Payloads stay small, but consumers depend on the producer's API.
  • Event-carried state transfer: the event contains the data consumers need (customer, items, total). Consumers need no callback and keep local copies, but events are bigger and the schema is a shared contract.
Python A tiny event bus with independent reactions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

To add a fraud check, you add a new subscriber. The order service does not change. That is the extensibility gain.

Choreography versus orchestration

When a business process spans several services, someone has to coordinate. There are two styles.

Choreography: there is no central controller. Each service reacts to events and emits new ones. Order placed, so stock is reserved; stock reserved, so payment starts; payment captured, so shipping starts. It is loosely coupled, but the overall flow exists only implicitly across many handlers, so it is hard to see, debug or change.

Orchestration: a central orchestrator (a workflow engine or a service) tells each participant what to do and tracks progress. The flow lives in one place, which makes it easier to understand and to handle failures, at the cost of a coordinating component that knows about everyone.

flowchart TB
  subgraph Choreography
    O1[Order] -->|OrderPlaced| S1[Stock]
    S1 -->|StockReserved| P1[Payment]
    P1 -->|PaymentCaptured| H1[Shipping]
  end
  subgraph Orchestration
    C[Orchestrator] -->|reserve| S2[Stock]
    C -->|charge| P2[Payment]
    C -->|ship| H2[Shipping]
  end

Choreography: services pass events along. Orchestration: one coordinator drives the steps.

Rule of thumb: choreography for simple, few-step reactions; orchestration when a flow has many steps, branches or needs compensation (see Saga Pattern).

The dual-write problem and the outbox

A service often must update its database and publish an event. Doing both separately is dangerous: if the database commit succeeds and the publish fails, the world never learns of the change; reverse the order and you announce something that never happened. Databases and brokers cannot share one transaction.

The transactional outbox pattern solves it. In the same database transaction as the business change, insert the event into an outbox table. A separate relay process reads unsent rows and publishes them, marking them sent. The event is never lost, though it may be published twice if the relay crashes after sending, so consumers must be idempotent.

Python Outbox: change and event commit together

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Strengths and costs

Strengths:

  • Loose coupling and extensibility: add consumers without touching producers.
  • Resilience: consumers can be down and catch up later.
  • Scalability: consumers scale independently.
  • Real-time reactions: natural for notifications, fraud, IoT and analytics.

Costs:

  • Hard to trace: "what happens when an order is placed?" has no single answer. Invest in distributed tracing with correlation IDs carried in every event.
  • Eventual consistency: state is spread over services and catches up at different times.
  • Duplicates and ordering: handle with idempotency and keys.
  • Schema contracts: events are public APIs; version them carefully.
  • Debugging and testing are harder than for a call stack.

Do not turn every call into an event. If a user needs an immediate answer ("is this password correct?"), a request-response call is simpler and clearer. Many good systems combine both: synchronous APIs at the edge, events between services.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is a well-named event?

    Events are past-tense facts about what happened. The others are commands addressed to a specific handler.
  2. 2. What is the main downside of choreography?

    With no coordinator, the process exists only as a chain of reactions across services, which makes it hard to see and debug.
  3. 3. What problem does the transactional outbox solve?

    The event is written in the same database transaction as the change, then a relay publishes it, so neither is lost without the other.
  4. 4. Event-carried state transfer means:

    Consumers can keep local copies from the event payload, at the cost of larger events and a shared schema.

Summary

  • EDA lets services announce facts (events) that others react to without being called.
  • Commands are addressed requests; events are past-tense facts anyone may consume.
  • Choreography is decentralized and loosely coupled; orchestration centralizes the flow for clarity.
  • The outbox pattern avoids dual-write bugs; consumers must still be idempotent.
  • EDA improves extensibility and resilience but costs traceability and consistency: mix it with plain requests.

Next: Event Sourcing and Streaming