Devops
Production Deployment Patterns
📋 At a Glance
| Aspect | Details |
|---|---|
| Difficulty | 🟠 Advanced |
| Prerequisites | Part 15 (Dependencies), Part 17 (CI/CD) |
| Patterns | Rolling, Blue-Green, Canary |
| Time Investment | 26 minutes read + 90 minutes practice |
| Payoff | Zero-downtime deployments, confident rollbacks |
🎯 What You'll Learn
After this article, you'll be able to:
- Implement rolling updates with zero downtime
- Execute blue-green deployments for instant rollbacks
- Run canary deployments to test with real traffic
- Handle rollbacks quickly and safely
- 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)CodeLoading 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)CodeLoading 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)CodeLoading syntax highlighter...
Deployment Script:
BASH(35 lines)CodeLoading syntax highlighter...
Manual Rolling Update (without Swarm):
BASH(26 lines)CodeLoading syntax highlighter...
2. Blue-Green Deployment
Architecture:
┌──────────────┐ │ Nginx/LB │ └──────┬───────┘ │ ┌────────────┴────────────┐ │ │ ┌─────┴─────┐ ┌─────┴─────┐ │ Blue │ │ Green │ │ (live) │ │ (new) │ │ port 80 │ │ port 8080 │ └───────────┘ └───────────┘
Compose Configuration:
YAML(38 lines)CodeLoading syntax highlighter...
Nginx Configuration:
NGINX(20 lines)CodeLoading syntax highlighter...
Blue-Green Deployment Script:
BASH(54 lines)CodeLoading syntax highlighter...
Instant Rollback:
BASH(19 lines)CodeLoading syntax highlighter...
3. Canary Deployment
Nginx with Weighted Traffic:
NGINX(15 lines)CodeLoading syntax highlighter...
Compose for Canary:
YAML(31 lines)CodeLoading syntax highlighter...
Canary Deployment Script:
BASH(36 lines)CodeLoading syntax highlighter...
Promote or Rollback:
BASH(29 lines)CodeLoading 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)CodeLoading syntax highlighter...
Deployment with Migration:
BASH(21 lines)CodeLoading syntax highlighter...
Reversible Migration Example:
SQL(8 lines)CodeLoading syntax highlighter...
5. Health Checks and Readiness
Liveness vs Readiness:
YAML(8 lines)CodeLoading syntax highlighter...
Application Health Endpoints:
JAVASCRIPT(38 lines)CodeLoading syntax highlighter...
Load Balancer Health Check:
NGINX(14 lines)CodeLoading syntax highlighter...
6. Zero-Downtime Deployment Checklist
YAML(40 lines)CodeLoading syntax highlighter...
Deployment Checklist Script:
BASH(67 lines)CodeLoading syntax highlighter...
7. Rollback Strategies
Quick Rollback Script:
BASH(22 lines)CodeLoading syntax highlighter...
Automated Rollback on Failure:
BASH(34 lines)CodeLoading syntax highlighter...
⚠️ Common Mistakes
Mistake 1: Stop-First Update Order
YAML(9 lines)CodeLoading syntax highlighter...
Mistake 2: No Health Check in Deployment
YAML(13 lines)CodeLoading syntax highlighter...
Mistake 3: Breaking Database Changes
SQL(11 lines)CodeLoading syntax highlighter...
Mistake 4: No Rollback Plan
BASH(7 lines)CodeLoading syntax highlighter...
Mistake 5: All-at-Once Updates
YAML(10 lines)CodeLoading syntax highlighter...
🐛 Debug This
Deployment completes but application is down:
BASH(8 lines)CodeLoading syntax highlighter...
All containers show as "Started" but the service is unreachable. What's happening?
Click to reveal analysis
Investigation:
BASH(12 lines)CodeLoading syntax highlighter...
Root Cause: Database isn't ready, but
depends_on wasn't using service_healthy condition.Fix:
YAML(12 lines)CodeLoading syntax highlighter...
Additional Fix: Add retry logic in application for resilience:
JAVASCRIPT(12 lines)CodeLoading 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:
| Strategy | Traffic Pattern | Rollback Speed | Resource Cost |
|---|---|---|---|
| Rolling | Gradual, container by container | Slow (redeploy) | Low |
| Blue-Green | Instant switch | Instant | High (2x) |
| Canary | Gradual % increase | Fast | Medium |
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)CodeLoading syntax highlighter...
Phase 2 - Deploy (use both):
JAVASCRIPT(3 lines)CodeLoading syntax highlighter...
Phase 3 - Contract (remove old):
SQL(2 lines)CodeLoading syntax highlighter...
Key principles:
- Never rename columns directly
- Never drop columns until all code is updated
- 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)CodeLoading 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)CodeLoading syntax highlighter...
For Compose (non-Swarm):
BASH(28 lines)CodeLoading 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)CodeLoading syntax highlighter...
Step 2: Run database migrations
SQL(2 lines)CodeLoading syntax highlighter...
Step 3: Enable feature for internal users
JAVASCRIPTCodeLoading syntax highlighter...
Step 4: Gradual rollout
JAVASCRIPT(5 lines)CodeLoading syntax highlighter...
Step 5: Remove old code and flag (later release)
JAVASCRIPT(2 lines)CodeLoading syntax highlighter...
📝 Summary & Key Takeaways
Deployment Strategies
| Strategy | Use When |
|---|---|
| Rolling | Standard deployments, most cases |
| Blue-Green | Need instant rollback |
| Canary | Testing risky changes |
Zero-Downtime Requirements
order: start-firstin deploy config- Health checks with
start_period - Graceful shutdown handling
- 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)CodeLoading syntax highlighter...
BASH(3 lines)CodeLoading 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