Volume 9 · Chapter 4 — Enterprise Application Architecture & System Design

Event-Driven Architecture

Goutham Mayaluri 8 min read .NETC#

4.1 Introduction

Event-driven architecture (EDA) lets components communicate by announcing facts rather than issuing direct calls. A service publishes “PaymentCompleted” and moves on; whoever cares - fraud, notifications, accounting - reacts independently. This inversion is the strongest form of loose coupling: the producer knows nothing about its consumers, so new reactions are added without changing the producer. EDA is how enterprise systems achieve scalability, resilience, and extensibility, at the cost of embracing asynchrony and eventual consistency.

4.2 Commands vs Events

The distinction is fundamental. A command is an instruction to do something - imperative, directed at one handler, and it can be rejected (“ProcessPayment”). An event is a statement that something already happened - past tense, broadcast, and it cannot be rejected because it is a fact (“PaymentProcessed”). Commands create coupling to the receiver; events invert it. Choosing the right one shapes how coupled your system is.


// Command: imperative, one handler, may fail/reject.
public sealed record ProcessPayment(Guid PaymentId, decimal Amount);

// Event: past-tense fact, broadcast to any interested subscriber.
public sealed record PaymentProcessed(Guid PaymentId, decimal Amount, DateTime At);

4.3 Choreography vs Orchestration

There are two ways to coordinate a multi-step process. Inchoreography, services react to each other’s events with no central coordinator - decentralized and simple for short flows, but the overall process is implicit and hard to follow as it grows. Inorchestration, a central coordinator drives the steps explicitly - easier to monitor, change, and reason about for complex, long-running workflows, at the cost of a coordinator that must itself be resilient.

sequenceDiagram
  autonumber
  participant Pay as Payments
  participant Bus as Event Broker
  participant Fraud as Fraud
  participant Notif as Notifications
  participant Ledger as Ledger
  Pay-)Bus: PaymentProcessed (fact)
  Bus-)Fraud: PaymentProcessed
  Bus-)Notif: PaymentProcessed
  Bus-)Ledger: PaymentProcessed
  Note over Bus,Ledger: Choreography - each reacts independently; Payments knows none of them

4.4 Event Brokers

Brokers provide the reliable channel between producers and consumers. Azure Service Bus offers queues and topics with sessions, dead-lettering, and duplicate detection for enterprise messaging. Kafka and Event Hubs are log-based streams for high- throughput, replayable event pipelines. The choice depends on whether you need message-broker semantics (competing consumers, per-message handling) or event-stream semantics (ordered, replayable logs read by many consumers at their own pace).

4.5 Event Schema and Versioning

Events are contracts that outlive any single deployment, so their schema must evolve without breaking existing consumers. The rules: make additive, backward- compatible changes; version events when a breaking change is unavoidable and run both during migration; and use tolerant readers that ignore unknown fields and default missing ones. Well-designed events carry enough business context to be useful without forcing consumers to call back to the producer.

4.6 Reliable Publishing and the Saga Pattern

Two hard problems recur. First, the dual write: updating state and publishing an event must be atomic, solved by the transactional outbox (write the event to a table in the same transaction, then relay it). Second, business processes spanning services cannot use a database transaction, so they use sagas - sequences of local transactions with compensating actions to undo prior steps on failure. Both are covered in depth in the messaging and CQRS chapters; here the point is that reliable eventing requires them.

Senior Interview Question

What is the difference between a command and an event, and why does it matter?

A command is an imperative request to perform an action, directed at a specific handler, and it can be validated and rejected - “ProcessPayment.” An event is a past-tense statement that something has already happened, broadcast to any interested subscriber, and it cannot be rejected because it is a fact - “PaymentProcessed.” The difference matters because it determines coupling: a command couples the sender to a known receiver and its contract, whereas an event inverts the dependency - the producer emits a fact and knows nothing about who consumes it, so new consumers are added without changing the producer. Designing interactions as events where possible is what gives event-driven systems their loose coupling and extensibility; using commands where you genuinely need a directed, rejectable action. Naming and tense discipline (imperative vs past) keeps the model clear.

Senior Interview Question

Choreography or orchestration - how do you choose?

