compensate property

Future<void> Function(T) compensate
final

Compensates (rolls back) a successful execution.

This function is called if:

  1. This step's execute completed successfully (returned a value T)
  2. A later step failed
  3. 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 T value 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;