Skip to main content
Reference vendor/dbos 1 min read

workflow-constraints


title: Follow Workflow Constraints impact: CRITICAL impactDescription: Violating constraints breaks recovery and durability guarantees tags: workflow, constraints, rules, best-practices

Follow Workflow Constraints

Workflows have specific constraints to maintain durability guarantees. Violating them can break recovery.

Incorrect (starting workflows from steps):

func myStep(ctx context.Context) (string, error) {
	// Don't start workflows from steps!
	// The step's context.Context does not support workflow operations
	return "", nil
}

func myWorkflow(ctx dbos.Context, input string) (string, error) {
	// Starting a child workflow inside a step breaks determinism
	dbos.RunAsStep(ctx, func(ctx context.Context) (string, error) {
		handle, _ := dbos.RunWorkflow(ctx.(dbos.Context), otherWorkflow, "data") // WRONG
		return handle.GetWorkflowID(), nil
	})
	return "", nil
}

Correct (workflow operations only from workflows):

func fetchData(ctx context.Context) (string, error) {
	// Steps only do external operations
	resp, err := http.Get("https://api.example.com")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	return string(body), nil
}

func myWorkflow(ctx dbos.Context, input string) (string, error) {
	data, err := dbos.RunAsStep(ctx, fetchData, dbos.WithStepName("fetchData"))
	if err != nil {
		return "", err
	}
	// Start child workflows from the parent workflow
	handle, err := dbos.RunWorkflow(ctx, otherWorkflow, data)
	if err != nil {
		return "", err
	}
	// Receive messages from the workflow
	msg, err := dbos.Recv[string](ctx, "topic", 60*time.Second)
	// Set events from the workflow
	dbos.SetEvent(ctx, "status", "done")
	return data, nil
}

Workflow operations that return an error when called inside a step: starting or enqueueing workflows, RunAsTransaction, handle.GetResult, CloseStream, Send, Recv, GetEvent, Sleep, and Go. Read operations (e.g. ListWorkflows), SetEvent, WriteStream, and GetAuthenticatedUser are allowed inside steps.

Additional constraints:

  • Don’t modify global variables from workflows or steps
  • All workflows and queues must be registered, and all datasources created with NewDataSource, before Launch() (step-transactions.md)
  • Concurrent steps must start in deterministic order using dbos.Go/dbos.Select
  • Return DBOS errors such as dbos.ErrConflictingWorkflowID from the workflow instead of swallowing them: it means another execution owns the workflow (it was recovered, resumed, rewound, or cancelled elsewhere), and returning the error lets this execution adopt the owner’s outcome

Reference: Workflow Guarantees