Bỏ qua đến nội dung
DevOps Lab

Sync vs Async API Interactive

So sánh trực quan giữa Sync API (blocking) và Async API (non-blocking) qua mô phỏng.

So sánh Sync API và Async API trong DevOps và Network Automation

Sơ đồ tổng quan Sync vs Async API Hai luồng: Sync API (blocking, nhận kết quả ngay) và Async API (non-blocking, nhận task ID rồi poll) Sync API blocking · immediate response Client Server 1. Request ⏳ Blocking waiting... ⚙ Processing fast task 2. Response + data ✓ Done. Data in hand. Async API non-blocking · task tracking Client Server 1. Submit job 2. 202 + task_id ✓ Free to do other work ⚙ Running long task... 3. Poll status RUNNING RUNNING 4. COMPLETED + result ✓ Done. Result retrieved. 3 failure domains trong Async: Queue · Worker · Status store vs 1 failure domain trong Sync (chỉ HTTP connection)
Client
idle
Server
idle
Đơn giản, dễ debug
Không cần tracking
Code 3 dòng là xong
Blocking thread
Timeout nếu task chậm
HTTP session bị giữ
Client
idle
Queue
idle
Worker
idle
State machine đầy đủ — phải xử lý hết:
SCHEDULED RUNNING COMPLETED | FAILED | ABORTED | PARTIAL_SUCCESS | EXPIRED

Với mỗi trạng thái terminal không phải COMPLETED: quyết định retry, alert, hay compensate?

So sánh Sync và Async API theo nhiều tiêu chí Bảng so sánh trực quan giữa Sync API và Async API theo các tiêu chí: model, use case, complexity, scalability, failure domains Tiêu chí Sync API Async API Execution model Blocking Non-blocking Response Immediate data 202 + task_id Use case Query, health check Provision, deploy, CI/CD Code complexity Low High (polling/webhook) Failure domains 1 (HTTP connection) 3 (Queue, Worker, Store) Scalability Limited High Idempotency risk Low High — cần idempotency key AWS example describe-instances create-stack (waiter) GCP example compute.instances.list operations.wait() (LRO)
# Sync — đơn giản, dùng cho query/read import requests response = requests.get( "https://api/dna/intent/api/v1/network-device", headers=headers ) data = response.json() # data ngay lập tức print(data["response"])
Failure modes của Async API Sơ đồ các failure scenarios: timeout không biết server có nhận không, duplicate từ retry, eventual consistency khi poll 3 failure modes nguy hiểm nhất của Async 1. Two Generals Problem Client gửi POST → Network timeout → Server nhận hay chưa? Không biết! → Nếu retry không có idempotency key → Duplicate deployment / VM / workflow Fix: idempotency key 2. Thundering Herd 100 jobs complete cùng lúc → 100 clients poll cùng lúc → Rate limit: 429 Too Many Requests → Cascade failure Fix: backoff + jitter 3. Eventual Consistency Task thực sự đã COMPLETED → Status store chưa propagate → Poll trả về RUNNING → Automation nghĩ task đang chạy Fix: confirm N lần liên tiếp Timeline so sánh: task 2 phút Sync ❌ Blocking entire thread (120s) Async submit ✓ Free to do other work poll 0s 30s 60s 90s 120s Checklist khi gặp bất kỳ API nào: Sync hay Async? Có task_id không? Poll interval bao nhiêu? Retry strategy? Failure state nào? Idempotent không?