DeploymentModels

DeploymentModels

Part 11: Deployment Models - Zero Downtime Strategies - Complete Guide

Part 11: Deployment Models

Zero Downtime Deployment Strategies for Microservices - Complete Guide

πŸ“Œ Quick Summary (30 seconds):
Choose deployment model based on risk tolerance and downtime requirements: Rolling (simple, brief downtime), Blue-Green (zero downtime, higher cost), Canary (gradual rollout, lowest risk), Active-Active (highest availability, highest complexity). Blue-Green and Canary are most common for production microservices.

❓ The Problem: Deploying Without Downtime

Traditional Deployment Problems:
  • Taking down old version = User sees errors
  • Long maintenance windows = Business impact
  • Big bang releases = High risk of failure
  • No rollback strategy = Extended outages
  • Can't test in production = Unknown behavior
Deployment Strategies Spectrum

Low Risk / High Complexity ←—————————→ High Risk / Low Complexity

[Canary] β†’ [Blue-Green] β†’ [Rolling] β†’ [Recreate]
↓ ↓ ↓ ↓
Very Low Low Medium High Risk
High Cost Med Cost Low Cost Low Cost
Zero Downtime Zero Downtime Some Downtime Full Downtime

πŸ“Š Deployment Models Complete Comparison

Model Downtime Risk Cost Rollback Speed Complexity Best For
Recreate (Traditional) Full downtime High Low Slow (redeploy old version) Very Low Dev environments, maintenance windows
Rolling Update Some (brief per instance) Medium Low Slow (progressive rollback) Low Standard deployments, non-critical apps
Blue-Green None Low High (2x resources) Fast (switch router) Medium Critical production apps
Canary None Very Low Medium-High Fast (reduce % to 0) High Large-scale systems, user validation
A/B Testing None Low (feature-specific) Medium Fast Medium Testing features with user segments
Shadow/Replay None Lowest (no user impact) Medium N/A (traffic replay only) High Testing with real traffic without user impact
Active-Active None Low Very High (2 regions) Fast (DNS failover) High Global high-scale, multi-region

πŸ”„ 1. Recreate (Traditional)

Recreate Flow

Old Version (Running) β†’ Take Down Entirely β†’ Deploy New Version β†’ Start New Version

[===== DOWNTIME =====]

Pros: Simple, no traffic management
Cons: Full downtime, slow rollback

πŸ”„ 2. Rolling Update

Rolling Update Flow (4 instances)

Start: [V1] [V1] [V1] [V1] (All running old version)
Step 1: [V1] [V1] [V1] [V2] (Update instance 4)
Step 2: [V1] [V1] [V2] [V2] (Update instance 3)
Step 3: [V1] [V2] [V2] [V2] (Update instance 2)
Step 4: [V2] [V2] [V2] [V2] (Update instance 1)

Downtime: Each instance has brief interruption during restart
Risk: Mixed versions running simultaneously
βœ… PROS
  • No extra infrastructure needed
  • Gradual rollout
  • Works with auto-scaling
  • Low cost (no extra instances)
❌ CONS
  • Brief downtime per instance
  • Mixed versions during update
  • Slow rollback (must re-deploy)
  • Version compatibility issues

πŸ”΅ 3. Blue-Green Deployment (Most Popular)

Blue-Green Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚
β”‚ BLUE Environment (Live) GREEN Environment (New) β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ v1.0 β”‚ β”‚ v2.0 β”‚ β”‚
β”‚ β”‚ v1.0 β”‚ β”‚ v2.0 β”‚ β”‚
β”‚ β”‚ v1.0 β”‚ β”‚ v2.0 β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ Load Balancer β”‚
β”‚ β”‚ β”‚
β”‚ β–Ό β”‚
β”‚ Users β”‚
β”‚ β”‚
β”‚ Step 1: Deploy v2.0 to GREEN (while BLUE serves traffic) β”‚
β”‚ Step 2: Test GREEN environment β”‚
β”‚ Step 3: Switch Load Balancer β†’ BLUE to GREEN β”‚
β”‚ Step 4: Zero downtime! β”‚
β”‚ Step 5: Keep BLUE for rollback (10 min - 1 hour) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
# Kubernetes Blue-Green via Service selector
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    version: blue   # Currently pointing to blue (v1)
  ports:
    - port: 80

