Lab 11: Kafka Java Client

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm zchencow/innozverse-java:latest bash


Overview

Master the Apache Kafka Java client: configure producers with exactly-once semantics, consume with manual offset management, understand consumer group rebalancing, and design schemas with Avro. All API patterns compile and run verified against kafka-clients:3.6.1.


Step 1: Kafka Architecture Review

Kafka Cluster:
  ┌──────────────────────────────────────────────────┐
  │  Broker 1         Broker 2         Broker 3      │
  │  ┌──────────┐    ┌──────────┐    ┌──────────┐   │
  │  │ Topic-A  │    │ Topic-A  │    │ Topic-A  │   │
  │  │ Part 0   │    │ Part 1   │    │ Part 2   │   │
  │  │(leader)  │    │(leader)  │    │(leader)  │   │
  │  └──────────┘    └──────────┘    └──────────┘   │
  └──────────────────────────────────────────────────┘
       │                   │                │
  Producer sends to leader of each partition
  Consumer group: each partition → one consumer in group

Key concepts:
  Offset:      position within a partition (monotonically increasing)
  Partition:   ordered, immutable sequence of records
  Consumer group: multiple consumers sharing topic partitions
  Log compaction: retain latest record per key
  ISR: in-sync replicas (for durability guarantees)

Step 2: KafkaProducer Configuration


Step 3: Producer Send Patterns


Step 4: KafkaConsumer Configuration


Step 5: Consumer with Manual Offset Management


Step 6: Consumer Group Rebalancing


Step 7: Avro Schema and Exactly-Once Concepts


Step 8: Capstone — Kafka Client API Compilation

📸 Verified Output:


Summary

Concept
Config Key
Purpose

Idempotent producer

enable.idempotence=true

No duplicate messages on retry

Strong durability

acks=all

All ISR replicas confirm

Ordering

max.in.flight.requests=5

EOS with idempotence

Transactional

transactional.id=xyz

Atomic multi-partition writes

Manual commits

enable.auto.commit=false

Process-then-commit

Read committed

isolation.level=read_committed

Skip uncommitted records

Rebalancing

ConsumerRebalanceListener

Clean offset commit

Schema evolution

Avro + Schema Registry

Forward/backward compatible

Last updated