compensate property
Compensates (rolls back) a successful execution.
This function is called if:
- This step's execute completed successfully (returned a value
T) - A later step failed
- The saga is in compensation mode
The parameter is the exact value returned by execute, ensuring type safety. This function should undo whatever execute did, leaving the system in a consistent state.
Best practices for compensation:
- Make it idempotent: Safe to call multiple times without side effects
- Make it non-throwing or log errors gracefully: Other compensations shouldn't be blocked by failures
- Use the result: The
Tvalue contains necessary info to undo the step (e.g., ID of created resource) - Be thorough: Clean up all resources created by execute
Implementation
final Future<void> Function(T) compensate;