Leanpub Header

Skip to main content

Event Driven Architecture with Spring Boot 4.x and Kafka 4.x

Event-Driven Architecture with Spring Boot 4.x and Kafka 4.x is a hands-on, code-first guide to building resilient, scalable systems with real-world patterns like CQRS, event sourcing, sagas, outbox, idempotency, schema evolution, observability, and testing. Each chapter focuses on one concept and backs it with practical trade-offs, failure modes, and runnable Spring Boot projects you can clone and run today. If you want to move beyond buzzwords and actually ship event-driven systems that hold up in production, this book is for you.

Minimum price

$22.00

$29.00

You pay

$29.00

Author earns

$23.20
$
You can also buy this book with 1 book credit. Get book credits with a Reader Membership or an Organization Membership for your team.
PDF
EPUB
WEB
About

About

About the Book

Event-driven architecture is how modern systems scale, and how they fail silently when done wrong. This book is a practical guide to building production-grade event-driven systems with Apache Kafka 4.x and Spring Boot 4, written for developers who want to understand not just how, but why.

You'll start with the fundamentals: why monoliths hurt, what Kafka actually does under the hood, and how to wire your first event pipeline with KafkaTemplate and @KafkaListener. From there, you'll tackle the patterns that make or break real systems: CQRS, Event Sourcing, the Saga pattern, the Outbox pattern, idempotency, and schema evolution, with full working code for each.

The second half of the book goes where most tutorials stop. You'll cover observability across async services, testing strategies for event-driven systems, cache coherence in distributed read models, migrating legacy monoliths incrementally, and running multi-tenant Kafka infrastructure at scale.

Every chapter is grounded in production reality. You'll see the mistakes teams make, why they happen, and how to avoid them. Code examples use KRaft-mode Kafka (no ZooKeeper), Spring Boot 4's virtual thread model, and modern Java throughout.

What you'll learn:

- Build event pipelines with Spring Kafka and KafkaTemplate

- Implement CQRS and Event Sourcing with a full working example

- Coordinate distributed transactions with the Saga pattern

- Guarantee delivery with the Outbox pattern and CDC

- Evolve schemas safely with Avro and Confluent Schema Registry

- Trace events across services with OpenTelemetry

- Test async systems without flaky timeouts

Author

About the Author

Umur Inan

Umur Inan is a Principal Software Engineer with 15 years of experience building backend systems across enterprise, government, and high-growth environments. He specializes in microservices architecture, distributed systems, and cloud-native development, with deep expertise in Spring Boot, Kafka, and Kubernetes. Based in New York City, he has shipped products across five countries and holds a Master's in Computer Science and a Bachelor's in Computer Engineering.

Contents

Table of Contents

Preface

  1. What This Book Is
  2. Who This Book Is For
  3. How to Use This Book
  4. A Note on Kafka 4.x
  5. A Note on Spring Boot 4

Why Event-Driven Architecture?

  1. Overview
  2. The Monolith: A Love Story
  3. When the Monolith Starts to Hurt
  4. The Microservices Escape (And Its New Problems)
  5. Thinking in Events
  6. EDA vs Messaging vs Reactive: Clearing the Confusion
  7. A Tale of Two Services
  8. When EDA Is the Right Choice (And When It Isn’t)
  9. Common Mistakes and Summary

Apache Kafka from the Ground Up

  1. Overview
  2. What Kafka Is (And Isn’t)
  3. Topics, Partitions, and Offsets
  4. Producers: Writing to Kafka
  5. Consumers and Consumer Groups
  6. Brokers, Replication, and Leader Election
  7. KRaft Mode: Kafka Without ZooKeeper
  8. Retention: What Kafka Does With Old Messages
  9. Running Kafka Locally with Docker Compose
  10. Running Kafka in Production: Managed Services
  11. Common Mistakes and Summary

Spring Boot 4 + Kafka

  1. Overview
  2. Spring Kafka: What It Gives You
  3. Project Setup
  4. Sending Events with KafkaTemplate
  5. Receiving Events with @KafkaListener
  6. JSON Serialization with Jackson
  7. Error Handling and Dead Letter Topics
  8. Putting It Together: The Notification Service
  9. Common Mistakes and Summary

