Devops

Production Deployment Patterns

📋 At a Glance

AspectDetails
Difficulty🟠 Advanced
PrerequisitesPart 15 (Dependencies), Part 17 (CI/CD)
PatternsRolling, Blue-Green, Canary
Time Investment26 minutes read + 90 minutes practice
PayoffZero-downtime deployments, confident rollbacks

🎯 What You'll Learn

After this article, you'll be able to:

  1. Implement rolling updates with zero downtime
  2. Execute blue-green deployments for instant rollbacks
  3. Run canary deployments to test with real traffic
  4. Handle rollbacks quickly and safely
  5. Manage database migrations during deployments

🔥 Production Story: The Friday Afternoon Deployment

The Setup: 4:30 PM Friday. "Quick hotfix, just push it." No testing, direct docker compose up -d.
What Happened:
BASH(5 lines)
Code
Loading syntax highlighter...
The Damage:
  • 30 seconds of complete outage
  • 1,000+ failed requests
  • Angry customers over the weekend
The Root Cause: No deployment strategy. docker compose up -d stops old container before new one is ready.
The Fix:
YAML(8 lines)
Code
Loading syntax highlighter...
Result: New deployments start the new container, wait for it to be healthy, then stop the old one. Zero downtime.

🧠 Mental Model: Deployment Strategies

┌─────────────────────────────────────────────────────────────────┐
│                    DEPLOYMENT STRATEGIES                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ROLLING UPDATE (Default, safest)                              │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  v1 ████████████████████                                │   │
│   │  v2     ████████████████████                            │   │
│   │         ↑ start new    ↑ stop old                       │   │
│   │     start-first: new healthy before old stops           │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   BLUE-GREEN (Instant switch)                                   │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Blue (v1)  ████████████████                            │   │
│   │  Green (v2)     ████████████████████████████            │   │
│   │                 ↑ switch LB                             │   │
│   │     Both running, traffic switches instantly            │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   CANARY (Gradual rollout)                                      │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  v1  ████████████████ (90%)                             │   │
│   │  v2  ██ (10%)         ████ (25%)    ████████████ (100%) │   │
│   │      ↑ test with few   ↑ increase   ↑ full rollout      │   │
│   │     Gradually shift traffic if metrics OK               │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   RECREATE (Downtime, simple)                                   │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  v1  ████████████████                                   │   │
│   │                      (downtime)                         │   │
│   │  v2                      ████████████████████           │   │
│   │      ↑ stop all          ↑ start all                    │   │
│   │     Simple but has downtime                             │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

🔬 Deep Dive

1. Rolling Updates with Docker Compose

Basic Rolling Update:
YAML(20 lines)
Code
Loading syntax highlighter...
Deployment Script:
BASH(35 lines)
Code
Loading syntax highlighter...
Manual Rolling Update (without Swarm):
BASH(26 lines)
Code
Loading syntax highlighter...

2. Blue-Green Deployment

Architecture:
                    ┌──────────────┐
                    │   Nginx/LB   │
                    └──────┬───────┘
                           │
              ┌────────────┴────────────┐
              │                         │
        ┌─────┴─────┐             ┌─────┴─────┐
        │   Blue    │             │   Green   │
        │  (live)   │             │  (new)    │
        │  port 80  │             │ port 8080 │
        └───────────┘             └───────────┘
Compose Configuration:
YAML(38 lines)
Code
Loading syntax highlighter...
Nginx Configuration:
NGINX(20 lines)
Code
Loading syntax highlighter...
Blue-Green Deployment Script:
BASH(54 lines)
Code
Loading syntax highlighter...
Instant Rollback:
BASH(19 lines)
Code
Loading syntax highlighter...

3. Canary Deployment

Nginx with Weighted Traffic:
NGINX(15 lines)
Code
Loading syntax highlighter...
Compose for Canary:
YAML(31 lines)
Code
Loading syntax highlighter...
Canary Deployment Script:
BASH(36 lines)
Code
Loading syntax highlighter...
Promote or Rollback:
BASH(29 lines)
Code
Loading syntax highlighter...

4. Handling Database Migrations

