PostgreSQL CDC Example
A Rust-based Change Data Capture (CDC) application that streams real-time data changes from PostgreSQL to other databases (MySQL, SQL Server, etc.) using logical replication.
Overview
This project demonstrates how to build a production-ready CDC pipeline using the pg2any_lib
crate. It captures changes from PostgreSQL using logical replication and streams them to destination databases in real-time.
PostgreSQL Streaming logical Replication POC replication_checker_rs
Features
- Real-time streaming from PostgreSQL to multiple database types
- Logical replication with configurable replication slots and publications
- Comprehensive monitoring with Prometheus metrics and health checks
- Docker containerization for easy deployment
- Structured logging with configurable log levels
- Graceful error handling and automatic recovery mechanisms
Quick Start
Prerequisites
- Docker and Docker Compose
- PostgreSQL 10+ with logical replication enabled
- Destination database (MySQL 8.0+, SQL Server, etc.)
1. Clone and Setup
1 | git clone <your-repo> |
2. Environment Configuration
Copy and modify the environment file:
1 | cp .env .env.local |
Key environment variables:
1 | # Source PostgreSQL Database |
3. Run with Docker Compose
1 | # Start all services (PostgreSQL, MySQL, CDC app, Prometheus) |
4. Local Development
1 | # Install dependencies |
Configuration
Environment Variables
Variable | Description | Default | Required |
---|---|---|---|
CDC_SOURCE_CONNECTION_STRING |
PostgreSQL connection with replication | - | Yes |
CDC_DEST_TYPE |
Destination database type (MySQL/SqlServer) | - | Yes |
CDC_DEST_URI |
Destination database connection string | - | Yes |
CDC_REPLICATION_SLOT |
PostgreSQL replication slot name | cdc_slot |
No |
CDC_PUBLICATION |
PostgreSQL publication name | cdc_pub |
No |
CDC_PROTOCOL_VERSION |
Logical replication protocol version | 2 |
No |
CDC_BINARY_FORMAT |
Use binary format for data | false |
No |
CDC_STREAMING |
Enable streaming mode | true |
No |
CDC_CONNECTION_TIMEOUT |
Connection timeout (seconds) | 30 |
No |
CDC_QUERY_TIMEOUT |
Query timeout (seconds) | 10 |
No |
CDC_HEARTBEAT_INTERVAL |
Heartbeat interval (seconds) | 10 |
No |
METRICS_PORT |
Prometheus metrics port | 8080 |
No |
RUST_LOG |
Logging level | info |
No |
PostgreSQL Setup
Your PostgreSQL instance must have logical replication enabled:
1 | -- Check current settings |
Architecture
1 | ┌─────────────────┐ ┌───────────────────┐ ┌─────────────────┐ |
Components
- CDC Application: Main Rust application that handles replication
- Source Database: PostgreSQL with logical replication enabled
- Destination Database: Target database (MySQL, SQL Server, etc.)
- Monitoring: Prometheus metrics and health checks
Monitoring
Health Checks
The application exposes health endpoints:
- Health:
GET /health
- Application health status - Metrics:
GET /metrics
- Prometheus metrics
Prometheus Metrics
Available at http://localhost:9090
when using Docker Compose:
- Connection status
- Replication lag
- Message processing rates
- Error counts
Logs
Structured logging with configurable levels:
1 | # Set log level |
Troubleshooting
Common Issues
Connection Refused
1
2# Check if PostgreSQL allows replication connections
# Ensure pg_hba.conf has replication entriesReplication Slot Already Exists
1
2-- Drop existing slot if needed
SELECT pg_drop_replication_slot('cdc_slot');Permission Denied
1
2-- Grant necessary permissions
GRANT REPLICATION ON DATABASE your_db TO your_user;High Replication Lag
- Check network connectivity
- Monitor destination database performance
- Review CDC application logs
Performance Tuning
PostgreSQL Optimization
1 | -- Increase WAL settings for high throughput |
Application Tuning
1 | # Adjust timeouts for your environment |
Dependencies
- pg2any_lib - Core CDC functionality
- tokio - Async runtime
- tracing - Structured logging
Support
- Check the pg2any_lib documentation
- Open an issue for bugs or feature requests
- Review logs for detailed error information
__此文作者__:Daniel Shih(石頭)
__此文地址__: https://isdaniel.github.io/pg2any-rust-introduce/
__版權聲明__:本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 3.0 TW 許可協議。轉載請註明出處!