reflex-workflow: durable workflows on the application's own tables
Type of change
- New feature (non-breaking change which adds functionality)
What
A new workspace package, reflex-workflow: a prototype durable-workflow engine where a workflow is an ordinary SQLAlchemy table and each row is a run. There's no separate workflow store and no replay. The engine keeps its state in columns on your table, steps are async methods, and a run's data is the row you already query.
class Expense(Base, Workflow):
__tablename__ = "expense"
id: Mapped[int] = mapped_column(primary_key=True)
amount: Mapped[int]
status: Mapped[str] = mapped_column(default="submitted")
@step
async def submit(self):
return wait_for(Expense.decide, timeout=timedelta(days=7), on_timeout=Expense.expire)
@step
async def decide(self, verdict: str):
self.status = verdict
@step
async def expire(self):
self.status = "expired"
await Expense(amount=40).start(Expense.submit)
await Expense.by(Expense.id == 7).deliver(Expense.decide("approve"), key="click-123")
How it works
- Claiming. A worker claims due rows with
FOR UPDATE SKIP LOCKED, picked in a materialized CTE so theLIMITholds whatever plan Postgres chooses. The claim leases each row and bumps itswf_version. - Running a step. The worker runs one step, then commits the row's changed columns and the next step in a single
UPDATEfenced on the claimed version. A worker whose lease was taken over, or a run that was moved on, cannot commit. - Semantics. Execution is at-least-once. Every time comes from the database clock.
Features
- Events and timers: waits with timeouts; deduplicated event keys; events that arrive before a wait are held until it starts; timers, intervals and cron. The cron parser is dependency-free and differential-tested against
cronsimandcroniter. - Fan-out: child runs with an exactly-once join.
- Limits: per-group concurrency and rate limits, enforced across workers.
- Scheduling: fairness between tables, and lanes that keep steps on particular workers.
- Operations:
connect_workflows, to address runs from processes that don't run them;LISTEN/NOTIFYbetween workers; optional attempt history.
Examples
packages/reflex-workflow/examples/ rebuilds 13 of the 14 scenarios from the Notion examples table. Each has its pass condition as tests in tests/units/reflex_workflow/examples/, and the table links each row to its code and status. The examples live beside the package, not in the wheel. Example 14 needs cancellation, pausing a run, and forking from a checkpoint, none of which exist yet.
Benchmarks
packages/reflex-workflow/benchmarks/ measures throughput and timer lateness against real Postgres with pinned worker processes. It checks every figure against the rows: each run records which worker ran it and when, and a trial where any worker or customer exceeded its cap fails instead of printing a number. Headline numbers are on one laptop; the README has the rest.
- Per-step cost: a worker is CPU-bound at about 2 ms of Python per step, most of it SQLAlchemy. That's about 480 no-op steps/s per process, and it scales nearly linearly with processes.
- Realistic steps: with 50 ms steps it reaches 81–84% of the concurrency ceiling at 1, 2 and 4 workers.
- Timer lateness: about half the poll interval (p95 52 ms at a 50 ms poll).
The harness is what found the over-claim bug that's fixed in this branch. UPDATE … WHERE pk IN (SELECT … LIMIT n FOR UPDATE SKIP LOCKED) could claim every due row under some plans, including breaching a declared per-group limit. tests/units/reflex_workflow/engine/test_claim.py forces that plan.
Also in this PR
reflex-release: the dispatch checkbox cap goes from 19 to 24.workflow_dispatchtakes 25 inputs since December 2025, and the new package needs one.reflex-base:reflex_workflowis registered inPACKAGE_LOGGER_NAMES.- Dev dependencies:
cronsimandcroniter, for the differential cron tests. .gitignore: the root file ignores everyexamples/directory, so the two example directories this package owns are unignored.
Testing
- Needs Postgres, which CI doesn't have. The engine and example tests need
REFLEX_TEST_POSTGRES, and CI has no Postgres service, so they're skipped there (they're most of the 77 skips). Locally all 884 pass against Postgres 18, three consecutive clean runs. Adding a Postgres service tounit_tests.ymlwould be a good follow-up. - Rest of the suite: passes, with coverage at 79.06% (the floor is 72%).
- Other checks:
ruff,pyright,pre-commit, andcheck_min_deps.pypass. The SQLAlchemy lower bound is 2.0.20, bisected as the first version the package type-checks against. - Mutation testing: the guarantees were mutation-tested, meaning each safeguard was removed to confirm a test fails without it. That caught several tests that were passing for the wrong reason, which were then fixed.
Known gaps
- Live progress: progress is polled; nothing pushes changes to a connected browser yet.
- Not built: cancelling a run and its children, pausing a run, and forking from a checkpoint.
- Undecided policy: what happens to runs in flight when a deploy renames a step or changes its arguments, and a size limit on state stored in the row.
- Also missing: OTel metrics. The default lease (5 minutes) is also longer than the 60-second recovery target.
- Foreign-key stalls: the claim uses
FOR UPDATE, which conflicts with theFOR KEY SHARElock a foreign-key insert takes. So a workflow row can't be claimed while another open transaction references it by foreign key. Example 13 shows the effect;FOR NO KEY UPDATEshould fix it. - Speed: profiling found about 15% to gain cheaply (building the claim statement once, building the lease-renewal update lazily, loading rows through Core), and about 26% more by having the claim return the whole row.