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.
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
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
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
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) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β 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%)
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%
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.
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
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
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 Type | Rollback Method | Time to Rollback |
|---|---|---|
| Rolling Update | Redeploy previous version | 5-15 minutes |
| Blue-Green | Flip traffic back to BLUE environment | Seconds |
| Canary | Reduce traffic to new version to 0% | Seconds |
| Active-Active | DNS failover to healthy region | Minutes |
βΈοΈ Kubernetes Native Deployment Strategies
| Kubernetes Strategy | Command | Behavior |
|---|---|---|
| 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."
"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
β’ 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
Low Risk Tolerance β Canary or Blue-Green
High Risk Tolerance β Rolling or Recreate
Critical System β Blue-Green + Canary + Active-Active