Skip to content
baselabsPublic

About

Framework-agnostic Elixir CDC consumer for MySQL binary-log replication (row-based binlog) — streams committed transactions to a pluggable sink, fail-closed; at-least-once, or effect-once in sink-owned checkpoint mode.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

capstan

Hex.pm Docs License Run in Livebook

capstan is MySQL CDC on the BEAM: it connects to MySQL as a replica, tails the row-based binary log from a GTID position, and delivers committed transactions to your sink — durably checkpointed, at-least-once, and fail-closed on every condition that could otherwise lose or corrupt data silently. It is replicant's MySQL sibling.

It is a library in your supervision tree, not a daemon: no JVM, no ports, no second runtime. The MySQL replication-protocol client is in-library and pure Elixir (:gen_tcp/:ssl/:crypto only).

When to use it

You want capstan
Row-level change streams out of MySQL 8+ into Elixir ✅ this is exactly it
An initial backfill of existing rows, then a gap-free handoff to the stream ✅ built in (:snapshot)
Delivery guarantees without operating Debezium/Maxwell ✅ one dependency, your tree
Schema-change notifications without DDL text leaking into logs ✅ DDL redacted by design
A source with binlog_transaction_compression=ON ✅ consumed — the in-library zstd decoder inflates each compressed transaction (byte-exact conformance vs the reference zstd)
XA (two-phase) transactions ✅ tracked or refused, never half-delivered (xa: policy)
Amazon Aurora MySQL v3 as the source ✅ named + documented (ADR-0013); a failover resets the cycle budget and the checkpoint set gains both writers' UUIDs

Install

def deps do
  [
    {:capstan, "~> 1.3"}
  ]
end

MySQL requirements, checked at every connect (one query, distinct refusal per violation): binlog_format=ROW, binlog_row_image=FULL, binlog_row_metadata=FULL, binlog_row_value_options='' (full JSON), gtid_mode=ON, log_bin=ON (the binary log enabled — on Aurora this is the one variable that names a disabled binlog, because binlog_format still reads ROW when the cluster group turns logging off; ADR-0013). Amazon Aurora MySQL version 3 through the cluster writer endpoint is a named, documented source (ADR-0013), and its failover contract is verifiable locally with no AWS account — three real MySQL nodes behind a flipping endpoint: scripts/aurora-sim. Run scripts/capstan-preflight.sql against a prospective source for a read-only readiness report.

A 30-second pipeline

defmodule MyApp.OrderSink do
  @behaviour Capstan.Sink

  @impl true
  def handle_transaction(txn) do
    Enum.each(txn.changes, fn change ->
      # change.op ∈ {:insert, :update, :delete}; change.record is column => value
      MyApp.Projector.apply!(change)
    end)

    {:ok, txn.position}   # persist this atomically with your own write for effect-once
  end
end

defmodule MyApp.CDC do
  def start_link(opts) do
    Capstan.start_link(
      connection: [
        host: "db.internal",
        username: "capstan",
        password: opts[:password]
      ],
      server_id: 4242,
      sink: MyApp.OrderSink,
      checkpoint_store: [module: MyApp.CheckpointStore],
      tables: [{"shop", "orders"}]
    )
  end
end

That is the whole core: connect, stream, deliver, checkpoint. Kill it, restart it — it resumes from the durable checkpoint with no transaction lost (the sink sees replays at worst; make it idempotent — that is the contract).

Try it live (kill/restart resume included) in the getting-started Livebook, or run the minimal printable consumer in examples/. For the complete production shape as one runnable stack — receipts, idempotent mirror, durable checkpoint, all in docker — see examples/replication_pipeline.

The three ways to own your position

1. Lib-owned checkpointing (default). You implement a two-callback Capstan.CheckpointStore (read/write a GTID-set string — a table row, a file, Redis, anything durable). capstan delivers a transaction, then checkpoints. Crash between the two → the transaction is re-delivered on restart: at-least-once.

2. Sink-owned checkpointing (effect-once). Your sink implements checkpoint/0 and persists the position atomically with its own write — the delivery and the position advance become ONE transaction. capstan never checkpoints past a write the sink has not confirmed.

3. Batched (batch: [max_transactions:, flush_ms:, mode:]). The checkpoint write covers N transactions: :lib_owned batches the checkpoint writes; :sink_owned hands your handle_batch/2 the whole list + final position for one atomic write. Wider crash-replay window, fewer round trips.

