Run Workflow Steps in Parallel

In this chapter, you'll learn how to group workflow steps that don't rely on one another's results.

composer.Parallelize Function#

If your workflow has steps that don't rely on one another's results, group them with composer.Parallelize from the Workflows SDK.

Parallelize accepts functions as parameters. Each function runs a step, and returns the step's error. The workflow continues to the next step after all the functions finish.

For example:

src/workflows/my-workflow.go
// MyWorkflow creates the product's prices and attaches it to a sales
// channel at the same point of the workflow, since neither step relies on
// the other's result.
var MyWorkflow = composer.CreateWorkflow("my-workflow",
	func(e *composer.Execution, input WorkflowInput) (Product, error) {
		product, err := createProductStep.Run(e, input)
		if err != nil {
			return Product{}, err
		}

		var (
			prices              []string
			productSalesChannel string
		)
		err = composer.Parallelize(
			func() (err error) {
				prices, err = createPricesStep.Run(e, product)
				return err
			},
			func() (err error) {
				productSalesChannel, err = attachProductToSalesChannelStep.Run(e, product)
				return err
			},
		)
		if err != nil {
			return Product{}, err
		}
		_, _ = prices, productSalesChannel

		return getProductStep.Run(e, product.ID)
	})

In this example, the workflow creates a product, then creates its prices and attaches it to a sales channel. The two steps don't rely on each other, so they're passed to Parallelize.

Each function stores the step's result in a variable declared before the call, so you can use the results after Parallelize returns. Return the error of the Parallelize call from the workflow, as with any step error.

How Parallelize Executes the Steps#

MedusaGo runs the functions you pass to Parallelize one after the other, in the order you pass them. It doesn't run them at the same time. A step doesn't start before the previous one finishes.

Like in Medusa, a failure in one of the functions doesn't prevent the others from running. Parallelize runs all the functions, then returns the first error. The steps that succeeded are compensated with the failed one.

Note: Don't start your own goroutines to run steps concurrently. The compensation functions run in the reverse order in which the steps finish, so with concurrent steps the order of the rollback would depend on timing.
Found a problem with this page?Report Issue
Edited Nov 14·Edit this page