Event-Driven
Intent
Let services react to things that happened — published events — instead of calling each other directly, so producers and consumers stay decoupled in time and knowledge.
When It Helps
- Workflows fan out: one action (order placed) should trigger many reactions (billing, email, analytics).
- Producers should not wait for or even know their consumers.
- Load is spiky and consumers should drain work at their own pace.
Structure
flowchart LR P1[Producer: Checkout] -->|publishes order.placed| Broker[(Event Broker)] Broker --> C1[Consumer: Billing] Broker --> C2[Consumer: Notifications] Broker --> C3[Consumer: Analytics] C1 --> DB1[(Billing DB)] C2 --> DB2[(Notifications Store)] C3 --> DB3[(Analytics Store)]
Deployment unit: producers, consumers, and the broker each deploy and scale independently. Communication: asynchronous event delivery through a broker (queue/stream log style); producers never call consumers directly. Data ownership: each consumer owns its store and builds it from the events it subscribes to; the broker owns only transient delivery, not domain truth.
Rules
- Producers publish facts about what happened, never commands telling a specific consumer what to do.
- Consumers own their subscription position and must handle duplicate or out-of-order delivery idempotently.
- Event schemas evolve backward-compatibly; breaking changes ship as new event versions or new topics.
- The broker is infrastructure: business rules live in producers and consumers, not in routing configuration.
Illustrative Snippet
Illustrative pseudocode — not runnable.
# sketch: broker-based async delivery
topic: order.placed (v2: {order_id, total, items[]})
producer checkout:
on payment_accepted -> publish(order.placed, {...})
consumer billing:
on order.placed -> charge(total) # idempotent on order_id
consumer notifications:
on order.placed -> send_email(items) # retries on failure
Basic Recap
Event-driven architecture publishes durable facts that other components can consume asynchronously. Producers do not need to know every reaction, and consumers can scale or recover at their own pace. A broker buffers delivery, but domain truth remains with the producing application unless a stronger log-based model is chosen. The approach exchanges immediate consistency and simple call traces for loose coupling and elasticity.
Complex Scenario
A ticketing platform emits OrderConfirmed after a database commit, triggering seat delivery, loyalty points, fraud scoring, and analytics. A new consumer should be addable without changing checkout. Each consumer keeps its own checkpoint and store, and event versions include an order identifier and occurrence time. Checkout uses an outbox so a successful order cannot silently lose its event.
flowchart LR
Checkout["Checkout producer"] --> Tx["Order transaction and outbox"]
Tx --> OrderDB[("Orders DB")]
Relay["Outbox relay"] --> Broker[("Partitioned event broker")]
Tx --> Relay
Broker --> Delivery["Seat delivery consumer"]
Broker --> Loyalty["Loyalty consumer"]
Broker --> Fraud["Fraud consumer"]
Broker --> Analytics["Analytics consumer"]
Delivery --> Mail["Email provider"]
Delivery --> DLQ[("Delivery dead-letter topic")]
Loyalty --> PointsDB[("Points DB")]
Analytics --> Warehouse[("Analytics store")]
Failure walkthrough. During a tenfold sales burst, the email provider goes down and delivery retries repeatedly at the head of each partition. Lag grows, duplicates reach loyalty after redeployments, and checkout would fail if it published synchronously to a congested broker. Mitigate with outbox-based producer decoupling, consumer idempotency records, bounded retries with backoff, a dead-letter path plus replay tooling, and consumer scaling by lag without exceeding downstream provider limits.
Edge cases — concurrency needs per-order aggregate versions, retries assume at-least-once delivery, and ordering is normally preserved only within a partition key. Partial failure requires committing the consumer state and checkpoint atomically where possible, or making replay harmless.
Sketch (illustrative pseudocode, not runnable):
event OrderConfirmed.v2:
key: order_id
fields: {event_id, order_id, occurred_at, seats[], total}
compatibility: additive_fields_only
consumer seat-delivery:
group: delivery-production
on event:
if inbox.contains(event_id): acknowledge
else: send_and_record_inbox(event)
retry: exponential_backoff(max=5)
on_exhausted: dead_letter_with_cause
operations:
scale_signal: consumer_lag
alert: oldest_lag > 90s or dead_letter_growth
Trade-offs
- Benefit: loose coupling and elastic scaling — add consumers without touching producers, and let slow consumers lag without blocking checkout.
- Benefit: resilience to bursts — the broker buffers spikes so producers stay fast while consumers catch up.
- Cost: eventual consistency and observability gaps — tracing a single user request across topics, retries, and replays is harder than following one call chain.
- Cost: broker operations — schema registry, partition sizing, retention, dead-letter handling, and replay tooling become permanent upkeep.
When to Use / When Not to Use
Use when:
- Reactions across services with temporal decoupling via a broker.
Avoid when:
- Request/response with strong consistency needs — prefer direct calls or CQRS reads.
Anti-pattern / Failure Mode
- Misuse: Assuming broker delivery equals exactly-once processing without idempotent consumers.
- Review signal: Unordered consumers depending on global event order.
Failure Modes / Trade-offs
- Event soup: vague events such as
ObjectUpdatedcarry no stable business meaning, forcing consumers to inspect mutable producer state. - Distributed schema coupling: a producer renames a field and breaks unknown consumers. Registry checks and consumer-driven contract tests should gate publication.
- Poison-message blockage: endless retries prevent later valid events from progressing. Dead-lettering is useful only when ownership, alerting, and replay procedures exist.
- Consistency trade-off: producers stay responsive during bursts and consumers evolve independently, but users and operators must handle stale views and delayed reactions.
Flow review. Document which business fact each topic carries, its partition key, retention, owner, and known consumers; a topic named after a technical layer is usually a weak contract.
Replay review. Reprocessing history must not resend customer email or award points twice, so side-effect consumers need separate replay modes or durable idempotency evidence.
Lag review. Monitor the oldest unconsumed timestamp as well as message count, since many tiny events can hide a badly delayed consumer.
- Broker trade-off: managed brokers reduce infrastructure work but still require schema, quota, residency, recovery, and cost decisions.
- Coupling trade-off: producers avoid direct consumer dependencies, yet consumers become semantically dependent on event meaning and completeness.
- Debugging trade-off: correlation IDs connect a workflow across topics, but they do not replace checkpoints, dead-letter context, and a timeline of consumer decisions.
Constraint-based use / avoid
| Constraint | Recommendation |
|---|---|
| Bursty traffic with work that can complete later | Use broker buffering and scale consumers against measured lag. |
| Many independent reactions to one business fact | Use versioned events to avoid changing the producer for each consumer. |
| User must receive an authoritative decision within 100 ms | Avoid making asynchronous reactions part of that decision path. |
| Tiny team without broker and replay operating experience | Avoid a broad event backbone; start with a managed queue for one clear workflow. |
| Compliance requires an immutable audit trail | Use cautiously: delivery events alone are insufficient unless retention, integrity, and access controls are guaranteed. |
Related Patterns
- Microservices — services frequently use events as their decoupling mechanism.
- CQRS + Event Sourcing — differs by centering on a persistent event log plus separate read/write models, not just broker delivery.
- Serverless — functions pair naturally with events as triggers that scale to zero between bursts.
- CQRS & Event Sourcing — broker reactions vs log-as-truth.
- Observer — in-process precursor.
- Idempotency — retries are safe only with idempotent consumers.