Self-hosting
Run the engine as a plain Node process on Postgres with journeyNode, scale it to several processes, size its concurrency, and ship it with Docker.
This page is for a team that wants the engine on its own servers: a VM, a container platform, Kubernetes. At the end you will have a Node process that serves the API and runs every journey, with Postgres as its only dependency, a Dockerfile and a docker-compose file to run it, and the numbers to size it. The config, workflows and templates are the ones the Cloudflare and AWS Lambda hosts run.
How it works
There is no Workflow binding and no durable-function service. The process is the runtime: each journey instance is a row in the run table, its checkpoints are rows in run_step, and workers in every process started this way take due runs a turn at a time. A turn replays the journey from the top and answers finished steps from their checkpoints, the same model Cloudflare Workflows and Lambda durable functions use on their side.
The entry
sqlis a client several things may use at once: a pool. The host builds its stores over it and never closes it, so closing it at shutdown is yours. Any driver works through the smallSqlClientLikeinterface (queryandtransaction);postgresJsClientadapts postgres.js.varsis what the other hosts read from their platform's env:ENVIRONMENT,API_TOKEN,WORKFLOW_ORIGIN, and the push secrets. They are checked against the config'senvschema like theirs.emailpicks the transport. On Node, usesesEmailTransport(credentials from the environment or passed explicitly) orcloudflareEmailTransport({ accountId, apiToken }), which sends through Cloudflare's REST API.
Options
| Option | Default | Meaning |
|---|---|---|
concurrency | 10 | Runs one process takes at once |
retry | 5 retries, 10 s doubling, 10 min per attempt | A step's retry policy (limit, delayMs, timeoutMs) |
leaseMs | 60,000 | How long a claimed run is held before another process may take it |
pollMs | 1,000 | How often an idle worker looks for due runs when nothing told it one is |
sweepMs | 15 minutes | How often the sweep runs (segments, date triggers, maintenance) |
retentionMs | 7 days | How long a run that ended is kept before the sweep removes it |
maxBodyBytes | 10 MB | The largest request body the server reads; larger answers 413 |
version | None | This code's version; see below |
authorize, observer, messages, runtime, env and contractMode work as on the other hosts.
start, stop and the rest
| Member | Does |
|---|---|
start({ port, hostname }) | Starts the workers, the sweep timer, and an HTTP server when port is given |
stop() | Stops taking work, closes the server, waits for turns in flight to reach their next checkpoint and for a sweep in progress to finish its round |
fetch(request) | The HTTP API as a fetch handler, for mounting in your own server |
drain(limit?) | Takes every run that is due now a turn further, with nothing started |
sweep() | The sweep, now |
runtime | The NodeRuntime underneath |
For tests, journeys.fetch(request) and journeys.drain() run everything in-process with nothing started, over PGlite if you like. See testing.
Deploying a new version
With version, the bundle compiled into the process goes live by itself, and only over an older one, so two versions running side by side during a rolling deploy cannot take the routes back and forth. Give each build a unique id and a createdAt that grows with every build, such as the commit SHA and the build time. Without version, deploy with POST /bundle/deploy and the bearer after each start.
Run pnpm exec sendwend plan --database-url $DATABASE_URL before rolling out, as on the other hosts (versioning), and migrate first: the host refuses to apply a bundle until the tables match.
Several processes
Run as many processes as you like, on as many machines as you like, against one database:
- A run is claimed with
FOR UPDATE SKIP LOCKED, so two workers never take the same run and neither waits on the other. - Every claim increments the run's fence, and every write of a turn carries the value it claimed with. A worker that stalled past its lease, rather than died, cannot write over the one that took its run over.
- A run whose process died is taken again once its lease expires (
leaseMs, 60 seconds). Leases are told by each process's own clock, so the machines' clocks must agree to well within that, which NTP does. One far ahead takes runs that are still being run. - One process runs each sweep. The turn is a single row that whoever moves its time forward first claims; no lock is held while the sweep runs, so a process that dies in it costs one interval. Processes check several times an interval, so a survivor is not a whole interval late.
Steps are at least once, as on the other runtimes: a process that dies between a step's effect and its checkpoint leaves the step to be run again. Message delivery is protected separately by the engine's claim on each copy (delivery).
Size concurrency from your slowest action
A run holds one of the process's places while its action runs, so a process does concurrency ÷ action time actions a second. At the default of 10 and a send that takes a second, that is 10 sends a second, with the process and the database idle. Waiting on a provider costs almost nothing, so 200 is reasonable on one vCPU. The pool need not grow with it: a connection is held for a query, not for an action, and postgres.js's default pool is enough to start with.
How fast it is
On a 2 vCPU Postgres the Node host ran about 450 runs (1,350 actions) a second, whatever the size of the user base up to a million, with the database being the limit. The figure comes from bench/node-host.mjs in the SDK repository, which uses no-op actions and sends nothing. The setup, the per-population tables and the effect of slow actions are in the benchmark.
This host is newer than the other two. Its claim, its wakes and a process killed mid-step are tested on a real Postgres with several connections and processes.
Docker
The workflows and templates are TypeScript and TSX, so bundle the entry for Node at build time. With esbuild, keeping dependencies external:
CMD in exec form makes Node PID 1, so docker stop's SIGTERM reaches the shutdown handler. esbuild must be a dev dependency of the project.
docker-compose with Postgres
The engine's tables must exist before it starts. For a first run, write them as plain DDL once and let the Postgres image apply it on initialization:
The init directory runs only on an empty data volume, so it is a first-run convenience, not a migration tool. For later SDK upgrades, generate the drizzle file and migrate as on Postgres. Postgres must be 15 or later. Pass GIT_SHA and BUILD_TIME from your build so each image carries its own version.
Graceful shutdown
stop() stops taking new runs, closes the HTTP server, and waits for each turn in flight to reach its next checkpoint, then for a sweep in progress to finish its round. A step attempt may take up to 10 minutes, longer than most orchestrators wait, so set the grace period (stop_grace_period, Kubernetes' terminationGracePeriodSeconds) to cover your slowest action. A turn cut off by SIGKILL is not lost: its lease expires and another process runs the step again.
Health
GET /health answers { "ok": true } without authentication. The route itself does not query the database, so use it as a liveness check. For readiness against Postgres, check the database from your orchestrator, or call an authenticated route.
Pitfalls
- No reverse proxy. The server speaks plain HTTP. Terminate TLS in front of it, and use an https
WORKFLOW_ORIGIN, or no one-clickList-Unsubscribeheader is sent. - The same
version.createdAtfor different builds. A build only goes live over an older one; a build time that does not grow means the new routes never apply. - Clock skew between machines. Leases are read off each process's clock.
- Click classification. The requesting network is known on Cloudflare only, so the rules that use it do not apply on Node and the human click rate reads higher.
- One database schema per engine. Two separate deployments on one schema swap each other's routes. Several processes of one deployment on one schema is the intended setup.