# Switch to green (v2)
kubectl patch service myapp-service -p '{"spec":{"selector":{"version":"green"}}}'

# Rollback
kubectl patch service myapp-service -p '{"spec":{"selector":{"version":"blue"}}}'
βœ… PROS
  • Zero downtime
  • Instant rollback (flip traffic back)
  • Full testing in production environment
  • No version mixing
  • Clear success/failure boundary
❌ CONS
  • 2x infrastructure cost
  • Database migration complexity
  • Longer deployment time
  • Environment drift issues

🐦 4. Canary Deployment (Lowest Risk)

Canary Release Flow

Phase 1: 5% of users β†’ New Version (Canaries)
Phase 2: Monitor metrics for 1 hour (errors, latency)
Phase 3: If healthy β†’ Increase to 20%
Phase 4: Monitor again β†’ Increase to 50%
Phase 5: Monitor β†’ Increase to 100%
Phase 6: Old version decommissioned

If errors detected at ANY phase β†’ Immediate rollback (reduce to 0%)
# Istio VirtualService for Canary Deployment
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp-canary
spec:
  hosts:
  - myapp
  http:
  - match:
    - headers:
        x-canary:
          exact: "true"   # Specific users
    route:
    - destination:
        host: myapp
        subset: v2
      weight: 100
  - route:
    - destination:
        host: myapp
        subset: v1
      weight: 95
    - destination:
        host: myapp
        subset: v2
      weight: 5      # 5% canary traffic
βœ… PROS
  • Lowest risk (small blast radius)
  • Real user validation
  • Performance testing with live traffic
  • Gradual, controlled rollout
❌ CONS
  • Complex traffic routing
  • Requires advanced infrastructure (Istio, Spinnaker)
  • Long rollout time
  • Version compatibility challenges

πŸ§ͺ 5. A/B Testing Deployment

A/B Testing Flow

User Category A (50%) β†’ Feature Flag ON β†’ New Experience
User Category B (50%) β†’ Feature Flag OFF β†’ Old Experience

Compare metrics: conversion rate, engagement, error rate
Winner: Roll out to 100%

🎭 6. Shadow Deployment (Safest Testing)

Shadow/Replay Flow

Live Traffic β†’ Splitter β†’ 95% β†’ Old Version β†’ Response to User
↓
5% β†’ New Version β†’ Response LOGGED (Not to user)

Compare responses for correctness without impacting users!
Perfect for critical systems like payments, fraud detection.

🌍 7. Active-Active (Multi-Region)

Active-Active Multi-Region Deployment

US-East Region ──┐ β”Œβ”€β”€ Europe Region
[v2.0] [v2.0] β”‚ Load Balancer β”‚ [v2.0] [v2.0]
β”œβ”€β”€β”€β”€β”€β”€β”€β”€ DNS ────────
Asia Region β”€β”€β”€β”€β”€β”˜ (Geo-routing) └── Australia Region
[v2.0] [v2.0] [v2.0] [v2.0]

Benefits: Global low latency, disaster recovery, zero downtime

🎯 Deployment Strategy Decision Matrix

If you need... Choose... Why
Simple, low-cost deployment Rolling Update No extra infrastructure, works out of box
Zero downtime for critical apps Blue-Green Instant switch, instant rollback
Lowest risk for large user base Canary Small blast radius, real user validation
Test features with specific user segments A/B Testing Compare metrics, data-driven decisions
Test without user impact Shadow/Replay No user sees new version
Global scale and disaster recovery Active-Active Multi-region, automatic failover

⚠️ The Database Problem in Blue-Green Deployment

Challenge: How to handle database schema changes with Blue-Green?
Database Migration Strategies for Blue-Green

Strategy 1: Database per Environment
Blue DB (v1 schema) ←─── Replication ───→ Green DB (v2 schema)
(Switch app, keep both DBs during transition)

Strategy 2: Backward-Compatible Changes Only
- Add columns (not remove)
- Rename with migration period
- Deprecate old fields gradually

