Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 77 ยท DevOps, Tools and System Design

Event-Driven Architecture: Event Sourcing, CQRS, Pub/Sub and Streaming

Answer event-driven architecture interview questions: events versus commands, pub/sub versus queues and streams, event sourcing, CQRS, retries and dead-letter queues.

"Event-driven" is one of the most overused phrases in system design, so interviewers like to test whether you can be precise. What is an event? How is publish/subscribe different from a queue? When is event sourcing a good idea, and when does it hurt? Where does CQRS fit, and why does everyone warn you about it?

The lesson Microservices, Kafka and Kubernetes already covers sagas, the transactional outbox, Kafka partitions and consumer groups. Read it first if those terms are new. This lesson covers what is left: the vocabulary, event sourcing, CQRS, and what a consumer must do when messages are late, repeated or poisonous. All runnable blocks are small Node.js models, not real brokers.

Three messaging styles

Queue Each message goes to one worker.
Pub/Sub Each subscriber gets a copy.
Stream A durable log you can replay.

Kafka is a log. RabbitMQ and SQS are queues. SNS and Redis channels are pub/sub.

1 / 2

What is an event, and how is it different from a command?

A command is a request to do something and can be refused: PlaceOrder, ChargeCard. It is addressed to one handler, named in the imperative. An event is a fact that already happened and cannot be refused: OrderPlaced, CardCharged. It is named in the past tense, and the producer does not know or care who reacts. A message is the general word for the envelope that carries either.

Why the distinction matters: with commands the sender depends on the receiver (it needs a result); with events the receiver depends on the sender's schema, but the sender is free of its consumers. That decoupling lets you add a new consumer (analytics, email, fraud scoring) without touching the producer.

Two styles of event payload appear in answers. An event notification is small ("order 42 was placed"); consumers call back for details. Event-carried state transfer includes the data consumers need (the order lines and the address), so they do not call back, at the price of larger messages and a schema you must keep stable.

Queue, pub/sub or stream?

Work queue Pub/sub topic Log / stream
Delivery Each message to one consumer Each subscriber gets a copy Each consumer group reads independently
After consumption Deleted Gone (or kept per subscription) Kept for a retention period
Replay No Usually no Yes: rewind the offset
Ordering Often per queue Not guaranteed Per partition
Typical tools SQS, RabbitMQ queues SNS, Redis pub/sub Kafka, Kinesis, Pulsar
Best for Background jobs Notifying several systems Event history, analytics, rebuilding state

The interview trap is calling Kafka "a queue". It is a durable, partitioned log: messages are not removed when read, and a new consumer can read history from the start. That is what makes event sourcing and reprocessing possible.

What is event sourcing?

Short answer: instead of storing the current state of an entity, you store the sequence of events that led to it. The current state is a fold (a left-to-right reduction) over the events. The event store is append-only; you never update or delete an event, you add a new one that corrects it.

A bank account is the standard example. A row with balance = 70 forgets how it got there. An event-sourced account stores Deposited 100, Withdrew 30 and replays them to compute 70. Benefits: a complete audit trail, the ability to rebuild state at any past moment ("time travel"), new read models built from old events, and a natural source of integration events. Costs: replaying long histories is slow (so you take snapshots), queries on current state need a separate read model, event schemas must evolve carefully (old events live forever; use versioned events and "upcasters"), and the model is unfamiliar to most developers. GDPR "right to erasure" is hard with immutable events; the usual answers are encrypting personal data per user and deleting the key, or keeping personal data outside the event.

Node.js Model: an event-sourced account with replay and a snapshot

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.

Notice that the refused withdrawal leaves no event. Only facts are stored. Also notice that decide reads current state and apply only describes what happened; that separation is why events can be replayed without re-running business rules, which may have changed since.

What is CQRS?

CQRS (Command Query Responsibility Segregation) means using separate models for writing and for reading. The command side validates rules and changes state. The query side is shaped for fast reads (denormalized tables, a search index, a cache). The two are connected by events: the write side publishes a change, and projections update the read models.

