Skip to main content
← PROBLEMS / ENGINEERING
An end-to-end domain learning journey

Zero-Downtime Blue-Green
Database Deployments.

Running a destructive RENAME COLUMN or bare NOT NULL migration during a Green deploy breaks Blue (N-1) instantly on a shared database, destroying rollback safety.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

If latency budgets leave no room for synchronous retries, enforce 4-phase Expand-and-Contract schema evolution with dual-writes (`BEFORE` trigger or app), `COALESCE` reads, and CI SQL linting (Squawk) so N and N-1 always coexist.

TL;DR30-second simplified summary
▾

Core Mechanics: Stateless Blue and Green application fleets can be swapped in seconds behind an AWS ALB, but they almost always share a single primary PostgreSQL database (2,000 RPS).

Visual & Structural Flow: Depicts Shared-DB Schema Breakage During 60s ALB Drain (AWS ALB (60s drain) -> Blue v41 (N-1) -> Green v42 (N) -> Shared PostgreSQL 15); N and N-1 Coexistence Across 4 Migration Phases (1. EXPAND -> 2. DUAL-COMPAT -> 3. CONTRACT).

Trade-Offs & Production: Putting down-migrations in Flyway (U42__undo.sql) fails in production because rolling back a DROP loses data written by Green during canarying, and re-adding billing_zip under ACCESS EXCLUSIVE stalls 2,000 RPS. Every zero-downtime deployment pipeline with shared state relies on backward-compatible migrations: GitOps & Progressive Delivery: ArgoCD/Spinnaker Blue-Green and Canary.

Failure Modes & Application: Every zero-downtime deployment pipeline with shared state relies on backward-compatible migrations: GitOps & Progressive Delivery: ArgoCD/Spinnaker Blue-Green and Canary rollouts require N and N-1 schema compatibility during traffic shifting.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why Blue-Green apps still share one database.

Why this matters: The mechanism: understand how Why Blue-Green apps still share one database works from first principles.

Think of Why Blue-Green apps still share one database as the intuition layer for Zero-Downtime Blue-Green Database Deployments: The mechanism: understand how Why Blue-Green apps still share one database works from first principles.

Stateless Blue and Green application fleets can be swapped in seconds behind an AWS ALB, but they almost always share a single primary PostgreSQL database (2,000 RPS). Because ALB connection draining takes up to 60 seconds, Blue (v41) and Green (v42) execute reads and writes concurrently—meaning any schema change that breaks N-1 turns a routine rollback into a 100% outage.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Why destructive DDL traps both Blue and Green.

Why this matters: The problem: see what breaks without Why destructive DDL traps both Blue and Green under real concurrency or scale.

When Green (v42) ran RENAME COLUMN billing_zip TO postal_code and ADD COLUMN tax_jurisdiction NOT NULL:

  1. Rollback Broken: Flipping the ALB back to Blue (v41) caused 100% HTTP 500s (column billing_zip does not exist, 23502 not_null_violation).
  2. Table-Lock Risk: Adding NOT NULL without a default or running SET NOT NULL without NOT VALID scans 50M rows under ACCESS EXCLUSIVE.
  3. ORM SELECT * Crashes: Strict mappers fail if unexpected columns appear.
Broken Rollback vs. N/N-1 Overlap

Shared-DB Schema Breakage During 60s ALB Drain

AWS ALB (60s drain)Blue v41 (N-1)Green v42 (N)Shared PostgreSQL151. RENAME billing_zip + ADD NOTNULL2. Rollback to v41 (12% Green bug)3. INSERT/SELECT billing_zip ->500!4. Expand-Contract keeps bothvalid

Blue and Green overlap on one database during drains and rollbacks, requiring strict N/N-1 backward compatibility.

Engineering Decision Framework
Problem
How to implement Zero-Downtime Blue-Green Database Deployments without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
If latency budgets leave no room for synchronous retries, enforce 4-phase Expand-and-Contract schema evolution with dual-writes (BEFORE trigger or app), COALESCE reads, and CI SQL linting (Squawk) so N and N-1 always coexist.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Zero-Downtime Blue-Green Database Deployments experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive workarounds (and why they fail at 2,000 RPS).

