Compensation design
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write compensations that are safe and meaningful.
Planned topics
- A compensation is a new business action, not a database rollback (a refund, not a deleted payment).
- It must be idempotent: it may run twice.
- It must tolerate the forward step never having happened (or still being in flight): releasing stock that was never reserved is a no-op.
- Record intent first (a reservation id) so the compensation knows what to undo.
- Late arrivals: a "reserve" message that arrives after "release" must not re-reserve.
Draft outline details
Demonstration
@Transactional
void release(UUID orderId) {
reservations.findByOrder(orderId).ifPresentOrElse(
r -> { if (r.isActive()) { r.cancel(); stock.add(r.sku(), r.qty()); } },
() -> reservations.recordCancelled(orderId)); // blocks a late reserve
}Check your understanding
What should release do if nothing was reserved?
Show answer
Nothing harmful; record the cancellation so a late reserve is rejected.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.