Webhook and MongoDB: Saga Compensation Rollback Template

Build a Spojit workflow that records each completed step of a multi-step process in MongoDB and, if a later step fails, runs compensating http calls to undo the work it already did.

What It Builds

This template starts from a Webhook trigger that receives an order or request payload, then runs several Connector nodes in sequence. After each successful step it writes a record to MongoDB so the workflow always knows exactly how far it got. If a downstream step throws an error, a fallback branch reads those MongoDB records back and fires compensating http calls in reverse order to reverse each completed step, the saga rollback pattern, leaving the external systems in a clean, consistent state.

The Prompt

Paste this into Miraxa and it builds the workflow, connecting the tools for you:

Build a workflow with a Webhook trigger that receives an order payload. Run three steps in sequence: reserve stock, charge payment, and create a shipment, each by calling the relevant external REST API with an http-post Connector node. After each step succeeds, insert a record into a MongoDB collection called "saga_steps" capturing the order id, the step name, and the response id needed to undo it. If any step fails, find the already-completed records for that order id in MongoDB and run compensating http calls in reverse order to release the stock, refund the payment, and cancel the shipment, then send me a Send Email summary of what was rolled back.

Connectors Used

  • Webhook trigger - receives the inbound order payload as the parsed JSON body and starts the run.
  • http - calls each external system's REST API with http-post for the forward steps and the matching reverse calls for compensation.
  • mongodb - insert-documents records each completed step and find-documents reads them back when a rollback is needed.
  • Send Email node - reports which steps were undone after a failure.

Customize It

Swap the three example steps (reserve stock, charge payment, create shipment) for your own process, and change the MongoDB collection name from saga_steps to whatever you prefer. The key data to capture per step is the order id plus the response id you need to undo that step (for example a charge id or reservation id), so adjust those fields to match each API. If a step has no real reverse action, drop its compensating call from the prompt rather than inventing one.

Tips

  • Keep each http Connector node in Direct mode for the forward and compensating calls so they stay deterministic and cost no AI credits. Reserve Agent mode for steps that genuinely need judgement.
  • Record the undo identifier (charge id, reservation id) in each MongoDB document at the moment a step succeeds, so the rollback branch never has to guess what to reverse.
  • Run the compensating calls in reverse order of the steps you completed, and only for steps that actually wrote a record, so you never try to undo work that never happened.

Common Pitfalls

  • Forgetting to write the MongoDB record before the next step runs, which leaves the rollback branch blind to a step that already changed an external system.
  • Assuming every external API has an undo endpoint. Confirm the reverse call exists (refund, cancel, release) before relying on it, and handle the case where a compensating call itself fails.
  • Webhook deliveries can repeat. Use the inbound order id as your MongoDB key so a replayed event does not double-charge or double-ship.

Related Articles

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.