Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 59

    Event-Driven Architecture

    Event-Driven Architecture (EDA) replaces tight synchronous chains with facts on a log.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Event-Driven Architecture (EDA) replaces tight synchronous chains with facts on a log. Orders emits OrderPlaced; billing, inventory, notification, and analytics react without Orders importing their clients.

    On Boot 3 this usually means domain events + Kafka (or Rabbit), plus the outbox pattern so the database write and the broker publish stay consistent.

    Version events, tolerate additive fields, and route poison messages to a dead-letter topic.

    This lesson reframes TechLearningPRO Orders integrations as reactions to events rather than nested HTTP calls.

    Purpose of this lesson

    Apply EDA to Orders: emit events, decouple consumers, and use outbox/DLQ practices on Boot 3.

    Understanding the topic

    Producers publish facts; consumers decide reactions — loose coupling with replay.

    • domain event: something that happened (OrderPlaced).
    • fan-out: many consumers, one topic.
    • outbox: transactional write of event intent with business data.
    • versioning: additive changes; consumers ignore unknown fields.
    • DLQ: isolate poison messages for repair.

    Step-by-step explanation

    1. Define OrderPlaced as a versioned record (ids + totals, minimal PII).
    2. Write order + outbox row in one @Transactional method.
    3. Run a publisher that ships outbox rows to Kafka and marks them sent.
    4. Add independent consumers for billing and notification with DLQ configuration.

    Syntax reference

    Synchronous chain vs event-driven Orders fan-out.

    bash
    # Synchronous (tight coupling)
    Order ──► Billing ──► Inventory ──► Notification
    x any failure breaks the chain
    x each new consumer = code change in Order
    # Event-driven (loose coupling)
    Order ──► Kafka [order.placed] ──► Billing
    └──► Inventory
    └──► Notification
    └──► Analytics
    + add consumers without touching producer
    + retries, dead-letter queues, replay from log
    # Outbox (same DB transaction)
    orders table + outbox table ──► relay ──► Kafka

    Real-world use

    Adding a fraud-scoring partner used to mean changing Orders to call yet another HTTP API inside the purchase path. With EDA, fraud subscribed to order.placed and published order.flagged without a Orders redeploy for every new detector.

    Security implications

    Events travel farther than a single HTTP response.

    • Minimize personal data in events; use opaque ids.
    • Encrypt clusters in transit; ACL topics per team.
    • Treat replay environments carefully — they can re-trigger side effects.

    Best practices

    • Outbox or change-data-capture for reliable publish.
    • Contract tests for event schemas; document versions.
    • Always configure DLQ and alert on depth.

    Common mistakes

    • Dual-write: commit order then publish — crash loses the event.
    • Huge event payloads that embed entire aggregates and PII.
    • Chatty event storms without aggregation or idempotent handlers.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhat problem does the outbox pattern solve?+

    Answer

    It avoids dual-write inconsistency by storing the event intent in the same database transaction as the business update, then publishing asynchronously.
    2BeginnerQuestionWhy is EDA looser coupling than sync HTTP?+

    Answer

    Producers do not know consumers; new subscribers can be added without changing the producer’s code path.
    3IntermediateQuestionWhat is a dead-letter queue/topic for?+

    Answer

    It isolates messages that repeatedly fail processing so healthy traffic continues and operators can inspect poison payloads.

    Hands-on exercise

    Lab. Implement place-order with an outbox table. Publish to Kafka from a relay. Add a notification listener that fails once then succeeds, and confirm the business order remains committed while the event retries.

    Summary

    Event-driven architecture turns TechLearningPRO Orders into a producer of facts: outbox to Kafka, many consumers, safer evolution than synchronous chains.

    Ready to mark this lesson complete?Track your journey across the entire course.