Skip to content

High Availability + Disaster Recovery

Mở đầu

System down 1 phút = mất vài chục triệu. HA (High Availability) = system tiếp tục serve khi gặp hardware fail, software bug, network issue. DR (Disaster Recovery) = recover khi disaster lớn.

Bạn sẽ học:

  • Availability metric: "mấy 9" + downtime
  • Failover: active-standby, active-active, multi-region
  • DR strategy: RPO + RTO + backup
  • Fault detection: heartbeat, probe, circuit breaker
  • Chaos engineering: chủ động inject fault
ChươngNội dung
1Availability metric
2Failover architecture
3DR design
4Fault detection + recovery
5Chaos engineering

1. Availability metric: "mấy 9" nghĩa gì?

Availability = Uptime / Total time × 100%

Vd 1 tháng (30 ngày = 43200 phút) down 43 phút → (43200-43)/43200 ≈ 99.9%. Mỗi 9 thêm = downtime allowed giảm 1 order, complexity + cost tăng exponential.

Tier%Down/thángDown/nămUse
2 nines99%7.3h3.65 ngàyInternal tool
3 nines99.9%43 phút8.76hBusiness thường
4 nines99.99%4.3 phút52.6 phútE-commerce, SaaS
5 nines99.999%26 giây5.26 phútBanking, payment
可用性等级计算器
点击查看不同"几个 9"对应的停机时间
2 个 9
99%
3 个 9
99.9%
4 个 9
99.99%
5 个 9
99.999%
3 个 9(99.9%)
每年停机
8.76 小时
每月停机
43.8 分钟
每周停机
10.1 分钟
典型场景:普通 Web 应用、企业系统

SLA là gì?

SLA (Service Level Agreement) = cam kết formal vendor ↔ customer. Vd AWS S3 commit 99.99%, không đạt = refund tỷ lệ. SLA không chỉ tech metric, mà commercial contract — vi phạm = đền tiền.

Khoảng cách 3 nines → 4 nines

3 nines (99.9%) = 43 phút/tháng — 1 lần deploy lỗi rollback = hết. 4 nines (99.99%) = 4 phút/tháng — yêu cầu auto failover, rolling deploy, health check đầy đủ.


2. Failover architecture

Failover = core mechanism HA: main node fail → auto switch sang standby.

Active-Standby

Main xử mọi request, standby sync data nhưng không xử. Main fail → standby take over.

Normal:
  Client → Main (xử request)
           Standby (sync data, wait)

Failover:
  Client → Standby (new main)
           Original main (fault, repair)

Vấn đề key: Split Brain — network partition, 2 node đều nghĩ kia chết, cùng serve → data inconsistent. Solution: Quorum — ít nhất 3 node vote.

Multi-AZ

Deploy ở nhiều DC cùng region. DC đơn down không ảnh hưởng. AZ cloud có dedicated link low-latency (<2ms).

Multi-Region Active-Active

Deploy full replica ở thành phố/nước khác, mỗi site xử request độc lập. HA cao nhất, cũng complex nhất — challenge: cross-region data sync latency + consistency.

故障转移策略对比
点击查看不同高可用架构的工作方式
主备模式
主主模式
多可用区
异地多活
主备模式
一个主节点处理所有请求,备节点待命。主节点故障时,备节点接管。
主节点
处理请求
备节点
待命同步
优点
架构简单,易于理解
数据一致性好保证
缺点
备节点资源浪费
切换有短暂中断(秒级)
ArchitectureTierCostComplexityUse
Single99%~99.9%ThấpThấpDev test, internal
Active-Standby99.9%~99.99%TrungTrungMid-size business
Multi-AZ99.99%CaoCaoE-commerce, SaaS
Multi-Region A-A99.999%Cực caoCực caoBanking, big internet

3. DR design: RPO + RTO

MetricFullNghĩaVd
RPORecovery Point ObjectiveCho phép mất bao nhiêu dataRPO=0 → không mất data nào
RTORecovery Time ObjectiveCho phép down bao lâuRTO=5min → recover trong 5 phút

Backup strategy + RPO

