# One Worker, and four seams that keep a split cheap

The repository's architectural decision, rationale and consequences.

# One Worker, and four seams that keep a split cheap

**Status:** accepted · **Date:** 2026-11-28

One Worker serves the web app, the API and the cron tick. The question was whether to split the API into its own Worker. The review of 2026-11-28 found that no Workers limit is near (CPU three orders of magnitude under the default, the script at 44% of the 64 MiB limit, a service binding adding no fee), that the database is the first bottleneck at every traffic level, and that a split moves none of the database's limits: the API Worker would open the same connections to the same Postgres. A split would cost two to three weeks and make deploys non-atomic, previews two Workers each and SSR data a serialized hop instead of an in-process call. The figures are on [Limits, cost and when to split](/docs/architecture/limits-and-scale).

## Decision [#decision]

Stay on one Worker. Keep four seams, each worth having without a split, so that a later split is a move and not a rewrite:

1. **Routes mount.** A web route under `routes/api/**` or `routes/[.]well-known/**` that reaches the backend only mounts a `(request: Request) => Promise<Response>` handler from `@template-cloudflare/api/<feature>/http`. Authorization and data access live in `packages/api`.
2. **Two modules own backend access.** In `apps/web/src`, `platform/rpc/orpc.ts` (in-process SSR calls) and `platform/auth/read-session.server.ts` (the SSR session) are the importers of the router, the Context, `auth`, `db` and `storage`; the Worker entry `server.ts` is the only other file that may reach a backend module. Everything else calls one of them, mounts a handler, or imports a leaf or a type.
3. **The entry states the request facts.** The Worker entry derives the request id and the trusted client address once per request and forwards them; no handler below it reads `cf-ray` or `cf-connecting-ip`.
4. **The API stays on the app's host** ([ADR-0019](0019-api-stays-on-the-app-host.md)).

`AGENTS.md` ("Keep the split cheap") states the four seams as rules and names what checks each one; the [enforcement matrix](/docs/development/enforcement) lists the same.

## When to revisit [#when-to-revisit]

Measure first; none of these is known for production yet: CPU per request by route (Workers Logs), the raw bundle size, the p95 statement time and Hyperdrive pool errors.

| Trigger                                                                                                                                 | First response                                                                                             |
| --------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| A second team or release cadence, or a third-party or non-JavaScript consumer that needs a versioned API with its own availability goal | Split on the same host; the contracts are already the interface                                            |
| An endpoint over about 1 s of CPU at p95, or work over 30 s                                                                             | Move that endpoint to its own Worker, a Queue or a Workflow, not the API                                   |
| WebSockets or presence                                                                                                                  | A Durable Object beside the Worker, not the API                                                            |
| Raw Worker bundle over 40 MiB, or startup over 400 ms                                                                                   | Stop compiling docs into the Worker first ([ADR-0018](0018-docs-compile-into-the-worker.md)); split second |
| Compliance that pins the API and the data to a jurisdiction while pages render globally                                                 | Split, with placement on the API Worker                                                                    |
| p95 statement over 50 ms, or Hyperdrive "Failed to acquire a connection from the pool"                                                  | Indexes, compute size, a read replica. A split does not help                                               |

## Consequences [#consequences]

SSR keeps calling the router in process: one Context and one session read per page, shared by every prefetch. Deploys stay atomic and a preview stays one Worker. With the seams in place, a same-host split is: an API Worker that mounts the same handlers and takes `scheduled`; an `RPCLink` over a `fetch`-based service binding inside `platform/rpc/orpc.ts` (placement hints apply to `fetch` handlers only); the session read behind `read-session.server.ts`; and the request facts forwarded as trusted headers by the entry. What the seams do not prepare is the deployment side: the preview scripts, `deploy-prod.yml` and the e2e stack assume one Worker.

## Alternatives [#alternatives]

- Split now. It buys independent deploys nobody needs yet, at the price above, and brings back the hop between renderer and data that server rendering exists to remove.
- Stay on one Worker without the seams. The 13 web files that value-imported backend modules, above all the files gateway, were what made a split expensive; left alone, that number grows with every feature.
