CSE Roadmap

Core tier · about 3 hours · 12 parts

Part 4 of 12 · 2 min read

Why the workflow code must take the same path every time

Replay compares code to journal, so clocks and dice must be recorded.

Replay works by lining up the journal against the code, record by record. Record 1 must be the step the code runs first, record 2 the second, and so on. If the code can take a different path on a different day, the line-up breaks.

In plain words: the orchestration code may not look at a clock, roll a die or read anything live to decide what to do next. The one exception is a value already written to the journal the first time.

Example: a workflow that says "if it is after 5 pm, skip the review step" ran at 4 pm and journalled [plan, review, draft]. The worker died; the restart happens at 6 pm. The code now wants to run draft as its second step, but the journal's second record says review. The engine stops rather than guess:

# from code/mini-durable/durable.py
if journal.records[i]["name"] != s.name:
    raise NonDeterminism(f"journal has {journal.records[i]['name']} but code runs {s.name}")

The fix is to record the time on first use and replay the recorded value, which is what the example's Ctx.random() does for random numbers.

Deterministic

Given the same journal, the code takes the same sequence of steps.

Structure hash

A short fingerprint of the list of step names and kinds. The journal stores it when the run starts; a restart refuses to resume if the code's fingerprint differs, because someone deployed a changed workflow while this run was in flight.

Example: a journal written for plan, research, draft does not fit new code that runs plan, research, outline, draft. The engine raises StructureChanged instead of replaying the wrong step. Production systems version workflows and let old runs finish on the old version.