Cách backupRPOCostNote
Full backup daily24hThấpMax mất 1 ngày
Realtime incrementalVài phútTrungbinlog/WAL sync
Sync replication0CaoWrite phải đợi replica confirm

Không mọi data cần RPO=0

Avatar mất user upload lại được (RPO=24h OK), nhưng payment record không được mất (RPO=0). Backup strategy theo business value, không one-size-fits-all.


4. Fault detection + recovery

4.1 Detection

MechanismNguyên lýSpeedUse
HeartbeatSend heartbeat định kỳ, timeout = faultGiâyNode alive
Health checkHTTP/TCP probeGiâyLoad balancer backend
Business probeMô phỏng real requestGiây-phútEnd-to-end availability

Heartbeat: node A định kỳ (5s) send "tôi sống" lên monitor. N lần (3) không thấy = fault. Tham số key: interval + threshold.

3 cấp health check:

  • Liveness: process đang chạy? Không → restart
  • Readiness: service nhận request? Không → bỏ khỏi LB
  • Startup: service start xong? Chưa → đợi, không misjudge

4.2 Auto recovery

MechanismDescriptionTool
Auto restartProcess crash → restartsystemd, PM2, K8s
Auto scalingLoad tăng → scale upK8s HPA, Cloud Auto Scaling
Circuit breakerDownstream fail → fail fastHystrix, Sentinel, Resilience4j
Rate limitRequest quá capacity → rejectNginx limit_req, gateway

Circuit Breaker pattern:

Cảm hứng từ cầu chì điện — overload tự ngắt, bảo vệ mạch. Microservice, downstream fail → breaker "open", request fail-fast thay vì đợi timeout.

3 trạng thái Circuit Breaker:

  Closed (normal) ──→ Fail rate > threshold ──→ Open (broken)
       ↑                                            │
       │                                      Cooldown
       │                                            ↓
       └── Probe success ←── Half-Open (test)
  • Closed: forward request bình thường, count fail rate
  • Open: mọi request fail-fast, không call downstream
  • Half-Open: cooldown xong, cho probe ít. OK → close; fail → open

Fallback = strategy đi cùng circuit breaker: trigger breaker không trả error, return "fallback" result. Vd recommend service fail → return hot product list; avatar load fail → default avatar.


5. Chaos engineering: chủ động tìm bug

Core: thay vì đợi fault, chủ động tạo fault, verify resilience trong controlled env.

ToolByCapability
Chaos MonkeyNetflixRandom kill instance prod
Chaos MeshPingCAPFault injection K8s
LitmusCNCFCloud-native chaos framework
ChaosBladeAlibabaMulti-scenario fault injection

5 step

  1. Define steady state: metric system bình thường (vd P99 < 200ms)
  2. Hypothesis: nếu 1 node die, system phải recover trong 30s
  3. Inject fault: kiểm soát range (test trước, prod sau)
  4. Observe: system recover như expected? Cascade fail?
  5. Fix weakness: phát hiện → improve architecture + process

Tổng kết

HA không phải feature, mà capability architecture. Phải đảm bảo ở mọi mắt xích design → dev → deploy → ops.

  1. Mấy 9: mỗi 9 thêm = downtime giảm 1 order, cost + complexity exponential
  2. Failover: từ A-S đến multi-region A-A, theo business need
  3. RPO + RTO: theo data value design backup
  4. Automation: detect, restart, circuit breaker = HA infrastructure
  5. Chaos: chủ động tạo fault, verify resilience

2026 cho VN dev

  • VN context:
    • Banking: target 5 nines (Vietcombank, VPBank)
    • E-commerce VN (Shopee, Tiki): 4 nines, multi-AZ AWS/GCP
    • Startup: 3 nines đủ ban đầu
  • Modern HA tools 2026:
    • AWS Multi-Region: Route 53 + RDS read replica cross-region
    • Cloudflare: anycast network → automatic HA
    • Backup: AWS Backup, GCP Backup, Velero (K8s)
  • VN regulation: Banking Decree 13 yêu cầu DR site, RTO/RPO khắt
  • AI scenario: LLM API có circuit breaker với fallback model (vd GPT-4 fail → fallback Claude/Gemini), critical cho production AI app

Tài liệu