A Kestrel incident-response workflow might analyze a failed Kubernetes workload, propose a fix, wait for approval in Slack and then open a GitOps pull request. The approval could take hours or weeks. If Kubernetes replaces the worker in the meantime, its successor needs to know which steps have finished and whether the workflow is still waiting.
Kestrel records that progress in Postgres, its existing system of record. Rather than deploying a separate orchestrator such as Temporal or AWS Step Functions, each application server runs an embedded workflow library that reads and writes the same tables. That choice keeps a second stateful service off the execution path and avoids sending workflow payloads, which can include source code, logs and infrastructure details, to another system for storage.
Claiming work without holding a lock for hours
A trigger, such as a PagerDuty alert or a GitHub webhook, inserts a row into workflow_executions. Workers poll for queued rows. In a short transaction, a worker uses FOR UPDATE SKIP LOCKED to select and lock a row, marks it as running, records its worker ID and sets a lease expiry. Other workers skip the locked row instead of waiting for it. The transaction commits before the worker starts the task, so a slow external call does not hold a database row lock.
This transaction handles scheduling without a message broker, leader election or an external lock service. Workers can claim several rows per transaction to reduce database round trips, at the cost of leaving more work to recover if a worker fails. Skipping locked rows also means the queue cannot guarantee strict first-in, first-out order under contention.
What survives a restart
Each completed step writes its output to operation_outputs, whose primary key is (execution_id, step_id). Before running a step, the workflow library checks for that key. If it finds a result, it passes the stored output to the next step rather than repeating the work. An ON CONFLICT ... DO NOTHING insert prevents a later write from overwriting the first committed result.
The distinction is between one committed checkpoint and one external action. The primary key guarantees the former. If a worker makes an external call and dies before recording its result, there is no checkpoint for its successor to find; the database constraint alone does not guarantee exactly-once execution of that call.
A running worker periodically renews its lease. In Kestrel’s example, each heartbeat extends it by 30 seconds. A periodic cleanup query, called a sweeper, returns executions with expired leases to the queue so another worker can resume from the saved outputs. A shorter lease speeds recovery after a crash but can also cause a slow worker to lose its claim while it is still running. A longer lease delays reassignment after a genuine failure.
Planned waits need no live worker. At a sleep or approval step, the library marks the execution as waiting and stores a row in workflow_waits with a wake_at timestamp. A periodic query requeues executions whose wake time has arrived. Whether an approval window lasts two hours or two weeks, the wait occupies a row rather than tying up a thread.
Where the design fits
Kestrel’s pipelines are I/O-heavy, run for seconds to hours and need moderate concurrency. The team reports observing tens of thousands of workflows per second on a single Postgres instance without read replicas or Citus. That is an observation from its deployment, not a general throughput guarantee. For workloads that must dispatch across thousands of workers with sub-millisecond latency, the team says a dedicated engine such as Temporal may be a better fit.
Adding stateless workers increases throughput until Postgres becomes the bottleneck. Hundreds of polling workers can exhaust the connection limit; PgBouncer transaction pooling and having idle workers poll less often, at staggered intervals, can reduce that pressure. Frequent queue updates and deletes can also cause table bloat. The team identifies table-specific autovacuum tuning, partial indexes and partitioning completed executions away from the active queue as ways to keep the working set small.
Keeping workflow state in relational tables also changes how operators investigate failures. They can join executions, steps and approvals to ask which proposed fixes were rejected and at what step, without exporting the data to a separate analytics system. The trade-off is that workflow availability depends on the existing Postgres high-availability setup. If queue traffic begins to compete with application traffic, a separate Postgres deployment can isolate the workloads while preserving the same recovery and monitoring model.
