EngineeringSolution ArchitectAzure Architecture

Session 04 - Multi-Region & Global Scale

Tổng quan

Multi-region architecture đảm bảo high availability, low latency cho users toàn cầu, và disaster recovery. Chi phí cao hơn đáng kể - chỉ áp dụng khi business requirements thực sự cần.

Khi nào cần Multi-Region?

Yếu tốSingle Region đủCần Multi-Region
SLA yêu cầu99.9% (8.76h downtime/năm)99.99% (52 phút/năm)
Users1 khu vực địa lýNhiều continents
ComplianceKhông yêu cầu data residency đa vùngGDPR, data sovereignty
RTO> 1 giờ chấp nhận được< 15 phút
RPO> 1 giờ chấp nhận được< 5 phút

Azure Traffic Manager vs Front Door

Tiêu chíTraffic ManagerAzure Front Door
LayerDNS-based (L4)HTTP reverse proxy (L7)
Routing methodsPriority, Weighted, Performance, GeographicLatency, Priority, Weighted, Session affinity
SSL offloadingKhôngCó
WAFKhôngCó (tích hợp)
CachingKhôngCó (CDN built-in)
Health probesTCP/HTTPHTTP/HTTPS
Failover speedDNS TTL (30-300s)Near-instant (< 30s)
GiáRẻ ($0.54/million queries)Đắt hơn (per request + data transfer)
Phù hợpNon-HTTP (TCP), simple failoverWeb apps, APIs, cần WAF + CDN

Khuyến nghị: Dùng Front Door cho web/API workloads. Dùng Traffic Manager cho non-HTTP protocols hoặc khi cần tiết kiệm chi phí.

Multi-Region Architecture

Deployment Stamps Pattern

Mỗi "stamp" là một bản sao hoàn chỉnh của toàn bộ stack, deploy độc lập ở mỗi region.

Đặc điểm Deployment Stamps:

  • Mỗi stamp self-contained - có đủ compute, data, cache
  • Scale bằng cách thêm stamps mới (scale unit)
  • Isolate failure - 1 stamp fail không ảnh hưởng stamps khác
  • Dễ rollout - canary deploy theo từng stamp
bash
# Deploy stamp mới bằng Bicep
az deployment sub create \
  --location southeastasia \
  --template-file main.bicep \
  --parameters \
    stampName=sea-01 \
    region=southeastasia \
    environment=production

az deployment sub create \
  --location eastasia \
  --template-file main.bicep \
  --parameters \
    stampName=ea-01 \
    region=eastasia \
    environment=production

Cosmos DB Multi-Region Writes

bash
# Tạo Cosmos DB với multi-region writes
az cosmosdb create \
  --resource-group rg-global \
  --name cosmos-myapp-global \
  --locations regionName=southeastasia failoverPriority=0 isZoneRedundant=true \
  --locations regionName=eastasia failoverPriority=1 isZoneRedundant=true \
  --enable-multiple-write-locations true \
  --default-consistency-level Session

Consistency Levels - Chọn đúng

LevelLatencyConsistencyUse Case
StrongCao nhất100% consistentFinancial transactions
Bounded StalenessCaoConsistent within boundInventory counts
SessionTrung bìnhConsistent per sessionUser profiles (phổ biến nhất)
Consistent PrefixThấpOrdered, có thể staleActivity feeds
EventualThấp nhấtCó thể out-of-orderAnalytics, logs
csharp
// Cấu hình Cosmos DB client với preferred regions
builder.Services.AddSingleton(sp =>
{
    var client = new CosmosClient(connectionString, new CosmosClientOptions
    {
        ApplicationRegion = Regions.SoutheastAsia,
        // Hoặc set preferred regions list
        ApplicationPreferredRegions = new List<string>
        {
            Regions.SoutheastAsia,
            Regions.EastAsia
        },
        ConsistencyLevel = ConsistencyLevel.Session
    });
    return client;
});

Azure SQL Geo-Replication

bash
# Tạo failover group
az sql failover-group create \
  --resource-group rg-global \
  --server sql-myapp-sea \
  --name fog-myapp \
  --partner-server sql-myapp-ea \
  --partner-resource-group rg-global \
  --failover-policy Automatic \
  --grace-period 1

