Architecture

Architecture

How a Sendwend journey goes from TypeScript to a running instance, what lives in your repository, on the host and in your Postgres, and why the compiled workflow is data rather than code.

Sendwend splits a customer journey into three things that change at different speeds: the workflow you write, the engine that interprets it, and the platform that keeps it running for days or weeks. You write workflows as TypeScript. A compiler turns them into a versioned intermediate representation (IR), which is plain JSON. One generic runner, deployed once, reads that IR and executes every journey. This page explains how those pieces fit and why they are cut where they are.

From code to a running journey

text
 your repository                         the host                       your Postgres
 ───────────────                         ────────                       ─────────────
 contract.ts   ─┐
 templates     ─┤   compileConfig()      ┌──────────────────────┐       profiles, events
 workflows     ─┼──────────────────────▶ │ BundleIR (JSON)      │ ────▶ routing table
 segments      ─┤   (types + checks)     │  workflows + hashes  │       stored IRs by hash
 workflow.config.ts                      └──────────┬───────────┘       entry ledger
                                                    │                   messages, opens, clicks
                                                    ▼                   subscriptions
 your api ── JourneyClient.ingest() ──▶  /ingest ─▶ router ─▶ instance       ▲
                                                    │                         │
                                                    ▼                         │
                                          generic runner (interpreter) ───────┘
                                          one durable step per node
                                                    │
                                                    ▼
                                          email · push · Slack · Discord · actions
  1. You write a contract (events and profile fields as Zod schemas), templates, workflows and segments, and register them in one workflow.config.ts.
  2. The compiler (compileConfig) checks the whole project and produces a BundleIR: every workflow as a tree of nodes, each with a content hash. See Versioning.
  3. Deploying writes the bundle's routing table to Postgres: which event or segment starts which workflow, under which re-entry policy.
  4. Your api reports what users do through JourneyClient.ingest(). The host validates the batch against the contract, records the events, and starts an instance for each workflow whose trigger matches.
  5. The runner loads the instance's IR by the hash it entered on and walks it node by node. Each message, action and wait is a durable step, so a crash or redeploy resumes where it stopped.

The three planes

The package itself is divided along the same line:

PlaneWhat it isWhere it lives
AuthoringThe DSL, compiled to IR by compileBundlesendwend
ExecutionThe interpreter, over abstract portssendwend/engine
AdapterBindings, ingest, the runner for one hostsendwend/cloudflare, sendwend/aws, sendwend/node

The interpreter never touches a database or the network. Everything it needs (reading a profile, sleeping, sending a message, emitting an event) arrives through the ports defined in sendwend/engine. That boundary is what makes a journey testable without deploying one, and what lets the same interpreter run on three different platforms.

Why the IR is data

The DSL looks like a chain of method calls, but none of it runs at send time. Chained calls and condition expressions build a serializable graph. Sub-flow callbacks run exactly once, while the workflow is being defined, and values that are only known at run time are always references (u.name, checkout.payload.value), never closures.

ts
workflow({
  id: 'welcome',
  reentry: { mode: 'never' },
  trigger: trigger.event(e.sign_up),
})
  .email(email.getting_started, { name: u.name })
  .delay('3 days')
  .waitUntil(performed(e.activated), { timeout: '7 days', onTimeout: 'exit' });

compiles to a tree of a message node, a delay node and a wait_until node, with u.name stored as a reference to resolve when the email is sent. Keeping the workflow as data has concrete consequences:

  • One deployed runner executes every journey. A new workflow is a new bundle, not a new code path. On Cloudflare, one Worker serves all of them; on AWS, one Lambda function.
  • A running journey can be pinned to a version. Each workflow's IR is stored under its content hash, and an instance keeps the hash it entered on, so it finishes the version it started however often you deploy. See Versioning.
  • A deploy can be checked before it happens. plan() diffs two bundles, and the in-flight check compares a new bundle against the IRs journeys are still running on. Neither has to execute anything.
  • It can be drawn. Sendwend Studio and the canvases on this site render the same IR the runner executes.

Branching stays declarative on purpose: there are no custom conditions. An action that needs to influence routing writes a property or emits an event, and the decision is an ordinary condition on that data, so the canvas can always explain why a user took a path.

What lives where

PlaceWhat it holds
Your repositoryThe contract, templates (React email components, push, Slack, Discord), workflows, segments, custom actions and workflow.config.ts. Everything is reviewed and versioned like the rest of your code.
The hostThe compiled bundle and the code that serves it: the HTTP API (/ingest, /devices, /subscriptions, tracking routes), the runner, the senders. Secrets such as API_TOKEN and provider keys come from its environment, never from the config.
Your PostgresEverything the engine remembers: profiles and events, the routing table, stored IRs, the entry ledger, message copies with opens and clicks, opt-outs, devices and segment membership. The tables live in one schema, workflow by default.

The engine's tables are generated as a drizzle file (pnpm exec sendwend generate) or as plain DDL (renderPostgresDdl() from sendwend/store), and your migrations create them. Nothing creates or alters tables at runtime: a deploy checks the live schema first and refuses with every missing table or column. See Postgres.

The hosts

The same config runs on three hosts. They differ in what provides durability, not in what a journey does:

HostImportWhat makes steps durableGuide
Cloudflare Workerssendwend/cloudflareCloudflare Workflows, with Postgres reached through HyperdriveCloudflare
AWS Lambdasendwend/awsLambda durable functions, behind a Function URLAWS Lambda
Node (self-hosted)sendwend/nodePostgres itself: instances are rows, taken a turn at a timeSelf-hosting

On every host, steps are at least once: a process that dies between a step's effect and its checkpoint leaves the step to run again. Messages are kept to at most once on top of that by a claim in Postgres; Delivery explains how.

The SDK holds the hosts to one behavior with a conformance suite: one config, deployed on Node, Cloudflare and AWS, driven through the HTTP API every host serves, with each held to the same observable results (the actions called, the copies handed to the sender, and the entry, instance, message, event and subscription tables). A scenario never names a host. A difference between hosts has to be recorded on the scenario as a known divergence rather than hidden, and a script a local harness cannot drive is skipped with the reason in its title. Today no scenario records a divergence; a few are skipped on AWS, whose local durable runner cannot hold an instance in the middle of a sleep.

Where to go next

  • Contract: the events and profile fields everything else is typed by.
  • Workflows and Steps: writing a journey.
  • Testing: running a journey end to end in a unit test.