# DBLog: A Reference Implementation (dblog-impl)

Canonical URL: https://aandreakis.com/dblog-impl
Markdown URL: https://aandreakis.com/dblog-impl.md
GitHub Repository: https://github.com/aandreakis/dblog-impl
Author: Andreas Andreakis (https://orcid.org/0009-0003-9025-9402)
DBLog Conceptual Guide: https://aandreakis.com/dblog (Markdown: https://aandreakis.com/dblog.md)
Original Paper: https://arxiv.org/abs/2010.12597 (DOI: https://doi.org/10.48550/arXiv.2010.12597)
Netflix Technology Blog: https://netflixtechblog.com/dblog-a-generic-change-data-capture-framework-69351fb9099b
Companion Essay: https://aandreakis.com/posts/i-vibe-coded-my-old-netflix-project (Markdown: https://aandreakis.com/posts/i-vibe-coded-my-old-netflix-project.md)
Hydroscope TUI Docs: https://aandreakis.com/dblog-impl/ops/tap-tui/docs (Markdown: https://aandreakis.com/dblog-impl/ops/tap-tui/docs.md)
License: MIT

---

dblog-impl is a Java 21 reference implementation of DBLog, by its creator Andreas Andreakis. DBLog is a Change-Data-Capture (CDC) algorithm that copies the full state of a source database alongside live changes, without blocking writes or stalling change event propagation. This repository makes the published algorithm executable, inspectable, and testable against real MySQL (binlog) and PostgreSQL (pgoutput) streams.

DBLog originated at Netflix and was published in the [Netflix Technology Blog post](https://netflixtechblog.com/dblog-a-generic-change-data-capture-framework-69351fb9099b) (2019) and the [DBLog paper](https://arxiv.org/abs/2010.12597) (2020). Its watermarked-chunk mechanism later informed incremental snapshots in [Debezium](https://debezium.io/) and [Apache Flink CDC](https://nightlies.apache.org/flink/flink-cdc-docs-stable/).

This repository is a reference implementation for study, verification, and experimentation, not a production connector. For production CDC with DBLog-based incremental snapshots, use a maintained system such as [Debezium](https://debezium.io/) or [Apache Flink CDC](https://nightlies.apache.org/flink/flink-cdc-docs-stable/). For a visual introduction to the algorithm first, read [An introduction to DBLog](https://aandreakis.com/dblog).

## Architecture Summary

`dblog-impl` separates the source-neutral watermark state machine from database-specific log readers, sinks, and local operational tooling:

| Component | Included in `dblog-impl` |
| --- | --- |
| Runtime | Java 21, Spring Boot, Gradle |
| Core algorithm | Source-neutral watermark window reconciler, chunked table dump coordinator, targeted primary-key repair coordinator, and single-loop request pump |
| Sources | MySQL binlog streaming and PostgreSQL `pgoutput` logical replication |
| Sinks | NDJSON stream/file, typed H2 inspection sink, JDBC target apply (PostgreSQL and MySQL), and explicit no-op sink |
| State and checkpoints | Embedded H2 store tracking source checkpoints, table schemas, operator requests, and completed-chunk progress |
| Control plane | Local HTTP API (`127.0.0.1:8085` by default) for runtime status, metrics, request submission, and inspection |
| Verification | Unit tests, adapter/state integration tests, Docker-backed end-to-end convergence tests, and multi-version database compatibility matrix |
| Visualizer | Optional Hydroscope Rust/ratatui terminal UI over an educational tap stream (off by default) |

## Database Adapter Compatibility Matrix

Tested automatically via `./gradlew compatibilityMatrix` against disposable Docker containers:

| Source Database | Capture Protocol | Tested Docker Images | Adapter Notes |
| --- | --- | --- | --- |
| MySQL | Row-based `binlog` | `mysql:8.0`, `mysql:8.4`, `mysql:9.6` | Requires full row images and write access to the source watermark table. See [docs/adapters/mysql.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/adapters/mysql.md). |
| PostgreSQL | Logical replication (`pgoutput`) | `postgres:14`, `postgres:15`, `postgres:16`, `postgres:17`, `postgres:18` | Chunk `SELECT` waits until tracked log commits that modified the table being read are visible to the snapshot ([table-scoped commit visibility](https://github.com/aandreakis/dblog-impl/blob/main/docs/adapters/postgres.md#commit-visibility)). `TRUNCATE` on captured tables is not captured ([details](https://github.com/aandreakis/dblog-impl/blob/main/docs/adapters/postgres.md#truncate-is-not-captured)). |

## How the Watermark Algorithm Works (Worked Example)

A chunked table read can race with live database writes. DBLog makes that race explicit and deterministic by bracketing each chunk read with two markers written to a dedicated watermark table in the source database. Because all tables share one transaction log, both watermarks appear in commit order alongside normal row changes.

Worked example for a chunk selecting `pk=41`, `pk=42`, and `pk=43` while live log events arrive:

1. **Stream live changes.** The connector consumes committed source-log events (for example, `UPDATE pk=39`) and emits them in order to the output stream.
2. **Pause log reading and write `LOW`.** To begin a chunk, the connector briefly pauses log reading at the current log position and writes a **low watermark** (`LOW`) into the source watermark table.
3. **Select a bounded chunk.** The connector runs a primary-key-ordered `SELECT` for the next chunk (`pk=41`, `pk=42`, `pk=43`) and holds those rows in an in-memory buffer. They are not emitted yet. (On PostgreSQL, this read first waits until tracked log commits that modified the table being read are visible to the chunk snapshot.)
4. **Write `HIGH` and resume.** Immediately after the chunk `SELECT` finishes, the connector writes a **high watermark** (`HIGH`) into the source watermark table and resumes log reading from the pause point. Application writes at the source are never blocked.
5. **Reconcile collisions inside the pause-to-HIGH window (log wins).** In logical output order, every in-window log event (for example, `DELETE pk=37`, `UPDATE pk=42`) stays in log order. If an in-window log event targets the same table and primary key as a buffered chunk row (`UPDATE pk=42` on the captured table), the buffered chunk row for `pk=42` is evicted. By `HIGH`, that key's last emitted state from the log is at least as new as the selected row.
6. **Flush survivors at `HIGH`.** When `HIGH` appears on the log stream, the watermark window closes. Watermark markers (`LOW` and `HIGH`) are omitted from downstream output. In logical output order, the in-window log events come first, followed by the surviving buffered rows (`pk=41` and `pk=43`) in primary-key order, and then subsequent live log events (`UPDATE pk=46`, `DELETE pk=39`).

**Batch delivery and durability detail (implemented in `dblog-impl`):** In the actual `dblog-impl` runtime (`DefaultDumpWindowCoordinator.java` and `RuntimeRequestPump.java`), in-window log events and surviving chunk rows are accumulated in memory until `HIGH` closes the window and then appended to the sink as one reconciled batch. Completed-chunk progress is persisted to the embedded H2 state store before acknowledging the source log checkpoint past `HIGH`. If the process restarts mid-window, replay resumes from the pre-window checkpoint rather than skipping an unfinished chunk.

## Key Source Files (Reading Path)

| File | Purpose |
| --- | --- |
| [docs/PAPER_MAP.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/PAPER_MAP.md) | Maps every Algorithm 1 step from the 2020 paper to Java classes, fail-closed guards, test locks, and explicit paper deltas |
| [WindowReconciler.java](https://github.com/aandreakis/dblog-impl/blob/main/src/main/java/io/github/aandreakis/dblog/core/reconcile/WindowReconciler.java) | Low/high watermark window state machine and collision-driven row eviction |
| [DefaultDumpWindowCoordinator.java](https://github.com/aandreakis/dblog-impl/blob/main/src/main/java/io/github/aandreakis/dblog/core/request/DefaultDumpWindowCoordinator.java) | Opens bounded chunk windows, runs chunk selects, and persists restart boundaries |
| [DefaultTargetedRepairCoordinator.java](https://github.com/aandreakis/dblog-impl/blob/main/src/main/java/io/github/aandreakis/dblog/core/request/DefaultTargetedRepairCoordinator.java) | Re-reads and repairs specific primary keys using the same watermark window machinery |
| [RuntimeRequestPump.java](https://github.com/aandreakis/dblog-impl/blob/main/src/main/java/io/github/aandreakis/dblog/runtime/loop/RuntimeRequestPump.java) | Interleaves live log streaming with queued dump and repair requests |
| [PostgresVisibilityGuard.java](https://github.com/aandreakis/dblog-impl/blob/main/src/main/java/io/github/aandreakis/dblog/adapter/postgres/internal/PostgresVisibilityGuard.java) | Table-scoped PostgreSQL commit-visibility guard ensuring chunk reads see tracked log commits |
| [ops/tap-tui/src/bin/hydroscope.rs](https://github.com/aandreakis/dblog-impl/blob/main/ops/tap-tui/src/bin/hydroscope.rs) | Hydroscope Rust/ratatui three-pane terminal UI renderer |
| [docs/OPERATION.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/OPERATION.md) | Configuration properties, boot modes, sinks, and operational behavior |
| [docs/CONTROL_PLANE.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/CONTROL_PLANE.md) | Local HTTP API contract, request lifecycle, and educational tap notes |
| [docs/adapters/mysql.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/adapters/mysql.md) | MySQL binlog adapter preconditions and limits |
| [docs/adapters/postgres.md](https://github.com/aandreakis/dblog-impl/blob/main/docs/adapters/postgres.md) | PostgreSQL `pgoutput` adapter preconditions, commit visibility, and limits |
| [AGENTS.md](https://github.com/aandreakis/dblog-impl/blob/main/AGENTS.md) | Architecture invariants and guidance for coding agents inspecting the repository |

## Quick Start

Prerequisites: Java 21, Docker (for demos and Docker-backed tests), Python 3.9+ (for `scripts/demo/*.py`), and Rust stable (only if building Hydroscope).

Run the shortest end-to-end demo (MySQL source to PostgreSQL target):

```bash
python3 scripts/demo/mysql_to_postgres.py
```

On Windows:

```powershell
py -3 scripts/demo/mysql_to_postgres.py
```

Other bundled demos:

```bash
python3 scripts/demo/mysql_to_ndjson.py
python3 scripts/demo/postgres_to_mysql.py
```

The demo starts disposable local containers bound to `127.0.0.1`, runs DBLog on the host, submits an `ALL_TABLES` dump over the local HTTP control plane, verifies initial convergence, applies live writes, and verifies convergence again. On success it prints `Initial dump converged.`, `Live changes converged.`, and `Demo succeeded.` (exit 0). Set `DBLOG_DEMO_KEEP_CONTAINERS=1` to keep fixture containers running after exit.

Run verification suites:

```bash
./gradlew test                  # fast unit tests (< 1 min)
./gradlew integrationTest       # adapter and state integration tests
./gradlew integrationTestDocker # Docker-backed adapter integration tests
./gradlew e2eTest               # inspection-mode recovery, drift, and failure tests
./gradlew e2eTestDocker         # live Docker convergence and repair scenarios
./gradlew compatibilityMatrix   # mysql:8.0/8.4/9.6 and postgres:14-18 (20+ min)
```

Submit an on-demand dump manually against a running local process:

```bash
curl -sS http://127.0.0.1:8085/api/v1/runtime/status
curl -sS -X POST http://127.0.0.1:8085/api/v1/requests \
  -H 'Content-Type: application/json' \
  -d '{"scope":"ALL_TABLES"}'
```

Build and run the Hydroscope visualizer (offline deterministic replay, no database or Docker required):

```bash
cd ops/tap-tui
cargo build --release --bin hydroscope
./target/release/hydroscope --scenario showcase
```

Start `./target/release/hydroscope` without `--scenario` to attach to a live DBLog runtime with the educational tap enabled. See [Hydroscope TUI Documentation](https://aandreakis.com/dblog-impl/ops/tap-tui/docs) for live-mode setup and UI anatomy.

## Scope Boundaries

- Implements the 2019/2020 DBLog algorithm (chunked snapshots and targeted key repair for existing keys) and does not implement the `ABSENT` / orphan-row repair extension.
- Single process, single host: no high availability, leader election, leases, or failover takeover.
- Local submit/query HTTP control plane only: no pause, resume, or cancel endpoints.
- Embedded H2 state only: no external distributed state store.
- MySQL and PostgreSQL sources only; NDJSON, H2 inspection, JDBC apply, and no-op sinks only.
- Conservative schema handling: no online schema-evolution workflow, DDL replay, or schema-history topic.
- At-least-once delivery: JDBC apply is idempotent by primary-key upsert/delete; NDJSON consumers must deduplicate.
- Low-maintenance and feature-stable by design: small bug fixes within existing scope may be considered ([CONTRIBUTING.md](https://github.com/aandreakis/dblog-impl/blob/main/CONTRIBUTING.md)); feature requests and support asks are out of scope.

## Sources and Related Reading

- [An introduction to DBLog](https://aandreakis.com/dblog): Conceptual guide to DBLog, incremental snapshots in Debezium and Apache Flink CDC, sink repairs, and orphan rows.
- [DBLog: A Watermark Based Change-Data-Capture Framework](https://arxiv.org/abs/2010.12597): Andreas Andreakis and Ioannis Papapanagiotou, arXiv, 2020 (DOI: `10.48550/arXiv.2010.12597`).
- [DBLog: A Generic Change-Data-Capture Framework](https://netflixtechblog.com/dblog-a-generic-change-data-capture-framework-69351fb9099b): Andreas Andreakis and Ioannis Papapanagiotou, Netflix Technology Blog, 2019.
- [I Vibe Coded My Old Netflix Project](https://aandreakis.com/posts/i-vibe-coded-my-old-netflix-project): Notes on building and testing `dblog-impl` with coding agents.
- [Hydroscope TUI Documentation](https://aandreakis.com/dblog-impl/ops/tap-tui/docs): Walkthrough of the bundled Rust terminal visualizer.

## Notice and License

Built from the published [DBLog paper](https://arxiv.org/abs/2010.12597) and [Netflix Technology Blog post](https://netflixtechblog.com/dblog-a-generic-change-data-capture-framework-69351fb9099b). Independent open-source reference implementation for study and testing, not Netflix's internal production codebase, not intended for production CDC, and not affiliated with, endorsed by, or sponsored by Netflix.

Released under the MIT License. See [LICENSE](https://github.com/aandreakis/dblog-impl/blob/main/LICENSE), [NOTICE](https://github.com/aandreakis/dblog-impl/blob/main/NOTICE), and [PATENTS.md](https://github.com/aandreakis/dblog-impl/blob/main/PATENTS.md).
