Blue/Green Database Migration Playbook with Shadow Reads
This playbook outlines a zero-downtime database migration strategy using blue/green deployment with shadow reads. It minimizes risk by validating the new (green) database in production traffic before cutover.
Assumptions:
- Production database is "Blue" (current live DB).
- New database is "Green" (target DB with schema changes, new version, etc.).
- Application supports configurable read/write endpoints (e.g., via feature flags).
- Databases support replication (e.g., PostgreSQL logical replication, MySQL binlog, or cloud services like AWS RDS Aurora).
- Monitoring tools (e.g., Datadog, Prometheus) and logging (e.g., structured logs to ELK) are in place.
- Rollback plan is always available.
Key Concepts:
- Blue/Green: Duplicate environments; switch atomically.
- Shadow Reads: Duplicate read queries to green DB alongside blue; compare results for validation without affecting users.
- Dual Writes (optional): Mirror writes to both DBs during transition.
Risks Mitigated: Data loss, schema incompatibilities, performance regressions.
Estimated Duration: 1-4 weeks (prep + shadow + cutover).
Phases
Phase 0: Preparation (1-2 days)
-
Inventory & Planning
- Document schema diffs, data volume (~TB?), query patterns (top 10% of queries).
- Define success criteria: <0.1% shadow read discrepancies, <5% latency increase.
- Set up alerting: Shadow read failures >1/min, replication lag >5s.
-
Infrastructure Setup
| Component | Blue | Green | Notes |
|---|
| DB Instance | Production DB | New instance (same size +20% buffer) | Use cloud autoscaling if possible. |
| Replication | N/A | Logical replication from Blue | Enable CDC (Change Data Capture). |
| Network | VPC peering | Same VPC | Low-latency access. |
| Backups | Enabled | Enable + test restore | Point-in-time recovery (PITR). |
-
Application Updates
- Deploy app version supporting:
- Configurable read DB (blue/green).
- Shadow reads: Fork reads to green, compare results (hash/JSON diff).
- Feature flag:
enable_shadow_reads, enable_green_writes.
- Example pseudocode:
def read_query(sql, params):
blue_result = execute(blue_conn, sql, params)
if shadow_enabled:
green_result = execute(green_conn, sql, params)
compare_and_log(blue_result, green_result)
return blue_result
-
Initial Data Sync
- Full dump/load from blue to green (e.g.,
pg_dump or DMS).
- Start replication; wait for lag <1s.
-
Test in Staging
- Mirror prod traffic to staging blue/green.
- Run shadow reads for 24h; fix discrepancies.
Gate: All tests pass, replication healthy.
Phase 1: Shadow Reads (3-7 days)
-
Enable Shadow Reads
- Flip feature flag in production.
- Writes: 100% to blue.
- Reads: 100% to blue + shadow to green.
-
Monitoring Dashboard
| Metric | Target | Alert Threshold |
|---|
| Shadow Discrepancies | 0% | >0.1% |
| Green Latency P95 | < Blue P95 +10% | >+20% |
| Replication Lag | <5s | >30s |
| Error Rate (Green) | <0.01% | >0.1% |
-
Tuning & Fixes
- Query logs: Index missing on green? Fix schema.
- Discrepancies: Data drift? Adjust replication filters.
- Gradually ramp shadow traffic (10% → 100%) if app supports sampling.
Gate: 72h stable shadow reads, zero critical issues.
Phase 2: Dual Writes (Optional, 1-2 days)
- If schema changes prevent read-only shadow:
- Enable writes to green (app forks writes).
- Monitor write discrepancies/latency.
- Resolve conflicts (e.g., last-write-wins).
Gate: Writes synced, lag <1s.
Phase 3: Cutover (30-60 minutes)
-
Pre-Cutover Checklist
- Quiesce long-running jobs.
- Replication lag <1s.
- App config:
primary_db = green.
-
Switch Reads
- Atomic flag flip: Reads → 100% green.
- Monitor 15min; rollback if errors >1%.
-
Switch Writes
- Pause blue writes (app → green only).
- Final sync: Catch up replication.
- Promote green to primary (failover if managed DB).
- DNS/update connection strings: Blue → Green.
-
Verify
- Smoke tests: Key queries, writes.
- Traffic mirror back to blue (canary reads) for 1h.
Rollback: Revert flags to blue; resume replication.
Phase 4: Post-Cutover (1-3 days)
-
Monitoring
- Full prod traffic on green.
- Decommission blue: After 7 days, snapshot → delete.
-
Cleanup
- Disable shadow/dual writes.
- Update docs: Green is now blue.
Rollback Plan
| Scenario | Actions | Time |
|---|
| Shadow Failures | Disable flag; revert app deploy | <5min |
| Cutover Issues | Revert read/write flags; resume blue replication | <15min |
| Data Loss | PITR from blue backup | <1h |
Tools & Best Practices
- DB-Specific: PostgreSQL (logical rep), MySQL (GTID), Mongo (Oplog).
- Cloud: AWS DMS/Snapshot Restore, GCP Database Migration, Azure Database Migration Service.
- App Frameworks: Dual DB support in Rails (e.g.,
shadow_db), Django routers.
- Automation: Terraform for infra, ArgoCD/Spinnaker for app deploys.
- Lessons Learned: Log everything; start with non-critical traffic.
This playbook ensures safe migration. Customize for your stack. Questions? Let's deduce the details! 🕵️