Skip to main content
Reference vendor/dbos 1 min read

workflow-determinism


title: Keep Workflows Deterministic impact: CRITICAL impactDescription: Non-deterministic workflows cannot recover correctly tags: workflow, determinism, recovery, reliability, DBOSStepNondeterminismError

Keep Workflows Deterministic

Workflow functions must be deterministic: given the same inputs and step return values, they must invoke the same steps in the same order. Non-deterministic operations must be moved to steps.

Incorrect (non-deterministic workflow):

import random

@DBOS.workflow()
def example_workflow():
    # Random number in workflow breaks recovery!
    choice = random.randint(0, 1)
    if choice == 0:
        step_one()
    else:
        step_two()

Correct (non-determinism in step):

import random

@DBOS.step()
def generate_choice():
    return random.randint(0, 1)

@DBOS.workflow()
def example_workflow():
    # Random number generated in step - result is saved
    choice = generate_choice()
    if choice == 0:
        step_one()
    else:
        step_two()

Non-deterministic operations that must be in steps:

  • Random number generation
  • Getting current time
  • Accessing external APIs
  • Reading files
  • Database queries (use datasources or steps)

If DBOS detects that a single execution of a workflow recorded different results for the same step, it raises DBOSStepNondeterminismError: the workflow is not deterministic.

Reference: Workflow Determinism