Gracefully Cancelling a Process Instance

By
  • Blog
  • >
  • Gracefully Cancelling a Process Instance
TOPICS

30 Day Free Trial

Bring together legacy systems, RPA bots, microservices and more with Camunda

Sign Up for Camunda Content

Get the latest on Camunda features, events, top trends, and more.

TRENDING CONTENT
Cancelling a running process instance in Camunda is easy:

 

DELETE /process-instance/{id}

or, using Java API:

RuntimeService.deleteProcessInstance(String, String)

this will simply delete the process instance and remove it from the database. Sometimes this is exactly what you need.

However: what if you want to cancel the process instance gracefully? Gracefully in the sense that the effects it has had on the outside world are undone? The answer to this is compensation. In this post I discuss two ways to implement compensation.

Internal Compensation

Modeling compensation inside the process itself:

Travel booking process with Internal Compensation

The compensation undoes the effects of the process so far. This is usually modeled in a way where you attach a compensation handler to those service tasks which have effects on “the outside world” and implement logic which undoes the effects of those service tasks. Then, top level inside the process you can have an interrupting event subprocess with a message start event followed by an intermediate compensation throw event.

When you send the message it will

  1. interrupt (effectively cancel) everything currently happening inside the process instance.
  2. throw compensation which will propagate to all compensation handlers which have been activated so far.

Advantage

  • everything self-contained inside the same process model
  • compensation handlers can directly access variables of the process instance
  • the process engine “knows” which service tasks have already been executed (effectively how far the process instance made progress) and handles triggering of the right compensation handlers for you

Downside

  • you cannot implement it “retro actively” in the sense that it already has to be modeled inside the process before you deploy the process.
  • if every service task has a compensation handler, the model may become “cluttered”.

External Compensation

Modeling a second process which undoes the effects of the first process.

First, you model a regular process (lets call it the “main process”) without any compensation logic:
Travel booking process without any compensation logic

Then you can model a second process (lets call it “compensation process”) which undoes the effects of the main process:

Travel booking process with compensation logic

The compensation process can load the variables of the main process from history and may also check history to see how far the main process has made progress (because it does not know which services in the main process were executed and which services were not).

Or Better: you provide the necessary variables as input of the compensation process and make the compensation services idempotent. Meaning, the Cancel Flight service does nothing if no flight has been booked. That way you can just call them all.

Advantage

  • can be done retro-actively after the “main process” has been deployed into production
  • cancellation and compendation logic do not “clutter” main process model

Downside

  • if you change the main process you may have to change the compensation process. People tend to forget to do this 🙂 If you have everything inside a single model, this is simpler.
  • You either need to load the progress of the main process from history or make the compensation services idempotent
 
Any thoughts?

Camunda Developer Community

Join Camunda’s global community of developers sharing code, advice, and meaningful experiences

Try All Features of Camunda

Related Content

We're streamlining Camunda product APIs, working towards a single REST API for many components, simplifying the learning curve and making installation easier.
Learn about our approach to migration from Camunda 7 to Camunda 8, and how we can help you achieve it as quickly and effectively as possible.
We've been working hard to reduce the job activation latency in Zeebe. Read on to take a peek under the hood at how we went about it and then verified success.