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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
