Skip to main content
PreparationZone Logo
PrepZone

Kafka Producers and Consumers

KafkaTemplate, @KafkaListener, serialization and error handling with DLT.

Pass checkpoint below

Read these first

Why this matters

  • When the inventory service, notification service, and analytics pipeline all need to know about a new order, point-to-point HTTP calls create a fragile web of dependencies.
  • Kafka persists messages to a log — consumers can restart, replay from an offset, and scale independently by adding members to a consumer group.
  • Spring for Apache Kafka wraps the raw client with KafkaTemplate and @KafkaListener, handling serialization and error hooks with minimal boilerplate.

KafkaTemplate

L1 · Fresher

Send messages to topics with keys for partitioning — BookStore order events keyed by orderId keep ordering per order.

@KafkaListener

L2 · Intermediate

Consumers declare topics and group IDs; offset commits define at-least-once vs effectively-once with idempotent handlers.

Error handling

L3 · Advanced

Dead-letter topics and DefaultErrorHandler prevent poison messages from stalling the whole consumer group.

Serialization

L4 · Expert

Use Schema Registry or JSON contracts with compatibility rules — never change event schemas without versioned subjects.

Spring Kafka essentials

  • KafkaTemplate<K, V> — the producer API; send synchronously, asynchronously, or with callbacks.
  • @KafkaListener — declares a method that consumes from one or more topics.
  • Consumer groups — partition assignment and offset tracking; each message is delivered to one member per group.
  • Serializers — convert Java objects to bytes; JSON with Jackson is common for BookStore DTOs.
  • Dead-letter topic (DLT) — routes poison messages after retries exhaust, so one bad payload does not block the partition.
OrderServiceVaultCommerce
vaultcommerce.orders.placed.v1
InventoryConsumer
KafkaTemplate publishes to a topic. @KafkaListener consumes from a consumer group.

Dependencies and configuration

Java
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Publishing order events
  • Consuming with error handling

Test yourself

Answer these before moving on — recall is what makes it stick.