Building a Kafka Event-Driven Spring Boot Application with Avro, Schema Registry and PostgreSQL

Published: (January 4, 2026 at 04:28 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Cover image for Building a Kafka Event-Driven Spring Boot Application with Avro, Schema Registry and PostgreSQL

Introduction

If you’re building event‑driven systems with Apache Kafka, you must think about data contracts early. This post shows a practical, end‑to‑end Spring Boot example using:

  • Apache Kafka
  • Confluent Schema Registry
  • Avro serialization
  • PostgreSQL
  • Docker Compose

Full source code is provided in the repository linked at the end of the article.

Why Schema Registry + Avro?

JSON works… until it doesn’t. Common problems in Kafka‑based systems:

  • Breaking consumers when producers change payloads
  • No schema versioning
  • Unclear data contracts between teams

Avro + Schema Registry solves this by:

  • Enforcing schema compatibility
  • Allowing safe schema evolution
  • Decoupling producers from consumers

Architecture Overview

Client (Postman)
      |
      v
Spring Boot Producer (REST)
      |
      v
Kafka Topic (users.v1)
      |
      v
Spring Boot Consumer
      |
      v
PostgreSQL
  • Producer exposes POST /users
  • Payload is converted to an Avro record
  • Message is published to Kafka
  • Consumer deserializes Avro and persists data to PostgreSQL

What This Demo Includes

  • Spring Boot Kafka Producer (Avro)
  • Spring Boot Kafka Consumer (Avro)
  • Confluent Schema Registry
  • PostgreSQL persistence using Spring Data JPA
  • Schema evolution with backward compatibility
  • Docker Compose for local development

Local Setup (Kafka + Schema Registry + PostgreSQL)

Prerequisites

  • Java 21
  • Maven
  • Docker & Docker Compose

Start infrastructure

docker compose up -d

Services started:

  • Kafka → localhost:29092
  • Schema Registry → (address omitted in original)
  • PostgreSQL → localhost:5432

Run the Applications

Consumer

cd consumer-app
mvn spring-boot:run

The consumer listens to users.v1 and persists messages to PostgreSQL.

Producer

cd producer-app
mvn spring-boot:run

The producer exposes a REST endpoint.

Produce an Event

curl -X POST http://localhost:8080/users \
  -H "Content-Type: application/json" \
  -d '{
    "id": "u-1",
    "email": "user@test.com",
    "firstName": "John",
    "lastName": "Doe",
    "isActive": true,
    "age": 30
  }'

You’ll see:

  • Avro schema registered (or validated)
  • Message published to Kafka
  • Consumer saving the record to PostgreSQL

Schema Evolution (The Important Part)

Avro allows safe evolution when rules are respected.

Typical steps:

  1. Add a new optional field.
  2. Provide a default value.
  3. Keep compatibility set to BACKWARD.

Schema Registry ensures:

  • Old consumers keep working.
  • New producers don’t break the system.

This demo is designed to show real‑world schema evolution, not toy examples.

Confluent Cloud Ready

The project also supports Confluent Cloud via Spring profiles:

  • SASL/SSL
  • Schema Registry API keys
  • use.latest.version=true
  • auto.register.schemas=false

Perfect for CI/CD pipelines.

Source Code

GitHub repository includes:

  • Docker Compose configuration
  • Avro schemas
  • Producer & consumer applications
  • PostgreSQL setup
  • Postman collection

Who Is This For?

  • Java & Spring Boot developers
  • Kafka users moving beyond JSON
  • Teams building event‑driven microservices
  • Anyone learning Schema Registry + Avro

Final Thoughts

This is a production‑style Kafka example, not a simple hello‑world. If you’re serious about:

  • Schema contracts
  • Backward compatibility
  • Safe evolution
  • Real persistence

then this demo will save you a lot of trial and error.

Feel free to star the repository if it helped you, or fork it and adapt it to your own system.

Back to Blog

Related posts

Read more »

Spring Cloud 마이크로서비스 - Eureka와 Multi-Module 프로젝트

마이크로서비스와 Spring Cloud 마이크로서비스 아키텍처는 대규모 애플리케이션을 작은 서비스 단위로 분리하여 독립적으로 개발, 배포, 확장할 수 있게 합니다. Spring Cloud는 이러한 마이크로서비스 구축을 위한 다양한 도구를 제공합니다. Eureka는 Netflix OSS...

Spring Cloud Gateway: Basic Example

!Cover image for Spring Cloud Gateway: Basic Examplehttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fd...