We are going to create a playbook for blue/green database migrations with shadow reads.
The idea is to have two database environments (blue and green) and migrate data from blue to green.
During the migration, we will also perform "shadow reads" where we read from both databases to compare results and ensure correctness.
Steps:
- Setup: Have two database environments (blue and green). The green is the target.
- Initial Sync: Copy the data from blue to green (full dump and restore, or use change data capture).
- Shadow Reads: Start by reading from both databases for a subset of queries and compare results.
- Cutover: Once confident, switch the application to use the green database.
However, note that in a typical blue/green deployment, we have two production environments. Here we are focusing on the database migration.
We assume:
- The application is already configured to read from the blue database (current production).
- We have a separate green database that is a copy of blue at some point and we want to migrate to it.
We'll break down the steps in a playbook format.
Important: We must consider ongoing changes during the migration. We need to keep the green database in sync until cutover.
Let's outline the steps:
Phase 1: Preparation
a. Ensure green database is set up and has the same schema as blue.
b. Take a snapshot of the blue database and restore it to green (or use a tool for continuous replication).
Phase 2: Ongoing Sync
a. We need to capture changes (writes) on blue and apply them to green. This can be done via:
- Logical replication (e.g., PostgreSQL logical decoding, MySQL binlog)
- Change Data Capture (CDC) tools (Debezium, etc.)
b. We must also consider that during the migration, the application is still writing to blue.
Phase 3: Shadow Reads
a. We route a small percentage of read queries to green (shadow) and compare the results with blue.
b. The comparison can be done by the application (if it's designed to do so) or by a separate tool.
c. We gradually increase the shadow read traffic until we are confident.
Phase 4: Cutover
a. Stop the sync from blue to green (stop applying changes).
b. Switch the application's write operations to green (and reads too, if not already).
c. Verify that the application is working correctly with green.
d. If something goes wrong, switch back to blue (rollback).
Phase 5: Cleanup
a. Once stable, we can decommission the blue database or keep it as a backup for a while.
However, note: In a blue/green deployment, we usually have two environments and we switch traffic from blue to green.
But in this case, we are only migrating the database. The application might be deployed separately.
Let's write the playbook in a structured way.
We assume we have the following:
- Blue database: current production
- Green database: target
We'll describe the steps in a way that can be automated or done manually.
Important: We must have a way to compare the two databases for consistency during shadow reads.
Let's create a sample playbook:
Blue/Green Database Migration Playbook with Shadow Reads
Phase 1: Preparation
1.1. Setup Green Database
- Ensure the green database is running and accessible.
- Apply the same schema as the blue database. If there are schema changes, they must be applied to both.
- Take a initial snapshot of the blue database and restore it to green. This can be a logical dump or a physical backup.
1.2. Establish Replication
- Set up a replication mechanism from blue to green to capture ongoing changes.
- For PostgreSQL: Use logical replication with a publication on blue and a subscription on green.
- For MySQL: Use binlog replication or a CDC tool.
- Verify that the replication is working and that green is catching up.
Phase 2: Shadow Reads
2.1. Configure Application for Shadow Reads
- Modify the application configuration to allow reading from two databases (blue and green) for a subset of queries.
- Implement a mechanism to compare the results of shadow reads (from green) with the primary reads (from blue).
- This could be done by having the application read from both and compare, or by a separate service that does the comparison.
- Start with a small percentage of read queries (e.g., 1%) being shadowed to green.
2.2. Monitor and Analyze
- Monitor the replication lag (green should be as close as possible to blue).
- Check for any discrepancies in the shadow reads. If discrepancies are found, investigate and fix.
- Gradually increase the percentage of shadow reads (e.g., 5%, 25%, 50%, 100%) as confidence grows.
2.3. Duration of Shadow Reads
- Run shadow reads for a sufficient period (e.g., a few days) to cover different traffic patterns and ensure consistency.
Phase 3: Cutover
3.1. Plan the Cutover
- Choose a low-traffic period for the cutover.
- Notify stakeholders.
3.2. Stop Replication
- Stop the replication from blue to green. We want to stop the flow of new changes to green so that we can make green the primary.
3.3. Switch Application to Green
- Update the application configuration to use green as the primary database for both reads and writes.
- Deploy the new configuration (may require an application restart or a config reload).
3.4. Validate
- Check the application logs and monitor for errors.
- Verify that writes are being made to green and reads are consistent.
- If possible, run a set of read/write tests.
3.5. Rollback Plan
- If any critical issues are detected, switch the application back to blue immediately.
- The rollback should be as simple as changing the database connection string back to blue.
Phase 4: Post-Cutover
4.1. Monitor
- Closely monitor the green database for performance and correctness.
4.2. Cleanup
- After a period of stability (e.g., a week), we can decommission the blue database or keep it as a backup for a longer period.
4.3. Remove Shadow Read Configuration
- Remove the code/configuration for shadow reads from the application.
Additional Considerations
- Schema Changes: During the migration, if there are schema changes, they must be applied to both blue and green. We need a plan for that.
- Data Consistency: The replication must be consistent. We must handle any conflicts or errors during replication.
- Backup: Always have backups of both databases before starting.
This playbook assumes a relatively simple setup. In reality, there might be multiple databases, sharding, etc.
Let me know if you need more details on any specific step.
Here’s a comprehensive Blue/Green Database Migration Playbook with Shadow Reads – a risk-averse strategy to migrate production databases with zero downtime, validation, and rollback capability.
📘 Blue/Green Database Migration Playbook with Shadow Reads
🎯 Goal
Migrate production data from Blue (current) to Green (target) database with zero downtime, continuous validation via shadow reads, and immediate rollback capability.
📋 Pre-Migration Checklist
| Task | Owner | Status |
|---|
| ✅ Green DB provisioned (same version, config) | Infra | |
| ✅ Schema sync validated | DBA | |
| ✅ Network/connectivity tested (app → Green) | Network | |
| ✅ Backup of Blue DB taken (restorable) | DBA | |
| ✅ Monitoring/alerting on Green DB enabled | SRE | |
| ✅ Application config supports DB switching | Dev | |
| ✅ CDC/Replication tool installed & tested (Debezium, AWS DMS, etc.) | Data Eng | |
| ✅ Shadow read logic implemented in app | Dev | |
| ✅ Rollback playbook reviewed & approved | All | |
🔄 Migration Phases
Phase 1: Baseline Sync
Objective: Get Green to initial consistent state with Blue.
-
Full Dump & Restore
- Take consistent snapshot of Blue (pg_dump, mysqldump, etc.).
- Restore to Green during low-traffic window.
- Validation: Row counts, checksums on sample tables.
-
Enable CDC/Replication
- Start change data capture from Blue → Green.
- Ensure Green catches up (monitor replication lag = 0).
Phase 2: Shadow Read Validation
Objective: Compare read results between Blue/Green without affecting users.
Implementation Options:
- In-app shadowing: Route 1–5% of read queries to Green (non-critical endpoints). Compare responses with Blue. Log mismatches.
- Sidecar comparator: Deploy a shadow service that replays production queries to both DBs and diffs results.
- Tools:
pg_comparator (PostgreSQL), pt-table-checksum (MySQL), custom script.
Key Metrics:
- Replication lag (Green vs Blue) must be < 1s.
- Query mismatch rate must be 0% (or within acceptable business tolerance, e.g.,
NULL vs empty string).
- Performance: P99 latency Green ≤ Blue + 10%.
Ramp-up Schedule:
Day 1: 1% shadow reads (non-critical paths)
Day 2: 5% (add dashboard queries)
Day 3: 25% (add API read paths)
Day 4: 100% shadow reads (read-only mode simulation)
→ If any mismatch detected, pause & investigate before proceeding.
Phase 3: Write Dry-Run (Optional but Recommended)
Objective: Validate write path without committing.
- Shadow writes: Duplicate writes to Green (but discard/rollback immediately).
- Compare: Row counts, index bloat, transaction performance.
- Rollback: Ensure Green can handle write load without affecting Blue.
Phase 4: Cutover (Blue → Green)
Timing: Low-traffic window (e.g., weekend).
- Stop replication (pause CDC temporarily to avoid split-brain).
- Switch app writes to Green:
- Update connection strings / secrets manager.
- Deploy new config (rolling restart, zero-downtime).
- Immediate validation:
- Smoke tests (write → read from Green).
- Check application logs for DB errors.
- Verify replication lag = 0 (if resuming CDC for backup).
- Keep Blue read-only for 24–48h as hot standby.
- If issues: Rollback to Blue immediately (revert config, resume app → Blue).
Phase 5: Post-Cutover & Cleanup
- Monitor Green intensely for 72h (performance, errors, storage growth).
- Decommission Blue only after:
- 7 days of stable operation.
- Final backup archived.
- All dependant services confirmed on Green.
- Remove shadow read logic from app.
🚨 Rollback Triggers & Procedures
| Trigger | Action | Max MTTR |
|---|
| Replication lag > 5s | Pause migration, investigate | 5 min |
| Shadow read mismatch > 0% | Halt increase, debug | 15 min |
| App errors > baseline 2x | Switch reads/writes back to Blue | 2 min |
| Green performance degradation > 20% | Immediate failover to Blue | 1 min |
Rollback Steps:
- Update app config to point to Blue.
- Ensure Blue is fully caught up (resume replication if paused).
- Verify traffic on Blue (metrics, logs).
- Document root cause before retrying.
📊 Monitoring & Alerts
| Metric | Threshold | Alert |
|---|
| Replication lag | > 1s | Critical |
| Shadow read mismatch count | > 0 | Critical |
| Green DB CPU/Memory | > 80% | Warning |
| App error rate (Green) | > 2x baseline | Critical |
| Connection pool saturation | > 90% | Warning |
🔧 Tool Recommendations
| Task | Tools |
|---|
| CDC/Replication | Debezium, AWS DMS, pglogical, MySQL Replication |
| Shadow Reads | Custom middleware, Datadog Synthetic Queries, psycopg2/mysql-connector scripts |
| Data Comparison | pg_comparator, pt-table-checksum, databend-diff |
| Monitoring | Prometheus + Grafana, CloudWatch, Datadog |
📝 Notes & Assumptions
- Both DBs are same major version (e.g., PostgreSQL 14 → 14).
- Schema changes must be applied simultaneously to Blue/Green before shadow reads.
- Connection pooling (PgBouncer, HikariCP) must support dynamic host updates.
- Feature flags recommended for gradual read/write switching.
- Test in staging with production-like data volume first.
✅ Sign-off Section
| Role | Name | Approval | Date |
|---|
| DBA | | | |
| SRE | | | |
| Dev Lead | | | |
| Product Owner | | | |
Final Tip:
Start with non-critical read-only services (e.g., analytics, reporting) for shadow reads before touching core transactional paths. Never proceed to cutover until 24h of zero mismatches in shadow reads.
Let me know if you need scripts for data comparison or CDC setup! 🚀