Preface
- What This Book Is
- Who This Book Is For
- How to Use This Book
- A Note on Kafka 4.x
- A Note on Spring Boot 4
Why Event-Driven Architecture?
- Overview
- The Monolith: A Love Story
- When the Monolith Starts to Hurt
- The Microservices Escape (And Its New Problems)
- Thinking in Events
- EDA vs Messaging vs Reactive: Clearing the Confusion
- A Tale of Two Services
- When EDA Is the Right Choice (And When It Isn’t)
- Common Mistakes and Summary
Apache Kafka from the Ground Up
- Overview
- What Kafka Is (And Isn’t)
- Topics, Partitions, and Offsets
- Producers: Writing to Kafka
- Consumers and Consumer Groups
- Brokers, Replication, and Leader Election
- KRaft Mode: Kafka Without ZooKeeper
- Retention: What Kafka Does With Old Messages
- Running Kafka Locally with Docker Compose
- Running Kafka in Production: Managed Services
- Common Mistakes and Summary
Spring Boot 4 + Kafka
- Overview
- Spring Kafka: What It Gives You
- Project Setup
- Sending Events with KafkaTemplate
- Receiving Events with @KafkaListener
- JSON Serialization with Jackson
- Error Handling and Dead Letter Topics
- Putting It Together: The Notification Service
- Common Mistakes and Summary
CQRS: Separating Reads from Writes
- Overview
- The Problem: One Model Trying to Do Everything
- Command vs Query: The Core Idea
- CQRS Without Event Sourcing
- The Command Side: Handling Writes
- The Query Side: Building Read Models
- Eventual Consistency Between Write and Read Models
- When CQRS Pays Off (and When It Doesn’t)
- Common Mistakes and Summary
Event Sourcing
- Overview
- The Problem With Saving State
- Event Sourcing Fundamentals
- The Event Store
- GDPR and the Right to Erasure
- Aggregates and Rehydration
- Snapshots
- Implementing with Spring Boot
- When Event Sourcing Fits
- Common Mistakes and Summary
CQRS + Event Sourcing Together
- Overview
- Why They Belong Together
- The Write Side: Event Sourcing with Commands
- The Read Side: Multiple Independent Projections
- Replaying and Rebuilding: The Superpower
- Full Stack Implementation: Running the Pattern
- Operational Concerns: Running This in Production
The Saga Pattern
- Overview
- The Distributed Transaction Problem
- What Is a Saga?
- Choreography Sagas
- Orchestration Sagas
- Compensating Transactions
- Building a Saga with Spring Boot
- Choreography vs Orchestration
- Testing Sagas
- Common Mistakes and Summary
Eventual Consistency
- Overview
- The Consistency Spectrum
- The CAP Theorem in Practice
- Measuring the Consistency Window
- Designing Around Consistency Gaps
- Conflict Resolution
- Communicating Consistency to Users
- Common Mistakes and Summary
The Outbox Pattern
- Overview
- The Dual-Write Problem
- The Outbox Pattern
- Implementing the Outbox
- Polling Relay
- Change Data Capture
- Cleanup and Operations
- Testing the Outbox Pattern
- Common Mistakes and Summary
Idempotency and Duplicate Handling
- Overview
- At-Least-Once Means Duplicates
- Natural Idempotency
- Idempotency Keys
- Deduplication in Practice
- Database-Level Idempotency
- Exactly-Once Semantics
- Common Mistakes and Summary
Schema Evolution
- Overview
- The Schema Problem
- Avro Basics
- Confluent Schema Registry
- Compatibility Rules
- Evolving Schemas Safely
- Spring Boot Avro Integration
- Testing Schema Compatibility
- Common Mistakes and Summary
Observability
- Overview
- Why Observability Is Different
- Distributed Tracing with OpenTelemetry
- Propagating Context Through Kafka
- Kafka Metrics
- Logging Correlation
- Building the Observability Stack
- Securing Kafka in Production
- Disaster Recovery and Multi-Region
- Common Mistakes and Summary
Testing Event-Driven Systems
- Overview
- The Testing Challenge
- Unit Testing Event Handlers
- Integration Testing with EmbeddedKafka
- Contract Testing
- Testcontainers
- Testing Error Paths
- Testing the Outbox Pattern
- Testing Event Sourcing Aggregates
- Mistakes and Summary
Cache Coherence in Distributed Read Models
- Overview
- The Cache Invalidation Problem
- Cache-Aside vs. Write-Through in an EDA Context
- Event-Driven Cache Invalidation
- Read-Your-Writes Consistency
- Redis and Kafka: A Complete Pattern
- Handling Cache Stampede on Event Bursts
- Common Mistakes and Summary
Migrating to Event-Driven Architecture
- Overview
- The Strangler Fig Pattern
- CDC as a Migration Bridge
- The Anti-Corruption Layer
- Common Mistakes and Summary
Multi-Tenant Event-Driven Systems
- Overview
- Isolation Models
- Quota Management
- Topic Lifecycle Governance
- Common Mistakes and Summary