Skip to main content

Cloudflare Workflows Experience

Cloudflare has yet another new thing to play with - gotta try it right away.

Durable Execution Engine for Cloudflare Workers

First, check out the official documentation: workflows

Workflows is a durable execution engine built on Cloudflare Workers. Workflows allows you to build multi-step applications that can automatically retry, persist state, and run for minutes, hours, days, or weeks. Workflows introduces a programming model that makes it easier to build reliable, long-running tasks, observe their progress, and programmatically trigger instances based on events in your services.

Let's follow their guide and try it out.

Getting Started

Workflows allow you to build durable, multi-step applications using the Workers platform. Workflows can automatically retry, persist state, run for hours or days, and coordinate between third-party APIs.

You can build workflows to post-process files uploaded to R2 object storage, automatically generate Workers AI embeddings to Vectorize vector database, or trigger user lifecycle emails using your favorite email API.

This guide will walk you through:

  • Defining your first workflow and publishing it
  • Deploying the workflow to your Cloudflare account
  • Running (triggering) your workflow and observing its output

By the end of this guide, you should be able to write, deploy, and debug your own workflow applications.

Prerequisites

  • Have a Cloudflare account
  • Have Node.js installed

1. Define Your Workflow

To create your first workflow, use the create cloudflare CLI tool, specifying the workflow starter template:

npm create cloudflare@latest workflows-starter -- --template "cloudflare/workflows-starter"
tip

If creation fails, it's likely because cloning the template repository failed. Just go to https://github.com/cloudflare/workflows-starter to download it, then delete the ./git directory.

2. Create Your Workflow Steps

Each step in a workflow is an independent, retryable function. Steps are the power of workflows because you can encapsulate errors and persist state as the workflow progresses.

3. Configure Your Workflow

Before deploying your workflow, configure it in wrangler.toml.

4. Bind Your Workflow

Workers workflows can be triggered by HTTP requests, Queue messages, Cron Triggers, or the Workflows REST API.

5. Deploy Your Workflow

npx wrangler deploy

6. Run and Observe Your Workflow

npx wrangler workflows trigger workflows-starter '{"hello":"world"}'

Summary

The experience wasn't difficult - similar to writing different stages in a Jenkinsfile. One interesting aspect is that Workflows can automatically retry, persist state, run for hours or days, and coordinate between third-party APIs.

Since workflows are still in beta, checking back later will probably reveal more features and use cases.