Why do it? Reads and writes often have different shapes and different scale: a product page might be read 1,000 times per write, and needs data joined from five services. A read model built for exactly that page removes the joins. The price is eventual consistency: for a moment after a write, the read model is stale. A user who adds a comment and refreshes may not see it yet, so you must design for it (return the new data in the write response, or route that user's next read to the write model, or show an optimistic UI).

CQRS does not require event sourcing, and event sourcing does not require CQRS, although they fit together: events are the natural feed for projections. A reasonable warning to give: for a simple CRUD application CQRS adds moving parts for no benefit. Use it where read and write needs really diverge.

Node.js Model: a read model built by a projection (eventual consistency)

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.

How do consumers handle duplicates, failures and bad messages?

Brokers give you at-least-once delivery in practice, so a consumer sees duplicates after a crash, a timeout or a rebalance. Three tools make consumers safe:

  1. Idempotency. Make processing a message twice equal to processing it once. Store the IDs of processed events (in the same transaction as the side effect), use natural idempotent operations (SET status = 'shipped', not balance = balance + 10), or use a unique constraint.
  2. Retries with backoff. Transient failures (a database timeout) should be retried, with increasing delays and a limit, so a struggling dependency is not hammered.
  3. Dead-letter queue (DLQ). A poison message (malformed or always failing) must not block the queue forever. After the maximum number of attempts, move it to a DLQ, alert, and let a person inspect and replay it. On a partitioned log, one poison message at the head of a partition blocks everything behind it, so you either park it on a retry topic or skip it into a DLQ.

Other points to mention: preserve order only where you need it by using a partition key (all events of one order go to one partition); keep handlers fast and offload slow work; and monitor consumer lag (how far the consumer is behind the end of the log), the most useful health metric for a stream.

Node.js Model: an idempotent consumer with retries and a dead-letter 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.

How do events evolve without breaking consumers?

Producers and consumers deploy at different times, so the event format is a contract. Safe changes are additive: new optional fields with defaults. Removing or renaming a field, or changing its type, breaks old consumers. Practical rules: version events (OrderPlaced.v2), use a schema registry with compatibility checks (Avro, Protobuf, JSON Schema), make consumers tolerant readers (ignore unknown fields), and when a breaking change is unavoidable, publish both versions during a migration window. For event sourcing, old events stay forever, so upcast them when you read.

When should you not use events?

When you need an immediate answer or a strongly consistent result ("can this user afford the item right now?"), a direct synchronous call or a transaction is simpler. Event-driven systems are harder to trace and debug (see distributed tracing and correlation IDs), have more failure modes, and make the whole flow only eventually consistent. A good answer names both sides: events for decoupling, scaling and integration; synchronous calls for simple request/response.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which is an event rather than a command?

    Events are facts in the past tense that cannot be refused. The others are requests addressed to a handler.
  2. 2. What makes a log such as Kafka different from a simple work queue?

    A log keeps messages after they are read, and each consumer group tracks its own offset, so history can be reprocessed. A work queue deletes a message once handled.
  3. 3. In an event-sourced system, how do you get the current state of an entity?

    State is derived by applying events in order. Snapshots only shorten the replay.
  4. 4. A user posts a comment and the page, served from a CQRS read model, does not show it for a second. What is this?

    The projection that updates the read model runs after the write. Design for the delay, for example by returning the new comment in the write response.
  5. 5. A message fails on every attempt and blocks later messages in its queue. What should the consumer do after the retry limit?

    A DLQ isolates poison messages so the rest of the stream keeps flowing, and keeps the failed message for inspection and replay.

Summary

  • Commands ask for something and can be refused; events are past-tense facts. Events decouple the producer from its consumers.
  • Queues deliver each message to one worker, pub/sub gives each subscriber a copy, and a log keeps history that consumers can replay.
  • Event sourcing stores the events and folds them into state; it gives audit and time travel, and costs snapshots, schema care and unfamiliarity.
  • CQRS separates write and read models, connected by projections; it brings scalable reads and eventual consistency, and needs no event sourcing.
  • Consumers must be idempotent, retry with backoff and park poison messages in a dead-letter queue; event schemas should change additively.

Next: Distributed Systems: Consensus, Tracing, CAP and Consistency Patterns.