How a single Docker container can solve your multi-database synchronization challenges
The Modern Database Replication Challenge
In today’s distributed application landscape, keeping multiple databases in sync is no longer a luxury — it’s a necessity. Whether you’re building microservices, implementing read replicas for performance, creating analytics pipelines, or ensuring disaster recovery, database replication has become a critical component of modern infrastructure.
However, setting up PostgreSQL logical replication has traditionally been a complex, error-prone process that requires deep database administration knowledge. From configuring WAL levels to managing publications and subscriptions, the learning curve is steep and the potential for mistakes is high.
What if there was a better way?
Introducing postgres-replica: Replication Made Simple
I’m excited to introduce postgres-replica, a Docker-based service that transforms PostgreSQL logical replication from a complex database administration task into a simple configuration management problem.
# That's it. Really.
docker run --rm \
-v "./replication-config.yml:/config/replication-config.yml:ro" \
-p 3001:3000 \
darkmatter08/postgres-replica:latestWhy Logical Replication Matters
Before diving into the solution, let’s understand why logical replication is so powerful:
1. Selective Data Synchronization
Unlike physical replication that copies everything, logical replication lets you choose exactly which tables to replicate. Need only user data in your analytics database? No problem.
2. Cross-Version Compatibility
Logical replication works across different PostgreSQL versions, making it perfect for gradual migrations and heterogeneous environments.
3. Minimal Impact
The source database continues operating normally while changes are streamed to targets in real-time.
4. Multiple Targets
One source database can replicate to multiple targets simultaneously, each with different table selections.
The Traditional Way vs. The postgres-replica Way
Traditional Setup (The Hard Way)
Setting up logical replication traditionally involves:
Source Database Configuration:
# postgresql.conf
wal_level = logical
max_replication_slots = 10
max_wal_senders = 10Creating Publications:
CREATE PUBLICATION my_pub FOR TABLE users, posts, comments;Target Database Setup:
CREATE SUBSCRIPTION my_sub
CONNECTION 'host=source-db port=5432 user=repl_user dbname=source_db'
PUBLICATION my_pub;Managing Schema Synchronization: You must manually ensure target databases have matching schemas.
Monitoring and Error Handling: Custom scripts to monitor replication lag and handle failures.
Adding New Tables: Manually alter publications and refresh subscriptions.
The postgres-replica Way (The Easy Way)
With postgres-replica, the same setup becomes:
Create a simple YAML config:
replication:
publication_name: "my_publication"
source:
host: "source-db"
port: 5432
user: "postgres"
password: "secret"
database: "main_db"
targets:
- name: "analytics_replica"
subscription_name: "analytics_sub"
host: "analytics-db"
port: 5432
user: "postgres"
password: "secret"
database: "analytics_db"
tables:
- "users"
- "posts"
- "comments"Run the container:
docker run --rm \
-v "./replication-config.yml:/config/replication-config.yml:ro" \
darkmatter08/postgres-replica:latestThat’s it. The service handles everything else automatically.
Real-World Use Cases
1. Microservices Data Synchronization
Imagine you have an e-commerce platform with separate services for users, orders, and inventory. Each service has its own database, but the recommendation service needs user and order data.
# Sync user and order data to recommendation service
replication:
publication_name: "recommendation_sync"
source:
host: "main-db"
database: "ecommerce"
targets:
- name: "recommendation_db"
host: "recommendation-db"
database: "recommendations"
tables:
- "users"
- "orders"
- "order_items"2. Analytics Pipeline
Your production database serves your application, but you need the same data in your analytics warehouse without impacting performance.
# Real-time analytics replication
replication:
publication_name: "analytics_feed"
source:
host: "prod-db"
targets:
- name: "data_warehouse"
host: "analytics-db"
tables: ["users", "events", "transactions"]
- name: "ml_pipeline"
host: "ml-db"
tables: ["users", "user_behaviors"] # Different table selection3. Multi-Region Deployment
Deploying across multiple regions while keeping data synchronized:
# Multi-region sync
replication:
publication_name: "global_sync"
source:
host: "us-east-db"
targets:
- name: "eu_west_replica"
host: "eu-west-db"
- name: "asia_pacific_replica"
host: "ap-southeast-db"Key Features That Set It Apart
1. Dynamic Table Management
Adding a new table to replication? Just update your config:
tables:
- "users"
- "posts"
- "comments"
- "new_table" # Add this lineRestart the service, and it automatically:
Adds the table to the publication
Refreshes all subscriptions
Validates the table exists in targets
2. Built-in Health Monitoring
Get real-time status of your replication:
curl http://localhost:3001/health{
"status": "healthy",
"details": {
"source": { "status": "healthy" },
"targets": [
{ "name": "analytics_replica", "status": "healthy" },
{ "name": "ml_pipeline", "status": "healthy" }
]
}
}3. Production-Ready Error Handling
The service includes comprehensive error handling:
Automatic connection retry logic
Graceful handling of temporary network issues
Detailed logging for troubleshooting
Validation that prevents common misconfigurations
4. Multi-Platform Docker Images
Built for both AMD64 and ARM64 architectures, the service runs everywhere:
Local development on Apple Silicon Macs
Production on AWS, GCP, or Azure
Edge deployment on ARM servers
Getting Started in Minutes
Want to try it yourself? Here’s a complete working example:
Clone the example repository:
git clone https://github.com/dark-matter08/db-sync-test
cd db-sync-testStart the complete setup:
docker-compose up -dThis starts:
Source PostgreSQL database with sample data
Two target PostgreSQL databases
The replication service connecting them all
Test the replication:
# Add data to source
docker exec -it source-db psql -U postgres -d main_db -c \
"INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');"# Verify it appears in targets
docker exec -it target-db-1 psql -U postgres -d replica_db -c \
"SELECT * FROM users WHERE name = 'John Doe';"Advanced Configuration Options
Per-Target Table Selection
Different targets can replicate different table subsets:
replication:
publication_name: "selective_sync"
source:
host: "main-db"
targets:
- name: "full_replica"
tables: ["users", "posts", "comments", "analytics_events"]
- name: "public_api_cache"
tables: ["users", "posts"] # Only public data
- name: "analytics_only"
tables: ["analytics_events"] # Only analytics
tables: ["users", "posts", "comments", "analytics_events"] # Global defaultCustom Replication Settings
Fine-tune replication behavior per target:
targets:
- name: "high_performance_replica"
settings:
enable_initial_sync: true
disable_triggers_during_sync: true
max_wait_attempts: 50
wait_interval_seconds: 2Monitoring and Observability
Health Check Integration
Integrate with your monitoring stack:
# Kubernetes liveness probe
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10Comprehensive Logging
The service provides detailed, emoji-enriched logging:
🚀 Starting PostgreSQL Replication Service
📋 Loading configuration from: /config/replication-config.yml
✅ Configuration loaded successfully
📊 Publication: my_publication
📍 Source: main-db:5432/source_db
🎯 Targets: 2
1. analytics_replica (analytics-db:5432/analytics_db)
2. ml_pipeline (ml-db:5432/ml_db)
📋 Tables: users, posts, comments🔄 Setting up publication on source database...
✅ Publication "my_publication" created for tables: users, posts, comments
📋 Setting up subscriptions on target databases...
✅ analytics_replica: Subscription setup completed successfully
✅ ml_pipeline: Subscription setup completed successfully📊 Current publication status:
📋 Publication: "my_publication"
📊 Published tables (3): comments, posts, usersProduction Deployment Considerations
1. Database Preparation
Ensure your PostgreSQL instances are properly configured:
# postgresql.conf for all databases
wal_level = logical
max_replication_slots = 10
max_wal_senders = 10 # Source only
max_logical_replication_workers = 10 # Targets only
max_worker_processes = 162. Network Security
Configure pg_hba.conf for secure replication:
# Allow replication connections
host replication username replica_subnet/24 md53. Schema Management
Target databases must have matching table structures. The service validates this but doesn’t create tables automatically — this is by design to prevent accidental schema conflicts.
4. Monitoring Integration
Set up alerts based on the health endpoint:
# Prometheus monitoring
- alert: ReplicationUnhealthy
expr: postgres_replica_health_status != 1
for: 5m
annotations:
summary: "PostgreSQL replication is unhealthy"Performance and Scalability
Minimal Overhead
Logical replication has minimal impact on source database performance:
Changes are streamed asynchronously
No locks on source tables during replication
Configurable batch sizes for large data sets
Horizontal Scaling
Scale by running multiple replication services:
Different services for different table groups
Geographic distribution of replication services
Load balancing across multiple source databases
Common Pitfalls and How postgres-replica Helps
1. Schema Drift
Problem: Target schemas get out of sync with source.
Solution: Built-in validation ensures tables exist and match before starting replication.
2. Connection Management
Problem: Replication connections drop and need manual intervention. Solution: Automatic connection retry with exponential backoff.
3. Monitoring Blind Spots
Problem: Replication fails silently.
Solution: Built-in health monitoring and detailed status reporting.
4. Configuration Complexity
Problem: Managing publications and subscriptions across multiple databases.
Solution: Single YAML file manages entire replication topology.
The Future of Database Replication
postgres-replica represents a shift toward treating database replication as infrastructure-as-code. By containerizing the replication logic and making it configuration-driven, we can:
Version control replication configurations
Test replication setups in CI/CD pipelines
Deploy replication using GitOps workflows
Scale replication using Kubernetes operators
Community and Ecosystem
The project is open source and actively maintained:
GitHub: postgres-replica
Docker Hub: darkmatter08/postgres-replica
Example Repository: db-sync-test
Contributions are welcome! Whether it’s bug reports, feature requests, or pull requests, the community drives the project forward.
Conclusion
Database replication doesn’t have to be complex. With postgres-replica, what once required deep PostgreSQL expertise and custom scripting becomes a simple configuration file and a Docker command.
Whether you’re building microservices, implementing analytics pipelines, or ensuring high availability, postgres-replica provides a production-ready, monitoring-enabled solution that scales with your needs.
The future of infrastructure is declarative, containerized, and simple. postgres-replica brings that future to PostgreSQL logical replication today.
Ready to simplify your database replication?
🚀 Get started: docker pull darkmatter08/postgres-replica:latest
📖 Documentation: GitHub Repository
🧪 Try the example: db-sync-test
💬 Questions? Open an issue on GitHub
Have you struggled with database replication in your projects? What solutions have worked for you? Share your experiences in the comments below.
Tags: #PostgreSQL #Docker #DatabaseReplication #DevOps #Microservices #DataEngineering #OpenSource
Comments
No comments yet.