Migration Strategies:
┌────────────────────────────────────────────────────────────┐
│                 MIGRATION PATTERNS                         │
├────────────────────────────────────────────────────────────┤
│                                                            │
│  1. BACKWARDS COMPATIBLE (preferred)                       │
│     ┌─────────────────────────────────────────────────┐    │
│     │  v1: Uses column 'name'                         │    │
│     │  Migration: Add column 'full_name', copy data   │    │
│     │  v2: Uses 'full_name', ignores 'name'           │    │
│     │  Later: Drop 'name' column                      │    │
│     └─────────────────────────────────────────────────┘    │
│                                                            │
│  2. EXPAND-CONTRACT                                        │
│     ┌─────────────────────────────────────────────────┐    │
│     │  Expand: Add new column/table                   │    │
│     │  Deploy: v2 writes to both old and new          │    │
│     │  Migrate: Backfill old data                     │    │
│     │  Contract: v3 only uses new, drop old           │    │
│     └─────────────────────────────────────────────────┘    │
│                                                            │
│  3. FEATURE FLAGS                                          │
│     ┌─────────────────────────────────────────────────┐    │
│     │  Deploy code with flag off                      │    │
│     │  Run migration                                  │    │
│     │  Enable flag for small % of users               │    │
│     │  Roll out to 100%                               │    │
│     └─────────────────────────────────────────────────┘    │
│                                                            │
└────────────────────────────────────────────────────────────┘
Migration Service in Compose:
YAML(18 lines)
Code
Loading syntax highlighter...
Deployment with Migration:
BASH(21 lines)
Code
Loading syntax highlighter...
Reversible Migration Example:
SQL(8 lines)
Code
Loading syntax highlighter...

5. Health Checks and Readiness

Liveness vs Readiness:
YAML(8 lines)
Code
Loading syntax highlighter...
Application Health Endpoints:
JAVASCRIPT(38 lines)
Code
Loading syntax highlighter...
Load Balancer Health Check:
NGINX(14 lines)
Code
Loading syntax highlighter...

6. Zero-Downtime Deployment Checklist

YAML(40 lines)
Code
Loading syntax highlighter...
Deployment Checklist Script:
BASH(67 lines)
Code
Loading syntax highlighter...

7. Rollback Strategies

Quick Rollback Script:
BASH(22 lines)
Code
Loading syntax highlighter...
Automated Rollback on Failure:
BASH(34 lines)
Code
Loading syntax highlighter...

⚠️ Common Mistakes

Mistake 1: Stop-First Update Order

YAML(9 lines)
Code
Loading syntax highlighter...

Mistake 2: No Health Check in Deployment

YAML(13 lines)
Code
Loading syntax highlighter...

Mistake 3: Breaking Database Changes

SQL(11 lines)
Code
Loading syntax highlighter...

Mistake 4: No Rollback Plan

BASH(7 lines)
Code
Loading syntax highlighter...

Mistake 5: All-at-Once Updates

YAML(10 lines)
Code
Loading syntax highlighter...

🐛 Debug This

Deployment completes but application is down:

BASH(8 lines)
Code
Loading syntax highlighter...

All containers show as "Started" but the service is unreachable. What's happening?

Click to reveal analysis
Investigation:
BASH(12 lines)
Code
Loading syntax highlighter...
Root Cause: Database isn't ready, but depends_on wasn't using service_healthy condition.
Fix:
YAML(12 lines)
Code
Loading syntax highlighter...
Additional Fix: Add retry logic in application for resilience:
JAVASCRIPT(12 lines)
Code
Loading syntax highlighter...

💻 Exercises

Exercise 1: Rolling Update

Implement a rolling update that:

  • Updates 1 container at a time
  • Waits for health check before continuing
  • Rolls back on failure

Exercise 2: Blue-Green Deployment

Set up blue-green with:

  • Two parallel environments
  • Nginx switching between them
  • Instant rollback capability

Exercise 3: Canary Deployment

Implement canary that:

  • Sends 10% of traffic to new version
  • Gradually increases to 100%
  • Can rollback instantly

Exercise 4: Database Migration

