Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 57 ยท Architectural and Modern Patterns

Event-Driven Architecture

Learn event-driven architecture: producers, consumers and brokers, pub/sub versus queues, event notification versus state transfer, and the limits to plan for.

In a request-driven design, one component calls another and waits: the order service calls the payment service, then the inventory service, then the email service. It works, but the order service must know every one of them, and a slow or broken email service now slows or breaks order placement.

In an event-driven architecture (EDA), the order service simply announces OrderPlaced and moves on. Whoever cares reacts. The payment, inventory, email and analytics components each subscribe on their own. Adding a fifth reaction means adding a fifth subscriber; the order service is not edited. This is the Observer pattern scaled up from objects in one process to services across a system, with a broker in the middle.

The building blocks

flowchart LR
    P1["Producer:<br/>Order service"] -->|"OrderPlaced"| B(("Broker"))
    B --> C1["Consumer:<br/>Email"]
    B --> C2["Consumer:<br/>Inventory"]
    B --> C3["Consumer:<br/>Analytics"]

A producer publishes an event to a broker, which delivers it to every interested consumer.

  • Event: an immutable record that something happened, named in the past tense (OrderPlaced, PasswordChanged). It carries an id, a time and enough data to act on.
  • Producer (publisher): creates events. It does not know who listens.
  • Consumer (subscriber): reacts to events. It does not know who produced them.
  • Broker (event bus, message broker): receives events and delivers them. Real ones include Kafka, RabbitMQ, cloud queues and topics, and Redis streams; in a single program it can be a plain object.

The central benefit is decoupling in three directions: producers and consumers do not know each other (design coupling), they do not need to be running at the same moment (time coupling), and they can scale independently.

Pub/sub versus a queue

Two delivery styles are used all the time, and mixing them up causes real bugs.

  • Publish/subscribe (topic): every subscriber gets its own copy of each event. Use it to announce facts to many interested parties.
  • Queue (work queue): each message is delivered to one of several competing workers. Use it to share a pile of jobs, such as "send this email", across a pool.
Node.js Pub/sub versus a work queue

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 node:readline or process.stdin), it gets the next line from this box.

Real brokers combine both: Kafka consumer groups and RabbitMQ exchanges let each service get a copy of an event while the instances of that service share the work.

Event notification versus event-carried state

How much data goes inside an event is a design choice.

  • Event notification: a thin event, such as { type: "CustomerMoved", customerId: 7 }. Consumers call back to the source for details. The event is small and the source stays the only owner of the data, but every consumer adds load and a dependency on the source being up.
  • Event-carried state transfer: a fat event with the new data ({ customerId: 7, newAddress: {...} }). Consumers keep a local copy and never call back. Services are more independent, at the cost of larger events and copies that must be kept consistent.

Choose notification when the data is large, sensitive or changes often. Choose state transfer when consumers need the data while the source might be down.

Processing styles

  • Simple event processing: one event triggers one action ("on UserRegistered, send a welcome mail").
  • Event stream processing: continuous operations over a flow of events, such as a moving average or alert when a sensor passes a threshold.
  • Complex event processing: detecting a pattern across several events ("three failed logins from different countries within five minutes").
  • Choreography versus orchestration: in choreography each service reacts to events and emits new ones, with no central controller. In orchestration one coordinator tells services what to do. Choreography is flexible but the overall flow is spread across services and hard to see; orchestration makes the flow visible in one place but adds a central component.

Reliability: what a broker really promises

Delivery guarantees decide how consumers must be written.

  • At most once: an event may be lost, never repeated.
  • At least once: an event is never lost but may arrive more than once. This is the common default.
  • Exactly once: usually achieved by at-least-once delivery plus consumers that ignore duplicates.

So make consumers idempotent: handling the same event twice must have the same result as handling it once. The usual trick is to remember processed event ids.

Node.js An idempotent consumer with retries

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 node:readline or process.stdin), it gets the next line from this box.

The first delivery failed once and was retried successfully; the duplicate was recognized by its id, so the customer was charged exactly 35 in total. A dead-letter queue collects events that keep failing so a person can inspect them instead of blocking everything behind them.

Benefits and limits

Benefits: loose coupling, independent scaling and deployment, resilience (a slow consumer does not slow the producer), easy addition of new reactions, and a natural audit stream.

Limits you should plan for:

  • Hard to follow. "What happens when an order is placed?" has no single place to read the answer. Document flows and use tracing with a correlation id on every event.
  • Eventual consistency. Consumers act a moment later; screens may show stale data (see CQRS).
  • Ordering and duplicates. Order is usually guaranteed only within a partition or stream; handle out-of-order events.
  • Debugging and testing across services is harder than stepping through one call stack.
  • Schema evolution. Events are contracts; change them with versioning, and add fields rather than renaming.
  • Operational cost of running and monitoring a broker.

Start with synchronous calls where the caller truly needs the answer now (checking a password), and use events for "this happened, whoever cares can react". Event Sourcing goes one step further by making the events the stored truth.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main advantage of event-driven design over direct service calls?

    The producer only announces what happened. New consumers can be added without changing it, and they can run independently.
  2. 2. Four jobs are sent to a work queue with two workers. How many workers process each job?

    A queue shares jobs among competing workers. A topic is the one that gives every subscriber its own copy.
  3. 3. Why should consumers be idempotent?

    With at-least-once delivery a retry or network glitch can deliver the same event twice, so handling it again must not repeat the effect.
  4. 4. Which is an example of event-carried state transfer?

    Event-carried state transfer puts the changed data in the event, letting consumers keep local copies and work while the source is down.
  5. 5. What is a dead-letter queue for?

    After repeated failures an event is parked in the dead-letter queue, so a person or tool can investigate while healthy events continue.

Summary

  • Event-driven architecture lets producers announce facts and consumers react through a broker, decoupling them in design, time and scale.
  • A topic gives every subscriber a copy; a queue gives each job to one worker.
  • Events can be thin notifications or carry state; pick by data size, sensitivity and availability needs.
  • Expect at-least-once delivery, so write idempotent consumers and use dead-letter queues and retries.
  • The costs are traceability, eventual consistency, ordering questions and operations; use events where "react later" fits.

Next lesson: Circuit Breaker, Retry and Bulkhead Patterns.