Saga orchestrator with table-backed state
Multi-step provisioning workflow whose state lives in a regular table — queue is the trigger.
A "create database" saga has 4 steps: allocate volume → start machine → run migrations → email user. Each step writes the state row + enqueues the next step in one transaction. If anything fails, the row stays at the last successful step; a retry consumer picks it up where it left off.
sql
BEGIN;
UPDATE provisioning
SET state = 'volume_allocated',
volume_id = $1
WHERE database_id = $2;
QUEUE PUSH provision_step
json_build_object(
'database_id', $2,
'next_step', 'start_machine'
);
COMMIT;
-- Volume allocation + state transition + next-step trigger
-- are atomic. If COMMIT fails, the saga has not advanced.