# Why do we add compensation action before action we want to compensate?

**URL:** <https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226>\
**Category:** Training\
**Tags:** saga\
**Created:** [January 29, 2026, 6:12pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226 "2026-01-29T18:12:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaykemelov](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaykemelov](https://community.temporal.io/u/shaykemelov)\
**Post date:** [January 29, 2026, 6:12pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/1 "2026-01-29T18:12:08Z")

</div>

```auto
try {
  saga.addCompensation(activities::revertInventory, order);
  String inventoryResult = activities.updateInventory(order);
  logger.info(inventoryResult);

  saga.addCompensation(activities::refundCustomer, creditCardInfo);
  creditCardConfirmation = activities.processCreditCard(creditCardInfo, bill);
} catch (ActivityFailure e) {
  saga.compensate();
  logger.error(e.getMessage());
  throw e;
}

```

If `activities.processCreditCard(creditCardInfo, bill);` fails it means that nothing was processed, so do we really need to compensate? It looks like a customer will receive an extra refund

I think we need to add compensation after successful action isn’t it?

```auto
creditCardConfirmation = activities.processCreditCard(creditCardInfo, bill);
saga.addCompensation(activities::refundCustomer, creditCardInfo);

```

and if something after failed or cancellation requested only then we need to compensate

It’s strange to compensate a failed action

#edu-errstrat-java

---

<div class="post-metadata">

**Author:** ![mmegger](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mmegger/32/3897_2.png) [@mmegger](https://community.temporal.io/u/mmegger)\
**Post date:** [January 29, 2026, 7:39pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/2 "2026-01-29T19:39:45Z")

</div>

Hello,

So at this point in the code we are just registering the compensating action with the Saga object, not actually performing the compensation. This saga object will be used in the event a failure occurs.

We register it before the Activity executes because it would be impossible to add it after the activity executes in the current format. If we added it after the Activity call and that call failed, all lines of code after become unreachable, so the compensating action would never be registered.

If you don’t require compensation after a failed Activity, then you have the option to not implement that. This example shows performing a compensation on an Activity we know we want to compensate.

---

<div class="post-metadata">

**Author:** ![shaykemelov](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaykemelov](https://community.temporal.io/u/shaykemelov)\
**Post date:** [January 29, 2026, 8:31pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/3 "2026-01-29T20:31:24Z")

</div>

> [@mmegger](#):
>
> we are just registering the compensating action with the Saga object, not actually performing the compensation

Yeah, I understand it

> [@mmegger](#):
>
> We register it before the Activity executes because it would be impossible to add it after the activity executes in the current format. If we added it after the Activity call and that call failed, all lines of code after become unreachable, so the compensating action would never be registered.

I think, it’s not the best example for educational purposes

```auto
saga.addCompensation(activities::refundCustomer, creditCardInfo);
creditCardConfirmation = activities.processCreditCard(creditCardInfo, bill);

```

If a processCreditCard method fails, a credit card balance is not changed, but a customer will receive an unexpected refund. It’s not good, or may be I missed something

–

Scenario with swapped lines:

if processCrediCard completed without exceptions, temporal will register a compensation because of a durable execution and it is ok (e.g. if cancellation request after withdrawal, it will be compensated) , and will not register a compensation for a failed action, because it did not change a state of credit card. And it makes sense: in a real world we would have something like a transaction Id that we are required to pass to a refund method

```auto
var transactionId = activities.processCreditCard(creditCardInfo, bill);
saga.addCompensation(activities::refundCustomer, new RefundContext(creditCardInfo, transactionId));

```

> [@mmegger](#):
>
> If you don’t require compensation after a failed Activity, then you have the option to not implement that.

Yeah, I wanted to see that, thank you

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [January 29, 2026, 9:48pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/4 "2026-01-29T21:48:10Z")

</div>

```auto
saga.addCompensation(activities::refundCustomer, creditCardInfo);
creditCardConfirmation = activities.processCreditCard(creditCardInfo, bill);

```

The compensation implementation should be skipped if processCreditCard didn’t happen. This solves the scenario of processCreditCard succeeding, but returning failure, for example, because of a timeout.

If you can wait indefinitely and guarantee that processCreditCard fails only due to business errors and when the processing didn’t happen, then you can reverse the order of these statements.

But in situations when you can wait a limited amount of time and you need to deal with processCreditCard timing out then you have to register compensation before.

---

<div class="post-metadata">

**Author:** ![shaykemelov](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaykemelov](https://community.temporal.io/u/shaykemelov)\
**Post date:** [February 7, 2026, 7:06am UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/5 "2026-02-07T07:06:42Z")

</div>

Thank you

I come with another case

What if processCreditCard failed because of business logic: a customer does not have enough money and activity throws non retryable exception?

And I was thinking about:

> [@maxim](#):
>
> The compensation implementation should be skipped if processCreditCard didn’t happen

I think we need something like:

```java
String paymentOperationId = Workflow.randomUUID().toString();
saga.addCompensation(activities::refundPayment, paymentOperationId);
activities.processCreditCard(paymentOperationId, creditCardInfo, bill);

```

And it will solve all my questions, right?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [February 16, 2026, 10:15pm UTC](https://community.temporal.io/t/why-do-we-add-compensation-action-before-action-we-want-to-compensate/19226/6 "2026-02-16T22:15:35Z")

</div>

This looks good to me.