Why this matters: The problem: see what breaks without Naive workarounds under real concurrency or scale.

Think of Naive workarounds (and why they fail at 2,000 RPS) as the intuition layer for Zero-Downtime Blue-Green Database Deployments: The problem: see what breaks without Naive workarounds under real concurrency or scale.

Putting down-migrations in Flyway (U42__undo.sql) fails in production because rolling back a DROP loses data written by Green during canarying, and re-adding billing_zip under ACCESS EXCLUSIVE stalls 2,000 RPS. Similarly, cloning a separate Green database introduces async replication lag and split-brain payment writes.

Component Topology

Naive workarounds (and why they fail at 2,000 RPS)

Ingress / LBingressK8s ControlcomputeWorker NodestorageApp Podscontrolactive trafficreconcileprobe / SIGTERM
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: 4-Phase Expand-and-Contract + CI Squawk Gate.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Decouple schema changes from code cutovers across four safe phases:

  1. Expand: Add nullable postal_code and tax_jurisdiction (< 5 ms metadata lock in PG 15) plus a lightweight BEFORE INSERT OR UPDATE sync trigger (< 0.2 ms overhead).
  2. Migrate / Dual-Read: Backfill historical rows in small 5,000-row batches; Green reads COALESCE(postal_code, billing_zip) and writes both.
  3. Verify & Cutover: Blue (v41) and Green (v42) are both 100% compatible, allowing < 30 s ALB rollback anytime.
  4. Contract (7+ days later): Only after pg_stat_statements confirms 0 queries touch billing_zip, add CHECK (tax_jurisdiction IS NOT NULL) NOT VALID -> VALIDATE CONSTRAINT (non-blocking) and drop billing_zip.
  • CI Gate: Block DROP, RENAME, and bare NOT NULL in PRs with Squawk.
Expand-and-Contract Lifecycle

N and N-1 Coexistence Across 4 Migration Phases

Batch backfill 5k rows<30s ALB rollback anytime7d zero refs in…1. EXPANDNullable cols + sync tri2. DUAL-COMPATBlue v41 & Green v42 bot3. CONTRACTVALIDATE + drop after 7d

Blue and Green overlap safely in Phase 2 until 7 days of zero legacy-column reads authorize Phase 3 contraction.

-- Phase 1 (Expand): < 5ms metadata lock, 100% Blue v41 compatible
ALTER TABLE invoices ADD COLUMN postal_code VARCHAR(20);
ALTER TABLE invoices ADD COLUMN tax_jurisdiction VARCHAR(16);

CREATE OR REPLACE FUNCTION sync_invoice_zip() RETURNS trigger AS $$
BEGIN
  IF NEW.postal_code IS NULL THEN NEW.postal_code := NEW.billing_zip; END IF;
  IF NEW.billing_zip IS NULL THEN NEW.billing_zip := NEW.postal_code; END IF;
  RETURN NEW;
END; $$ LANGUAGE plpgsql;

CREATE TRIGGER trg_sync_invoice_zip
  BEFORE INSERT OR UPDATE ON invoices
  FOR EACH ROW EXECUTE FUNCTION sync_invoice_zip();
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Expand-and-Contract is used in production.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Every zero-downtime deployment pipeline with shared state relies on backward-compatible migrations:

  • GitOps & Progressive Delivery: ArgoCD/Spinnaker Blue-Green and Canary rollouts require N and N-1 schema compatibility during traffic shifting.
  • High-Volume OLTP Migrations: Tools like gh-ost, pt-online-schema-change, and PostgreSQL NOT VALID constraints avoid table locks on multi-terabyte tables.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

In PostgreSQL 15, how should you enforce `NOT NULL` on a 50M-row table during the Contract phase without blocking 2,000 RPS writes?

Recommended Learning Flow