API / Microservices Design Patterns Interview Questions
What is the Saga rollback / compensating transaction pattern?
In a Saga (Q10), when a step fails, previously completed steps cannot be undone with a database ROLLBACK because each step has already committed its local transaction and those locks are released. Instead, the Saga executes compensating transactions — purpose-built operations that reverse the business effect of each completed step in reverse order.
A compensating transaction is a semantic undo, not a technical rollback. The key distinction:
- A technical rollback is performed by the database engine before a transaction commits — it undoes uncommitted SQL statements.
- A compensating transaction is a new, forward-moving operation that creates the business-level opposite of an already-committed action.
Example compensations:
- Forward step: reserve 5 units of stock → Compensation: release 5 units of stock reservation
- Forward step: charge customer 9.99 → Compensation: refund customer 9.99
- Forward step: create order in PENDING status → Compensation: update order status to CANCELLED
Important edge cases:
- Pivotal transactions — not all Saga steps can be compensated. A step that sends a physical shipment or charges a non-refundable fee is called a pivot transaction; if it succeeds, the Saga must complete rather than roll back.
- Retriable transactions — some steps after the pivot are guaranteed to succeed eventually (e.g., updating an order status). These steps are retried until success rather than being compensated.
- Idempotency — compensating transactions may be retried if the Saga coordination infrastructure fails, so each compensation must be idempotent.
More Related questions...