Open-Source Wikis

/

CockroachDB

/

Features

/

Changefeed (CDC)

cockroachdb/cockroach

Changefeed (CDC)

CockroachDB's change data capture exposes every committed row mutation as a stream of events. Implementation lives mostly in pkg/ccl/changefeedccl/, with rangefeed plumbing in pkg/kv/kvserver/rangefeed/ and pkg/kv/kvclient/rangefeed/.

Data flow

graph LR
  KV["KV writes"] --> RF["per-replica rangefeed processor"]
  RF -->|events| Aggregator["changefeedccl aggregators"]
  Aggregator --> Encoder["row encoder (Avro / JSON / Parquet / Topic)"]
  Encoder --> Sink["sink (Kafka, Pub/Sub, webhook, S3, …)"]
  Aggregator --> Resolved["resolved-timestamp checkpoints"]
  Resolved -->|persist| Job["jobs registry"]

Components

  • Rangefeed processorpkg/kv/kvserver/rangefeed/. Reads from MVCC and a closed-timestamp source, emits values and checkpoints over a gRPC stream.
  • Rangefeed clientpkg/kv/kvclient/rangefeed/. The consumer side; handles backfills and reconnects.
  • Aggregatorpkg/ccl/changefeedccl/changefeed_processors.go. A DistSQL processor on each gateway that collects events from many ranges, deduplicates, and orders by resolved timestamp.
  • Encoderspkg/ccl/changefeedccl/encoder.go and friends. Serializes rows to JSON, Avro, CSV, or Parquet; supports schema-registry integration for Avro.
  • Sinkspkg/ccl/changefeedccl/sink_*.go. One file per destination: Kafka (v2 native producer + an older sink), Google Pub/Sub, webhooks, S3-compatible buckets, Pulsar, Azure Event Hubs, Snowflake.
  • Schema feedpkg/ccl/changefeedccl/schemafeed/. Watches descriptor changes so that schema-version-aware encoders can re-resolve types.

Job lifecycle

A CREATE CHANGEFEED statement creates a JobType_CHANGEFEED job (pkg/jobs/jobspb/). The resumer (pkg/ccl/changefeedccl/changefeed_dist.go) plans a DistSQL flow with one aggregator per gateway and one rangefeed source per range owned by a node. Progress is recorded as a "resolved high-water timestamp" in the job's progress; on restart, the changefeed resumes from that timestamp.

Guarantees

  • At-least-once delivery is the default. Exactly-once is delegated to the sink (Kafka transactional, table-based dedup).
  • Per-key order is preserved.
  • Resolved-timestamp checkpoints are emitted on a configurable cadence and indicate "no new events at or below this timestamp will appear".

Performance

Changefeeds are tuned by:

  • changefeed.event_consumer_workers — fan-out per aggregator.
  • changefeed.frontier_checkpoint_frequency and _max_bytes — checkpoint cadence.
  • kv.rangefeed.enabled — globally enable/disable rangefeeds.
  • The closed-timestamp interval in kv.closed_timestamp.target_duration controls minimum end-to-end latency.
File / package Purpose
pkg/ccl/changefeedccl/changefeedbase/ Shared types and options
pkg/ccl/changefeedccl/changefeed_dist.go Job planner
pkg/ccl/changefeedccl/kvfeed/ Reads rangefeed events into the change pipeline
pkg/ccl/changefeedccl/cdcevent/ Decoded change events
pkg/ccl/changefeedccl/schemafeed/ Schema/descriptor watcher
pkg/util/parquet/ Parquet encoder used by some sinks

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Changefeed (CDC) – CockroachDB wiki | Factory