Handle a schema change that:

  • Renames a column
  • Works during rolling update
  • Allows rollback

Exercise 5: Complete Pipeline

Create an end-to-end deployment:

  • Build and test in CI
  • Deploy to staging automatically
  • Deploy to production with approval
  • Automatic rollback on failure

🎤 Interview Questions

Q1: Explain the difference between rolling update, blue-green, and canary deployments.

Answer:
StrategyTraffic PatternRollback SpeedResource Cost
RollingGradual, container by containerSlow (redeploy)Low
Blue-GreenInstant switchInstantHigh (2x)
CanaryGradual % increaseFastMedium
Rolling Update:
  • Updates containers one at a time
  • Always some serving traffic
  • Rollback requires redeployment
  • Best for: Standard deployments
Blue-Green:
  • Two complete environments
  • Traffic switches instantly
  • Instant rollback (switch back)
  • Best for: Critical systems, instant rollback needs
Canary:
  • New version gets small traffic %
  • Monitor, then increase
  • Quick rollback (send 0% to canary)
  • Best for: Testing in production, risky changes

Q2: How do you handle database migrations in a zero-downtime deployment?

Answer: Use backwards-compatible migrations with expand-contract pattern:
Phase 1 - Expand (add new, keep old):
SQL(6 lines)
Code
Loading syntax highlighter...
Phase 2 - Deploy (use both):
JAVASCRIPT(3 lines)
Code
Loading syntax highlighter...
Phase 3 - Contract (remove old):
SQL(2 lines)
Code
Loading syntax highlighter...
Key principles:
  1. Never rename columns directly
  2. Never drop columns until all code is updated
  3. Always be able to roll back

Q3: What's the order: start-first configuration and why is it important?

Answer: order: start-first means Docker starts the new container BEFORE stopping the old one.
Without start-first (stop-first):
1. Stop old container
2. (Service unavailable!)
3. Start new container
4. Wait for health check
5. Service available again
With start-first:
1. Start new container
2. Wait for health check
3. (Old still serving traffic)
4. Stop old container
5. Zero downtime!
YAML(4 lines)
Code
Loading syntax highlighter...
Requirement: Need enough resources to run both old and new simultaneously. For a 3-replica service, briefly need capacity for 4.

Q4: How do you implement automatic rollback on deployment failure?

Answer: Combine health checks with failure actions:
YAML(15 lines)
Code
Loading syntax highlighter...
For Compose (non-Swarm):
BASH(28 lines)
Code
Loading syntax highlighter...

Q5: How do you handle a scenario where you need to deploy a breaking change?

Answer: Use feature flags and gradual rollout:
Step 1: Deploy code with feature flag OFF
JAVASCRIPT(5 lines)
Code
Loading syntax highlighter...
Step 2: Run database migrations
SQL(2 lines)
Code
Loading syntax highlighter...
Step 3: Enable feature for internal users
JAVASCRIPT
Code
Loading syntax highlighter...
Step 4: Gradual rollout
JAVASCRIPT(5 lines)
Code
Loading syntax highlighter...
Step 5: Remove old code and flag (later release)
JAVASCRIPT(2 lines)
Code
Loading syntax highlighter...

📝 Summary & Key Takeaways

Deployment Strategies

StrategyUse When
RollingStandard deployments, most cases
Blue-GreenNeed instant rollback
CanaryTesting risky changes

Zero-Downtime Requirements

  1. order: start-first in deploy config
  2. Health checks with start_period
  3. Graceful shutdown handling
  4. Backwards-compatible migrations

Rollback Essentials

  • Always save previous version
  • Keep old images available
  • Automate rollback on failure
  • Test rollback procedure

📋 Quick Reference

YAML(15 lines)
Code
Loading syntax highlighter...
BASH(3 lines)
Code
Loading syntax highlighter...

📅 Review Schedule

  • Day 1: Implement rolling update with health checks
  • Day 3: Set up blue-green deployment
  • Day 7: Practice database migrations
  • Day 14: Implement canary with monitoring
  • Day 30: Full deployment pipeline with rollback

📚 Series Navigation