I choose based on the complexity and visibility needs of the process. Choreography, where services react to each other’s events with no central coordinator, is decentralized and elegant for short, simple flows - two or three steps - and avoids a single point of control, but as the flow grows the overall business process becomes implicit, smeared across many services and hard to follow, debug, or change. Orchestration introduces a coordinator that explicitly drives the steps, which makes the process visible in one place, easier to monitor, modify, and handle timeouts and compensation for - ideal for complex, long-running, or business-critical workflows - at the cost of a coordinator that must itself be made resilient and not become a bottleneck. My rule of thumb: choreography for simple event reactions, orchestration once the flow has multiple steps, branching, timeouts, or needs clear operational visibility, such as a payment-settlement saga.

Senior Interview Question

How do you evolve event schemas without breaking consumers?

Because events are contracts consumed by services that deploy on their own schedules, I treat schema evolution as a compatibility discipline. The default is additive, backward-compatible change - add new optional fields, never rename or remove existing ones or change their meaning - so old consumers keep working while new ones use the new data. Consumers use tolerant readers that ignore unknown fields and default missing ones, which absorbs additive changes gracefully. When a breaking change is truly unavoidable, I version the event (v1 and v2), publish both during a migration window, and retire v1 only after every consumer has moved. I also design events with enough business context up front so I rarely need to change them, and I use a schema registry or contract tests to catch incompatibilities before they ship. The governing principle is that producers and consumers must be able to deploy independently, which additive, versioned, tolerantly-read schemas make possible.

Principal Architect Question

How do you design an enterprise event-driven system to be reliable end to end?

Reliability in EDA is about guaranteeing that facts are recorded, delivered, and processed correctly despite failures. On the producer side I solve the dual-write problem with the transactional outbox so a state change and its event commit atomically - no lost or phantom events. On the transport I pick a broker with the delivery guarantees the domain needs (at-least-once with dead-lettering for poison messages) and design for ordering only where it matters, using partition/session keys per entity rather than global ordering. On the consumer side I make every handler idempotent, because at-least-once delivery and retries mean events can arrive more than once, using dedup keys or naturally idempotent operations. Cross-service processes use sagas with explicit compensation rather than pretending distributed transactions exist. I version event contracts for independent deployment, and I make the whole flow observable with correlation IDs and tracing so a business transaction can be followed across every hop and stuck messages are visible. The combination - outbox, reliable broker with DLQs, idempotent consumers, sagas, versioned contracts, and end-to-end tracing - is what turns “publish and hope” into a dependable event-driven system.

Principal Architect Question

What are the downsides of event-driven architecture, and how do you decide it is worth it?

EDA’s benefits - loose coupling, scalability, resilience, extensibility - come with real costs that I weigh honestly. Asynchrony brings eventual consistency, so the system and its users must tolerate the window where different parts disagree, and the UX has to be designed around pending states. Debugging is harder because there is no single call stack - a business process is an emergent choreography across services, which demands strong tracing and correlation to follow. Reliable delivery forces outbox, idempotency, and dead-letter handling; ordering and duplicates become your problem; and the implicit flow can make the overall behavior hard to reason about. Operationally you now run and monitor a broker as critical infrastructure. So I adopt EDA where its benefits are genuinely needed - many independent consumers of the same facts, high scalability, temporal decoupling from unreliable or slow dependencies, and the ability to add reactions without touching producers - and I avoid it for simple, synchronous request/response interactions where it would add asynchrony and operational complexity for no gain. The decision is driven by whether the coupling and scaling problems it solves actually exist in this system.

4.7 Banking Enterprise Scenario

A bank’s payment platform publishes aPaymentProcessedevent through a transactional outbox onto a broker, where fraud, notification, accounting, and customer-communication services each consume it independently and idempotently - adding a new reaction (say, a rewards service) requires no change to Payments. Simple reactions use choreography, but the multi-step settlement workflow, with its timeouts and compensations, is an explicit orchestration for visibility and control. Events are versioned and tolerantly read so services deploy independently, dead- letter queues capture poison messages for investigation, and correlation IDs let operators trace a single payment across every reacting service.

Chapter Revision Notes

  • Events are past-tense facts (broadcast); commands are imperative requests (directed).
  • Events invert dependencies - producers know nothing about consumers.
  • Choreography for simple flows; orchestration for complex, long-running workflows.
  • Evolve event schemas additively and version them; use tolerant readers.
  • Reliable EDA needs the outbox, idempotent consumers, sagas, and tracing.
  • Adopt EDA for its coupling/scaling benefits; avoid it for simple request/response.