CSE Roadmap

Core tier · about 3 hours · 12 parts

Part 5 of 12 · 1 min read

Waiting and undoing

Human approvals as journalled pauses, and sagas for the step that cannot be retried.

Two more situations fit the same journal.

Waiting. When step 4 needs a human to approve the charge, the worker does not sit and wait. It records "waiting for approval", exits, and frees its slot. When the approval arrives, any worker replays the journal and continues. In the example this is a step of kind wait that raises Suspended until a signal with its name is in the journal. A wait of three hours costs nothing while it waits.

Undoing. Sometimes a later step fails for good after earlier effects already happened: the email bounced permanently after the card was charged. You cannot roll back a payment the way a database rolls back a transaction, so you run the undo steps in reverse order (from compensate() in the code):

# from code/mini-durable/durable.py
for r in reversed(journal.records):
    st = next((s for s in steps if s.name == r["name"]), None)
    if st is not None and st.compensate:             # "charge_card" -> "refund_card"
        world.do_effect(st.compensate, run_id, True)

Saga and compensation

A saga is a multi-step operation where each effect has an undo step instead of a single transaction around everything. A compensation is that undo step.

Example: charge_card is compensated by refund_card; send_email by send_retraction. Compensations must themselves be idempotent, and some effects have no real undo, which is why approvals before irreversible steps are better than undo after them.