Strategy 3: Branch by Abstraction
- Code works with both schemas
- Deploy code first, then migrate data
- Remove old code after migration

βͺ Rollback Strategies

Deployment TypeRollback MethodTime to Rollback
Rolling UpdateRedeploy previous version5-15 minutes
Blue-GreenFlip traffic back to BLUE environmentSeconds
CanaryReduce traffic to new version to 0%Seconds
Active-ActiveDNS failover to healthy regionMinutes

☸️ Kubernetes Native Deployment Strategies

Kubernetes StrategyCommandBehavior
RollingUpdate (default) kubectl set image deployment/myapp myapp=v2 Gradually replaces pods
Recreate kubectl patch deployment myapp -p '{"spec":{"strategy":{"type":"Recreate"}}}' All pods down, then new pods up
Blue-Green kubectl patch service myapp -p '{"spec":{"selector":{"version":"green"}}}' Switch service selector
Canary via Istio kubectl apply -f virtualservice-canary.yaml Weighted traffic routing

βœ… Production Deployment Checklist

Before Deployment
  • Unit tests passing
  • Integration tests passing
  • Database migrations tested
  • Rollback plan documented
  • Monitoring dashboards ready
  • Feature flags configured
During Deployment
  • Monitor error rates
  • Watch latency percentiles
  • Check resource usage
  • Verify business metrics
  • Set alert thresholds
After Deployment
  • Smoke tests on new version
  • Validate logs for errors
  • Keep old version ready (Blue-Green)
  • Document deployment
  • Notify stakeholders

πŸ’‘ Interview Power Phrases

"We use Blue-Green deployment for critical services where downtime is unacceptable β€” instant rollback with just a traffic flip."
"For large-scale systems, we use Canary deployment. We start with 5% of users, monitor for an hour, then gradually increase to 100%."
"The database is the hardest part of Blue-Green. We design backward-compatible schema changes and use database replication between environments."
"Our rollback strategy is tested before every deployment. For Blue-Green, rollback is instant; for rolling, we have automated redeployment."

πŸ“š Quick Memory Reference

πŸ”‘ Key Terms
  • Blue-Green - Two identical environments
  • Canary - Gradual % rollout
  • Rolling - One instance at a time
  • Shadow - No user impact testing
  • A/B Test - Feature-specific testing
  • Active-Active - Multi-region
🧠 Mental Models
  • Blue-Green = Two runways, switch landing at once
  • Canary = Send a few birds into coal mine first
  • Rolling = Change tires while car is moving
  • Shadow = Practice surgery on dummy first
πŸ“Œ One-Line Summary

"Blue-Green for zero downtime and instant rollback, Canary for gradual risk reduction, Rolling for simplicity and lower cost."

πŸ“š Complete Reference: All Microservices Patterns

One-Line Summaries - Quick Revision:

β€’ Sync Communication: Immediate response, tight coupling, user-facing ops
β€’ Async Communication: Event-driven, loose coupling, scalable processing
β€’ Saga Pattern: Distributed transactions via compensating actions
β€’ CQRS: Separate read/write models with intentional duplication
β€’ Circuit Breaker: Stop calling failing services, prevent cascading failures
β€’ Retry: Handle transient failures with exponential backoff
β€’ Timeout: Never wait indefinitely for any remote call
β€’ Idempotency: Same operation multiple times = same result
β€’ Rate Limiter: Control request flow, return 429 when exceeded
β€’ Cache Aside: Load on miss, most common caching pattern
β€’ Blue-Green: Two environments, instant switch, zero downtime
β€’ Canary: Gradual rollout, lowest risk
β€’ Rolling Update: One instance at a time, simple

βœ… Key Takeaways (Remember These 5 Points)

  • Blue-Green = Zero downtime + instant rollback (2x cost)
  • Canary = Lowest risk (gradual, real user validation)
  • Rolling Update = Simple, low cost (brief downtime)
  • Database is hardest part = Plan backward-compatible changes
  • Test rollback before deployment = Deployment isn't done until rollback works
🎯 Remember This:
Low Risk Tolerance β†’ Canary or Blue-Green
High Risk Tolerance β†’ Rolling or Recreate
Critical System β†’ Blue-Green + Canary + Active-Active