Bài toán: microservices không chia sẻ DB → không dùng distributed transaction (2PC quá phức tạp) → làm sao đảm bảo consistency khi 1 operation update nhiều service?
Saga: chuỗi local transaction, mỗi bước publish event → service kế tiếp xử lý. Bước N fail → chạy compensating transaction ngược từ N-1.
1. Order: CREATE (PENDING) → OrderCreated
2. Inventory: RESERVE items → ItemsReserved
3. Payment: CHARGE → PaymentCompleted
4. Order: CONFIRMED
// Bước 3 fail → Inventory RELEASE (compensate) → Order CANCEL2 kiểu:
- Choreography — mỗi service nghe event và phản ứng (loose coupling, nhưng khó debug, cyclic dependency ngầm).
- Orchestration — 1 orchestrator điều phối (Axon, Temporal, AWS Step Functions) — dễ debug, flow rõ, nhưng thêm component.
Idempotency bắt buộc: mỗi step phải idempotent vì message có thể deliver nhiều lần (Kafka at-least-once).