Backfill existing rows first

Add a :snapshot block and capstan backfills pre-existing rows before (and concurrently with) the stream, gap-free — a per-chunk brief read lock pins each chunk's exact GTID position, and a cursor gate suppresses stream changes a chunk already covers. Kill it mid-backfill and it resumes per-table from durable cursors.

snapshot: [
  tables: [{"shop", "orders"}, {"shop", "customers"}],   # or :all (scoped base tables)
  store: [module: MyApp.SnapshotStore],
  chunk_size: 4096
]

Every table gets exactly one completion signal — a final_chunk?: true beat to handle_snapshot/2 (empty tables get exactly one empty final chunk). Primary keys may be integers, BINARY/VARBINARY, CHAR/VARCHAR (any charset and collation — the gate compares the server's own collation sort keys, not string bytes), or composites of those; the TEXT family and ENUM/SET keys are refused with a named halt rather than backfilled in a wrong order.

Fail-closed, everywhere

Any condition that could silently lose or corrupt data halts the pipeline ({:shutdown, {:halt, reason}}; every child is restart: :temporary, so the halt is yours to handle): a purged binlog range, a server identity mismatch, a malformed event, an unknown transaction shape, an unconsumable compression type, a sink error, a schema change mid-backfill. Halts surface as [:capstan, :connection, :halt] / [:capstan, :assembler, :halt] / [:capstan, :snapshot, :halt] telemetry with a value-free reason. No row value, no DDL text, no password ever appears in a halt reason, log line, or telemetry payload — that boundary is enforced at runtime, not by convention.

Where to go next

  • examples/replication_pipeline — the durable reference implementation as a docker stack: one docker compose up runs MySQL source → capstan (one OTP release container) → MySQL destination with value-free receipts, an idempotent upsert mirror, and a durable GTID checkpoint you can kill and restart mid-life.
  • usage-rules.md — the full operating contract: every option, every halt reason, delivery semantics, TLS posture, XA policy.
  • docs/telemetry.md — every telemetry event with its measurements and metadata, plus attach examples.
  • docs/recipes.md — idempotent sinks, warehouse loading, snapshot-then-stream migration, batching tradeoffs, TLS, "start from now", XA sources, and the Amazon Aurora MySQL recipe (cluster parameter group, retention hours, the writer endpoint, the AWS CA bundle).
  • ADRs — the decision record (position model, fail-closed posture, snapshot cursor gate, XA tracking, zstd consumption, …).
  • CHANGELOG.

Repository layout

Directory Contents
lib/ The Elixir library (Hex package capstan)
notebooks/ The getting-started Livebook
examples/ Runnable minimal consumers (examples/README.md)
scripts/ The MySQL init seed (mysql-init/), the read-only preflight report, and aurora-sim/ (the local Aurora-failover harness)
probe/ The executed protocol viability probe + committed evidence
docs/ ADRs, telemetry reference, recipes
usage-rules.md Consumer-facing usage contract

Support & maintenance

capstan is maintained as the MySQL side of the baselabs CDC stack (replicant's sibling). Issues and pull requests are welcome; there is no support SLA.

Publication

The dispatch-only publish.yml runs from the default branch and publishes new versions of existing Hex.pm packages only. It refuses a package name the registry has never had; a first publication is the owner's decision.

The build job builds and inspects the package and docs without a publishing environment. It passes the archives, checksums and upload plan to upload, the only job in environment publish. That job runs no project code, verifies the checksums, and exchanges GitHub OIDC for a 15-minute Hex workload token scoped to this public package. It uploads with the pinned Hex client and revokes the token even after failure. No stored Hex key is used. Only this job has id-token: write; every job retains the publisher actor restrictions.

A second dispatch on the same commit finishes a staged or partial publication: an existing package must match the inspected bytes, served docs are kept, and nothing is replaced. The read-back job runs after a successful upload or when nothing needs uploading. It checks the served package and docs, including the docs' release.json commit, and assembles the existing release-assets artifact without a publishing environment or token.

License

MIT — see LICENSE.

About

Framework-agnostic Elixir CDC consumer for MySQL binary-log replication (row-based binlog) — streams committed transactions to a pluggable sink, fail-closed; at-least-once, or effect-once in sink-owned checkpoint mode.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages