Files
spring-messaging-demo/debezium-cdc/README.md
T

63 lines
3.5 KiB
Markdown

# debezium-cdc
Companion code for the ankurm.com post *Debezium CDC with Spring Boot: Stream Postgres Changes to Kafka (and When to Prefer the Outbox)*.
One Postgres 18 database, one Kafka 4.4 broker, and Debezium running two ways against the same tables:
* **Kafka Connect** with the Debezium Postgres connector (`connect/*.json`, registered over REST).
* **The embedded engine** inside a Spring Boot app (`EmbeddedDebezium.java`), forwarding to Kafka with `KafkaTemplate`.
No `docs/` folder: the explanations live in the post, every transcript lives in `output/`.
## Versions (all read from Maven Central or the image, not from announcements)
| Part | Version |
|---|---|
| JDK | Temurin 25.0.5.1 |
| Spring Boot | 4.2.0 (Spring for Apache Kafka 4.2.0, kafka-clients 4.4.0) |
| Debezium | 3.7.1.Final (engine and the `quay.io/debezium/connect` image; 3.8.0.Alpha2 exists but is not GA) |
| Postgres | 18.7 (`quay.io/debezium/postgres:18`, `wal_level=logical` preset) |
| Kafka broker | `apache/kafka:4.4.0`, KRaft, 3 partitions per auto-created topic |
| Kafka Connect runtime inside the Debezium image | 4.3.1 |
## Run it
```bash
export JDK25_HOME=/path/to/jdk-25 # defaults to this author's sandbox path
./scripts/run-all.sh # fresh stack, builds, runs every scenario, rewrites output/ (about 12 minutes)
```
or step by step: `./scripts/up.sh`, then `./scripts/01-logical-replication.sh` and so on in order. The scenarios
build on each other (01 registers the connector 02 to 04 read from).
## Layout
| Path | What it is |
|---|---|
| `docker-compose.yml` | Postgres, Kafka, Kafka Connect |
| `sql/init.sql` | `orders`, `outbox`, `noise`, `heartbeat` tables and two seed rows |
| `connect/orders-connector.json` | Connector for the business table |
| `connect/outbox-connector.json` | Connector for the outbox table with the `EventRouter` transform |
| `src/main/java/com/ankurm/cdc/EmbeddedDebezium.java` | The embedded engine as a Spring `SmartLifecycle` bean |
| `src/main/java/com/ankurm/cdc/OrderChangeListener.java` | A plain `@KafkaListener` that handles tombstones and duplicates |
| `src/main/java/com/ankurm/cdc/ChangeEvent.java` | Reads the envelope (same for embedded and Connect) |
| `src/test/java/com/ankurm/cdc/ChangeEventTest.java` | Pins the envelope parsing, no infrastructure needed |
## Output index
| File | Shows |
|---|---|
| `output/01-logical-replication.txt` | Server settings, a `test_decoding` peek at the raw logical stream, the slot and publication the connector creates |
| `output/02-event-format.txt` | Snapshot, insert, update, delete and tombstone envelopes under the default replica identity |
| `output/03-replica-identity.txt` | `REPLICA IDENTITY FULL`, and an `ALTER TABLE ADD COLUMN` on a live connector |
| `output/04-ordering.txt` | Per-key order, no global order, one transaction spread across partitions |
| `output/05-outbox.txt` | `EventRouter` output, including an outbox row inserted and deleted in one transaction |
| `output/06-wal-retention.txt` | WAL pinned by a quiet slot, by an orphaned slot, and the heartbeat fix |
| `output/07-embedded.txt` | The embedded engine, a diff against the Connect event, and a `kill -9` redelivery |
## Things this module does not do
* No schema registry or Avro: JSON without the schema wrapper, so the transcripts are readable.
* No exactly-once claim anywhere: the redelivery in `07` is the point.
* Nothing here is hardened for production (plaintext passwords in compose, one broker, replication factor 1).