Part 09 · Distributed Systems

Saga, compensation và durable state machines

Saga là chuỗi local transactions; compensation là business action có thể fail, không phải rollback xuyên service.

State machine

Persist workflow ID, current state, attempts, deadlines và transition history. Command/event idempotent; compare-and-set/version ngăn concurrent transition. Terminal states và operator recovery phải rõ.

Choreography

Services react events, coupling trực tiếp thấp nhưng flow lớn khó nhìn, cycle/implicit dependency và debugging khó. Phù hợp flow nhỏ, domain events tự nhiên và ownership rõ.

Orchestration

Coordinator gửi commands và ghi state, làm flow/timeout/recovery rõ nhưng thêm component cùng central workflow logic. Coordinator không được bypass service data ownership.

Compensation

Refund, release inventory, cancel shipment có semantics riêng, có thể irreversible hoặc fail. Compensation cũng cần retry/idempotency, deadline và manual intervention. Đôi khi forward recovery tốt hơn undo.

Saga Pattern · Saga Pattern Reference