core-cdc (CDC a.k.a Change Data Capture)
It provides the core mechanism and required resources to implement “Change Data Capture” services…
Installation
Install from PyPI using pip:
pip install core-cdc
uv pip install core-cdc # Or using UV...
Features
- Multi-Database CDC Support
MySQL Binary Log (BinLog) based change capture
MongoDB Change Streams for real-time event streaming
PostgreSQL logical replication (pgoutput) through psycopg2
Extensible processor architecture for additional database engines
- Comprehensive Event Handling
DML operations: INSERT, UPDATE, DELETE
DDL operations: CREATE, ALTER, DROP (schemas and tables), delivered to the targets as engine independent DdlEvent objects
Configurable event filtering by operation type
- Flexible Target Replication
Implement your own target by subclassing ITarget
Send records to any destination: database, queue, data warehouse, etc.
Support for multiple simultaneous targets
- Standardized Data Format
Common Record (data) and DdlEvent (schema changes) structures for cross-service integration
Includes metadata: timestamps, transaction IDs, source position
JSON serialization support for streaming and messaging systems
- Production-Ready Features
Error handling: a failing event or target is logged without stopping the stream
Comprehensive logging for monitoring and debugging
Optional event timestamp column for UPSERT/MERGE operations
Quick Start
Installation
Install the package:
pip install core-cdc
uv pip install core-cdc # Or using UV...
pip install -e ".[dev]" # For development...
Setting Up Environment
Install required libraries:
pip install --upgrade pip
pip install virtualenv
Create Python virtual environment:
virtualenv --python=python3.12 .venv
Activate the virtual environment:
source .venv/bin/activate
Install packages
pip install .
pip install -e ".[dev]"
Optional libraries
pip install '.[all]' # MySQL + MongoDB + PostgreSQL
pip install '.[mysql]' # MySQL BinLog support
pip install '.[mongo]' # MongoDB Change Streams support
pip install '.[postgres]' # PostgreSQL logical replication support
Check tests and coverage
python manager.py run-tests # unit tests
python manager.py run-tests --test-type integration
python manager.py run-coverage
The documentation is built with Sphinx, the docs extra has what it needs:
pip install -e ".[all,docs]"
cd docs && make clean && make html
Functional Tests
Functional tests require live database servers and are not discovered by pytest or tox automatically (files are named check_*.py to prevent accidental execution).
The helper script tests/functional/quick_test.sh checks connectivity, runs both MySQL and MongoDB test suites, and prints a metrics summary (the PostgreSQL test is a separate file, see “Spinning Up Local Servers with Docker” below). They also check DDL events, transaction control queries, the JSON serialization of every type of column, the MongoDB invalidate events, the resume token and transactions (the tests of the column types change binlog_row_metadata in the server and restore it):
bash tests/functional/quick_test.sh
All connection parameters default to the Docker values below and can be overridden via environment variables (MYSQL_HOST, MYSQL_PASSWORD, MONGO_HOST, MONGO_DATABASE, etc.; the PostgreSQL test reads HOST_TEST_POSTGRES, PORT_TEST_POSTGRES, DATABASE_TEST_POSTGRES, USER_TEST_POSTGRES and PASSWORD_TEST_POSTGRES).
To run individual test files:
python manager.py run-tests --test-type functional --pattern "*.py"
Spinning Up Local Servers with Docker
The functional tests connect to these defaults. To use another port, map it in docker run and set the variable of the last column.
Engine |
Container |
Port |
Test |
Variable of the port |
|---|---|---|---|---|
MongoDB |
mongo1 to mongo3 |
27017 |
quick_test.sh |
PORT_TEST_MONGO |
MySQL |
(no name) |
3306 |
quick_test.sh |
PORT_TEST_MYSQL |
PostgreSQL |
pg |
5432 |
check_postgres_wal.py |
PORT_TEST_POSTGRES |
MongoDB Replica Set
Required for Change Streams (three nodes, then rs.initiate):
docker network create mongoCluster
docker run -d --rm -p 27017:27017 --name mongo1 --network mongoCluster \
mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo1
docker run -d --rm -p 27018:27017 --name mongo2 --network mongoCluster \
mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo2
docker run -d --rm -p 27019:27017 --name mongo3 --network mongoCluster \
mongo:5 mongod --replSet myReplicaSet --bind_ip localhost,mongo3
docker exec -it mongo1 mongosh --eval "rs.initiate({
_id: \"myReplicaSet\",
members: [
{_id: 0, host: \"mongo1\"},
{_id: 1, host: \"mongo2\"},
{_id: 2, host: \"mongo3\"}
]
})"
Check cluster status:
docker ps
docker exec -it mongo1 mongosh --eval "rs.status()"
MySQL
BinLog replication is enabled by default in the official image:
docker run \
--env=MYSQL_ROOT_PASSWORD=mysql_password \
--volume=/var/lib/mysql \
-p 3306:3306 \
--restart=no \
-d mysql:latest
PostgreSQL
Logical replication, for PostgresWalProcessor. It is one container, and the server has to be started with wal_level=logical:
docker run -d --rm --name pg -p 5432:5432 \
-e POSTGRES_PASSWORD=postgres_password \
postgres:16 -c wal_level=logical
# It must print "logical":
docker exec pg psql -U postgres -tAc "show wal_level"
-c wal_level=logical is what enables logical replication. With the default (replica) the server refuses to create the slot: logical decoding requires wal_level >= logical.
The postgres user is a superuser, so it has the REPLICATION attribute and nothing else has to be created.
The image tag is the version of PostgreSQL: 14, 15, 16, 17 and 18 were tested (for example postgres:18).
If 5432 is taken (a local PostgreSQL, for example) map another host port, -p 5433:5432, and run the test with PORT_TEST_POSTGRES=5433.
Run the functional test. It creates (and drops) its own table, publication and slot, it needs the driver, and it is not part of quick_test.sh:
pip install -e ".[postgres]"
python -m pytest -s tests/functional/check_postgres_wal.py
Stopping the containers
The MongoDB and PostgreSQL containers were started with --rm, stopping them removes them:
docker stop mongo1 mongo2 mongo3 pg
The MySQL container has no name: use docker ps to find it and docker stop <id>.
Tested Versions
These are the versions the library was verified with. Live means the functional tests (tests/functional) ran against real servers; the unit and integration tests use mocks and do not need any server.
Component |
Versions |
How it was tested |
|---|---|---|
MySQL |
9.7.2, 8.4.11, 8.0.46, 5.7.44 |
Live (official Docker images, ROW format). GTIDs on and off (9.7, 8.4). binlog_row_metadata FULL and MINIMAL (9.7, 8.4, 8.0). |
MongoDB |
6.0.28, 5.0.31 |
Live (replica set, official mongo:6 and mongo:5 images): change streams, DDL, invalidate, resume tokens and transactions. In 6.0 also the expanded events (create, createIndexes, dropIndexes, modify) and wallTime. |
PostgreSQL |
18.6, 17.11, 16.13, 15.17 and 14.24 (pgoutput, wal_level=logical) |
Live (official postgres images, psycopg2-binary 2.9.13, Python 3.14 only): INSERT, UPDATE, DELETE, jsonb, TOAST columns, the update of the key, TRUNCATE, REPLICA IDENTITY FULL with primary_keys, the ALTER TABLE detection and the confirmation of the position (of a commit and while idle). Not tested: 10 to 13. |
Python |
CPython 3.9, 3.10, 3.11, 3.12, 3.13, 3.14 and PyPy 3.11 |
tox: unit and integration tests, and the live functional tests (MySQL 8.4 and MongoDB 5), on each one. |
Libraries |
mysql-replication 1.0.15 and 1.0.17; pymongo 4.17.0 and 4.18.1; PyMySQL 1.1.3 and 1.2.3; core-mixins 3.2.0 and 3.2.2 |
Both sets of versions ran the live tests. |
Minimum declared libraries |
mysql-replication 1.0.9, pymongo 4.0.0, core-mixins 3.2.0 |
Unit and integration tests on Python 3.9 (not live). |
Notes:
MySQL 5.7 must be started with the binary log enabled (--server-id=1 --log-bin=mysql-bin --binlog-format=ROW). Four of the six MySQL functional tests run there: the other two are about binlog_row_metadata, a variable that does not exist in 5.7 (its column types are read from the database, see the MySQL section).
MongoDB create, createIndexes, dropIndexes and modify events need MongoDB 6.0+ and showExpandedEvents; rename, drop, dropDatabase and invalidate are sent by 5.0 too. Only the sharding operations (shardCollection, refineCollectionShardKey, reshardCollection) were tested with simulated events, since they need a sharded cluster.
Not tested (they may work, there is no verification): MySQL 8.1 to 8.3 and the 9.x versions other than 9.7.2, MariaDB, Percona and managed services (Amazon RDS / Aurora, Cloud SQL, Atlas), MongoDB before 5.0 and 7.0 or later, other minor versions of the ones listed, and sharded MongoDB clusters.
Implemented CDC Engines
The following database engines have CDC implementations:
Fully Implemented
- MySQL - Binary Log (BinLog) based CDC
Uses mysql-replication library
Captures INSERT, UPDATE, DELETE operations
Supports DDL events (CREATE, ALTER, DROP)
Fallback mechanism for column name resolution
- MongoDB - Change Streams based CDC
Uses native MongoDB Change Streams
Captures INSERT, UPDATE, DELETE operations
Requires replica set configuration
Real-time event streaming
Planned / Documentation Only
MS SQL Server and Oracle implementations are not yet included. Reference guides and implementation templates are available in the documentation.
Contributing
Contributions are welcome! Please:
Fork the repository
Create a feature branch
Write tests for new functionality
Ensure all tests pass and coverage stays at 100 %: python manager.py run-coverage
Run the linters and type checkers: ruff check core_cdc, ty check core_cdc, mypy --explicit-package-bases core_cdc, pyright core_cdc and pylint core_cdc
Run the security checks: bandit -r core_cdc and pip-audit
Submit a pull request
License
This project is licensed under the MIT License. See the LICENSE file for details.
Links
Support
For questions or support, please open an issue on GitLab or contact the maintainers.
Release files for core-cdc 4.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| core_cdc-4.1.0.tar.gz | 38.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| core_cdc-4.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 75.7 kB
Release files / core_cdc-4.1.0.tar.gz
| Download URL | core_cdc-4.1.0.tar.gz |
|---|---|
| Size | 38.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8cfcaa0b1216ff5b05680a22c51d4c2ee06737f1483a8551a3a93aa71cd92c9
|
|
BLAKE2b-256 checksum How to use checksums |
38cdc3e33333b0907ca913d8af1bb9dd97ab26de833d62ed27261edca819c67b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / core_cdc-4.1.0-py3-none-any.whl
| Download URL | core_cdc-4.1.0-py3-none-any.whl |
|---|---|
| Size | 37.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e731540472b818d33bf5808af24c8bbde0b917ecacb285b4c8fda3babc2239d2
|
|
BLAKE2b-256 checksum How to use checksums |
55d893694bbe18f04ca649d5ae31f4df59503bad38220cd82f4a545d94f3bdfe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|