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

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):

func exampleWorkflow(ctx dbos.Context, input string) (string, error) {
	// Random value in workflow breaks recovery!
	// On replay, rand.Intn returns a different value,
	// so the workflow may take a different branch.
	if rand.Intn(2) == 0 {
		return stepOne(ctx)
	}
	return stepTwo(ctx)
}

Correct (non-determinism in step):

func exampleWorkflow(ctx dbos.Context, input string) (string, error) {
	// Step result is checkpointed - replay uses the saved value
	choice, err := dbos.RunAsStep(ctx, func(ctx context.Context) (int, error) {
		return rand.Intn(2), nil
	}, dbos.WithStepName("generateChoice"))
	if err != nil {
		return "", err
	}
	if choice == 0 {
		return stepOne(ctx)
	}
	return stepTwo(ctx)
}

Non-deterministic operations that must be in steps:

  • Random number generation
  • Getting current time (time.Now())
  • Accessing external APIs (http.Get, etc.)
  • Reading files
  • Database queries

When an execution records a step ID it had already recorded with a different result (for example because steps ran concurrently in plain goroutines instead of dbos.Go), DBOS returns an error matching dbos.ErrStepNondeterminism (v1.6.0+). Return it from the workflow and fix the workflow function.

Reference: Workflow Determinism