Notes

Part 9 of 11

Durable execution and workflows

In a service-based app, work is split across services. Take a trip booking that uses three services: one to hold a seat, one to charge the card, and one to email the ticket. Booking one trip means calling all three in order.

  • Each step is a task, and the full sequence of steps is a workflow. Some tools call tasks "activities" or "durable functions"; the idea is the same.
  • A workflow is usually a graph of tasks. It can be written in a normal programming language, a special-purpose language, or a markup format.
  • A workflow engine decides when and where each task runs, what to do when something fails (like a machine crashing), and how many tasks run at once.
  • The engine has two parts: an orchestrator that schedules tasks, and executors that run them.

Workflows can be triggered in two ways:

  • On a schedule, such as every hour, by the orchestrator.
  • By something outside, such as a call to a web service or a person pressing a button.

There are three broad kinds of workflow engines:

KindUsed for
Data (ETL) orchestrationRunning data pipelines, like nightly jobs that move and transform data
Business process enginesWorkflows drawn as diagrams, so people who are not engineers can design them
Durable executionService-based apps that need steps to behave like one transaction

Durable execution matters when a sequence must happen exactly once. You want each trip booked once. A failure halfway could charge the card but never hold the seat. And you cannot wrap several services, let alone another company's payment service, in one database transaction.

How it works:

  • The framework writes every remote call and every state change to durable storage, much like a write-ahead log.
  • If a task fails and runs again, calls that already finished are skipped. Their logged results are returned instead of calling the service a second time.
  • This gives exactly-once behavior for the workflow as a whole.

Its pitfalls:

  • Outside services still need idempotent APIs. The framework cannot make another service safe to retry, so you must pass unique IDs (like a booking ID) so a repeat charge is spotted and dropped.
  • Code changes can break running workflows. Replay expects the same calls in the same order, so even reordering two calls can break workflows that are mid-run. It is safer to deploy the new code as a separate version: old runs replay with old code, and new runs use new code.
  • Code must be deterministic. Replay must make the same choices as the first run, so random numbers and the system clock break it. Frameworks offer safe replacements, but you must remember to use them; some also offer checks that catch mistakes before running.