Database

Manage Database Transactions in Multi-Step Workflows

Choose transaction boundaries, isolation expectations and recovery behavior for workflows that update several records.

A multi-step workflow may need to update an order, reserve inventory and record a payment state. If related database changes must succeed together, use a transaction with a clearly defined boundary. Transactions do not automatically make external services part of the same atomic operation.

Keep the transaction focused

Begin a transaction immediately before the related database changes and commit as soon as the invariant is restored. Avoid user interaction, long file processing or remote API calls while locks are held. Long transactions increase contention and make failures harder to recover from.

Handle failures consistently

Use try/catch or a structured transaction helper to roll back when an operation fails. Ensure exceptions are not swallowed after a partial operation. Check that all participating tables use a transactional storage engine.

Understand isolation and locking

The default isolation level may be sufficient for many workflows, but concurrent updates can still create lost updates or deadlocks. Use conditional updates, unique constraints and appropriate row locks to enforce invariants. Keep lock acquisition order consistent where possible.

Coordinate external side effects

A database transaction cannot roll back an email, remote API call or message already sent. Record the intended side effect in an outbox within the transaction, then deliver it asynchronously with retry and idempotency controls.

Checklist

  • Keep transactions short and purpose-specific.
  • Confirm tables use a transactional engine.
  • Handle rollback and exceptions explicitly.
  • Enforce invariants with constraints and conditional updates.
  • Coordinate external effects through recoverable workflows.
Share this page Help other developers discover this resource.