# Connection string dùng failover group endpoint
# Read-write: fog-myapp.database.windows.net
# Read-only:  fog-myapp.secondary.database.windows.net
csharp
// Connection string sử dụng failover group
// Tự động redirect khi failover xảy ra
{
    "ConnectionStrings": {
        "Default": "Server=tcp:fog-myapp.database.windows.net,1433;Database=MyAppDb;Authentication=Active Directory Managed Identity;",
        "ReadOnly": "Server=tcp:fog-myapp.secondary.database.windows.net,1433;Database=MyAppDb;Authentication=Active Directory Managed Identity;ApplicationIntent=ReadOnly;"
    }
}

Data Replication Strategies

StrategyRPOComplexityCostServices
Active-Active~0Rất cao (conflict resolution)$$$$Cosmos DB multi-write
Active-Passive (hot standby)< 5 phútTrung bình$$$SQL Failover Group
Active-Passive (cold standby)< 1 giờThấp$$Geo-restore from backup
Backup & RestoreTùy backup scheduleThấp nhất$Periodic backup

CDN Integration

bash
# Tạo Front Door profile với CDN
az afd profile create \
  --resource-group rg-global \
  --profile-name afd-myapp \
  --sku Premium_AzureFrontDoor

# Thêm endpoint
az afd endpoint create \
  --resource-group rg-global \
  --profile-name afd-myapp \
  --endpoint-name myapp-global \
  --enabled-state Enabled

# Thêm origin group
az afd origin-group create \
  --resource-group rg-global \
  --profile-name afd-myapp \
  --origin-group-name og-api \
  --probe-request-type GET \
  --probe-protocol Https \
  --probe-path /healthz \
  --probe-interval-in-seconds 10 \
  --sample-size 4 \
  --successful-samples-required 3

# Thêm origins (mỗi region)
az afd origin create \
  --resource-group rg-global \
  --profile-name afd-myapp \
  --origin-group-name og-api \
  --origin-name origin-sea \
  --host-name app-myapp-sea.azurewebsites.net \
  --origin-host-header app-myapp-sea.azurewebsites.net \
  --priority 1 --weight 1000 \
  --enabled-state Enabled

az afd origin create \
  --resource-group rg-global \
  --profile-name afd-myapp \
  --origin-group-name og-api \
  --origin-name origin-ea \
  --host-name app-myapp-ea.azurewebsites.net \
  --origin-host-header app-myapp-ea.azurewebsites.net \
  --priority 2 --weight 1000 \
  --enabled-state Enabled

Failover Testing - Playbook

Quy trình test failover định kỳ:

  1. Chuẩn bị - thông báo team, chọn maintenance window
  2. Baseline - ghi lại current metrics (latency, error rate, RPS)
  3. Trigger failover
    bash
    # Force failover SQL
    az sql failover-group set-primary \
      --resource-group rg-global \
      --server sql-myapp-ea \
      --name fog-myapp --allow-data-loss
    
    # Disable primary origin trên Front Door
    az afd origin update \
      --resource-group rg-global \
      --profile-name afd-myapp \
      --origin-group-name og-api \
      --origin-name origin-sea \
      --enabled-state Disabled
    
  4. Observe - monitor dashboard, kiểm tra error rate, latency tại secondary
  5. Validate - chạy smoke tests against secondary endpoint
  6. Failback - restore primary, verify data consistency
  7. Document - ghi RTO/RPO thực tế, lessons learned

Trade-off Analysis

Quyết địnhOption AOption B
Active-Active vs Active-PassiveNear-zero RPO, 2x cost, conflict handlingSimpler, cheaper, RPO > 0
Front Door vs Traffic ManagerL7 features, WAF, CDN, đắt hơnDNS-based, rẻ, chậm failover
Cosmos DB vs SQL geo-replicationMulti-write, eventual consistency tradeoffStrong consistency, single-write
2 regions vs 3 regionsĐủ cho hầu hết casesUltra HA, 3x cost

Nguyên tắc: Bắt đầu với Active-Passive 2 regions. Chỉ upgrade lên Active-Active khi RPO = 0 là hard requirement từ business.