CQRS: Separating Reads from Writes

  1. Overview
  2. The Problem: One Model Trying to Do Everything
  3. Command vs Query: The Core Idea
  4. CQRS Without Event Sourcing
  5. The Command Side: Handling Writes
  6. The Query Side: Building Read Models
  7. Eventual Consistency Between Write and Read Models
  8. When CQRS Pays Off (and When It Doesn’t)
  9. Common Mistakes and Summary

Event Sourcing

  1. Overview
  2. The Problem With Saving State
  3. Event Sourcing Fundamentals
  4. The Event Store
  5. GDPR and the Right to Erasure
  6. Aggregates and Rehydration
  7. Snapshots
  8. Implementing with Spring Boot
  9. When Event Sourcing Fits
  10. Common Mistakes and Summary

CQRS + Event Sourcing Together

  1. Overview
  2. Why They Belong Together
  3. The Write Side: Event Sourcing with Commands
  4. The Read Side: Multiple Independent Projections
  5. Replaying and Rebuilding: The Superpower
  6. Full Stack Implementation: Running the Pattern
  7. Operational Concerns: Running This in Production

The Saga Pattern

  1. Overview
  2. The Distributed Transaction Problem
  3. What Is a Saga?
  4. Choreography Sagas
  5. Orchestration Sagas
  6. Compensating Transactions
  7. Building a Saga with Spring Boot
  8. Choreography vs Orchestration
  9. Testing Sagas
  10. Common Mistakes and Summary

Eventual Consistency

  1. Overview
  2. The Consistency Spectrum
  3. The CAP Theorem in Practice
  4. Measuring the Consistency Window
  5. Designing Around Consistency Gaps
  6. Conflict Resolution
  7. Communicating Consistency to Users
  8. Common Mistakes and Summary

The Outbox Pattern

  1. Overview
  2. The Dual-Write Problem
  3. The Outbox Pattern
  4. Implementing the Outbox
  5. Polling Relay
  6. Change Data Capture
  7. Cleanup and Operations
  8. Testing the Outbox Pattern
  9. Common Mistakes and Summary

Idempotency and Duplicate Handling

  1. Overview
  2. At-Least-Once Means Duplicates
  3. Natural Idempotency
  4. Idempotency Keys
  5. Deduplication in Practice
  6. Database-Level Idempotency
  7. Exactly-Once Semantics
  8. Common Mistakes and Summary

Schema Evolution

  1. Overview
  2. The Schema Problem
  3. Avro Basics
  4. Confluent Schema Registry
  5. Compatibility Rules
  6. Evolving Schemas Safely
  7. Spring Boot Avro Integration
  8. Testing Schema Compatibility
  9. Common Mistakes and Summary

Observability

  1. Overview
  2. Why Observability Is Different
  3. Distributed Tracing with OpenTelemetry
  4. Propagating Context Through Kafka
  5. Kafka Metrics
  6. Logging Correlation
  7. Building the Observability Stack
  8. Securing Kafka in Production
  9. Disaster Recovery and Multi-Region
  10. Common Mistakes and Summary

Testing Event-Driven Systems

  1. Overview
  2. The Testing Challenge
  3. Unit Testing Event Handlers
  4. Integration Testing with EmbeddedKafka
  5. Contract Testing
  6. Testcontainers
  7. Testing Error Paths
  8. Testing the Outbox Pattern
  9. Testing Event Sourcing Aggregates
  10. Mistakes and Summary

Cache Coherence in Distributed Read Models

  1. Overview
  2. The Cache Invalidation Problem
  3. Cache-Aside vs. Write-Through in an EDA Context
  4. Event-Driven Cache Invalidation
  5. Read-Your-Writes Consistency
  6. Redis and Kafka: A Complete Pattern
  7. Handling Cache Stampede on Event Bursts
  8. Common Mistakes and Summary

Migrating to Event-Driven Architecture

  1. Overview
  2. The Strangler Fig Pattern
  3. CDC as a Migration Bridge
  4. The Anti-Corruption Layer
  5. Common Mistakes and Summary

Multi-Tenant Event-Driven Systems

  1. Overview
  2. Isolation Models
  3. Quota Management
  4. Topic Lifecycle Governance
  5. Common Mistakes and Summary

Glossary

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub