Trung BìnhSystem Design iconSystem Design

Saga Pattern giải quyết vấn đề gì trong microservices? Choreography vs Orchestration? (What problem does the Saga Pattern solve in microservices? Choreography vs Orchestration?)

Trong microservices, mỗi service có database riêng, nên không thể dùng distributed transactions (2PC) – quá phức tạp và tạo tight coupling. Saga Pattern giải quyết bằng cách chia distributed transaction thành chuỗi local transactions, mỗi bước publish event; nếu một bước fail, thực hiện compensating transactions để rollback các bước trước. Ví dụ Order Saga: CreateOrder → ReserveInventory → ProcessPayment → ShipOrder; nếu ProcessPayment fail → UnreserveInventory → CancelOrder.

  • Choreography: mỗi service lắng nghe events và tự quyết định hành động tiếp theo – không có coordinator trung tâm, loosely coupled hơn nhưng khó theo dõi flow tổng thể, dễ tạo circular dependencies.
  • Orchestration: có một Saga Orchestrator trung tâm điều phối các bước, gửi commands đến từng service – dễ hiểu và debug flow, nhưng orchestrator có thể trở thành bottleneck và chứa nhiều business logic.

Temporal.io, AWS Step Functions là công cụ hỗ trợ orchestration saga.

Xem toàn bộ System Design cùng filter theo level & chủ đề con.

Mở danh sách System Design