A hands-on Kafka learning project built around a Spring Boot Lost & Found service. It combines an interactive browser guide with runnable examples, so you can explore Kafka concepts visually and then see them in a real broker and application.
Open index.html in a browser. The guide introduces the project flow, explains the Kafka concepts, and includes interactive diagrams and examples.
- Produce and consume events, and inspect record keys, partitions, offsets, and retention.
- Evolve Avro event schemas with Schema Registry.
- Compare consumer groups, fan-out, partition assignment, and rebalancing.
- Explore retries, dead-letter topics, reliable notification delivery, acknowledgements, and Kafka transactions.
- Java 21
- Docker Desktop with Docker Compose
- Internet access for the Maven Wrapper and dependency downloads
The browser guide can be opened on its own. To run the service examples, start the dependencies and follow the project steps below.
index.html— interactive Kafka learning guidesrc/— Spring Boot application, Avro schemas, and testscompose.yaml— Kafka, Schema Registry, and PostgreSQL servicespostman/— requests and local environment for the API examplesdocs/PROJECT-3.md— Project 3 delivery and transaction exercisespom.xmlandmvnw.cmd— Java build configuration and Maven Wrapper
A small Spring Boot API publishes lost-item reports to Kafka. An audit listener logs each consumed record's key and broker metadata.
- Java 21
- Internet access for the Maven Wrapper and dependency downloads
- Docker Desktop with Docker Compose
Start the single-node KRaft broker and create the one-partition topic:
docker compose up -d
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --if-not-exists --topic lost-found.events --partitions 1 --replication-factor 1 --config retention.ms=604800000Start the application in another terminal:
.\mvnw.cmd spring-boot:runReport a lost item:
Invoke-RestMethod -Method Post -Uri http://localhost:8080/lost-items -ContentType 'application/json' -Body '{"itemId":"wallet-42","description":"Brown leather wallet near the library"}'The API returns HTTP 202 after Kafka acknowledges the send. eventId is a unique application event identifier; itemId is the Kafka record key. The broker assigns a partition and an offset within that partition. With one partition, all records are stored there; a key is the partitioning input and becomes more useful when the topic has multiple partitions. Offsets identify log positions, not event IDs.
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic lost-found.events
docker compose exec kafka /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic lost-found.events --from-beginning --property print.key=true --property print.partition=true --property print.offset=true --property print.timestamp=trueThe Spring audit consumer also logs key, topic, partition, offset, Kafka timestamp, and event body. To read one known partition/offset, use the consumer's --partition 0 --offset N options:
docker compose exec kafka /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic lost-found.events --partition 0 --offset 0 --max-messages 1 --property print.key=true --property print.partition=true --property print.offset=trueThe topic starts with a seven-day time retention. Shorten it to five seconds, publish an item, then wait and inspect again:
docker compose exec kafka /opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name lost-found.events --alter --add-config retention.ms=5000Kafka removes expired log segments asynchronously; it does not promise deletion exactly five seconds after an individual record. For a quick repeatable experiment, stop the app, set segment.ms low along with retention, publish records, and inspect after segment rolling/cleanup. Restore the seven-day setting afterward:
docker compose exec kafka /opt/kafka/bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name lost-found.events --alter --add-config retention.ms=604800000Stop the broker with docker compose down; add -v only when you intentionally want to delete the broker's stored data.
Maven is pinned to 3.9.12 in this repository. On Windows, use the checked-in wrapper, which downloads and SHA-512 verifies Maven on first use; no global Maven installation is needed:
.\mvnw.cmd spring-boot:runUse .\mvnw.cmd clean package to build the jar. Java 21 and an internet connection for the first Maven/dependency downloads are still required.
Import both files from postman/ into Postman:
Lost-Found-Project-1.postman_collection.jsonLost-Found-Local.postman_environment.json
Select the Lost & Found - Local environment, make sure baseUrl is http://localhost:8080, then send Report a lost item. The API requires itemId and description; it generates eventId and timestamp itself. Start Kafka and the Spring Boot app first.
Project 2 extends the same topic with two event types and Avro schemas:
POST /lost-items/v1publishes the historical V1 Avro shape. Use it first for the schema-evolution demonstration.POST /lost-itemspublishes the current V2ItemReportedLostschema. It adds optionalreportedLocationwith a null default.POST /found-itemspublishes anItemFoundevent.
All events use itemId as the Kafka key. Email and SMS listeners are separate readers in email-group and sms-group; each group gets its own copy of every event. These listeners log a simulated notification. They do not connect to a real email or SMS provider.
The Project 1 topic may contain JSON-encoded history. Project 2 consumers use auto-offset-reset: latest, so newly created email and SMS groups begin with events published after they start and do not try to decode old JSON records as Avro. The existing audit group resumes from its committed position.
The Compose file exposes Kafka to your computer at localhost:9092 and to containers at kafka:29092. Schema Registry is available at http://localhost:8081.
docker compose up -dThe topic from Project 1 may already exist with one partition. Increase it to three:
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic lost-found.events --partitions 3If creating the topic for the first time, create it with three partitions and replication factor one. Check the result:
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic lost-found.events
Invoke-RestMethod http://localhost:8081/subjectsWait until Schema Registry answers before starting the application.
.\mvnw.cmd -DskipTests clean package
.\mvnw.cmd spring-boot:runThe default app starts the audit, email, and SMS consumers. Open a second terminal for API requests.
Publish the V1 shape first, so Schema Registry sees the original schema before V2 adds the optional field:
Invoke-RestMethod -Method Post -Uri http://localhost:8080/lost-items/v1 -ContentType 'application/json' -Body '{"itemId":"wallet-42","description":"Brown leather wallet"}'Then publish V2 for the same item, including the new optional field:
Invoke-RestMethod -Method Post -Uri http://localhost:8080/lost-items -ContentType 'application/json' -Body '{"itemId":"wallet-42","description":"Brown leather wallet","reportedLocation":"Near the library"}'Finally publish an ItemFound event for the same key:
Invoke-RestMethod -Method Post -Uri http://localhost:8080/found-items -ContentType 'application/json' -Body '{"itemId":"wallet-42","foundLocation":"Library front desk"}'Both email and SMS logs should show the V1, V2, and ItemFound records. Because the same key is used, the records go to the same partition and keep their order within that partition. With three partitions, different item IDs can be spread across the topic.
Schema Registry records the schemas by Avro record name. Inspect registered subjects and the versions of the evolved lost-event schema:
Invoke-RestMethod http://localhost:8081/subjects
Invoke-RestMethod http://localhost:8081/subjects/com.example.lostfound.avro.ItemReportedLost/versionsreportedLocation is an Avro union of null and string with default: null. That default lets a V2 reader handle V1 data where the field is absent. The V1 endpoint is a learning aid; keep it ahead of V2 when creating a fresh Registry subject under the default backward-compatibility policy.
The first app process has one email consumer in email-group. Start a second PowerShell window in the project folder and run a second app instance with only the email listener enabled:
$env:EMAIL_CONSUMER_ENABLED = 'true'
$env:SMS_CONSUMER_ENABLED = 'false'
$env:AUDIT_CONSUMER_ENABLED = 'false'
$env:KAFKA_CLIENT_ID = 'email-instance-2'
.\mvnw.cmd spring-boot:run '-Dspring-boot.run.arguments=--server.port=8082'The second instance joins the same email-group. With three partitions, Kafka divides the partitions between the two email consumers and rebalances when the second instance joins. Stop it with Ctrl+C to see another rebalance.
While both are running, inspect lag and membership:
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group email-group
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group email-group --members --verbose
docker compose exec kafka /opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group sms-groupCompare the groups: email-group shares partitions between its two members; sms-group is separate and still receives every event independently.
Import postman/Lost-Found-Project-2.postman_collection.json and postman/Lost-Found-Local.postman_environment.json. Select the local environment. The collection lists the V1 request first, then V2 and ItemFound, to make the schema evolution sequence easy to follow.
See docs/PROJECT-3.md for the retry and DLT demo, idempotent PostgreSQL notification jobs, record versus manual acknowledgement, producer idempotence, and the separate Kafka transaction exercise.