Guides/Production patterns
Cloudflare Workers checklist
Per-request instances, database drivers, settings and request costs for an auth server on Cloudflare Workers.
The problem
A Worker does not let one request use a socket that another request opened, keeps no memory between requests, and does not put the visitor's address in X-Forwarded-For. An auth setup written for a long-running Node.js server answers the first request on a Worker and then fails, counts every visitor as one for rate limiting, or pays for a database connection on every page view.
The Cloudflare guide has the details and code for each item. This page is the order to go through them in.
When to use it
- The auth server runs in a Worker, alone or inside a framework such as TanStack Start, including an application that renders nothing on the server and uses its Worker only for the auth route.
Checklist
Choose how the instance meets the database
- A driver that queries over HTTP holds no socket, so one auth instance at module scope works.
- A driver that keeps a connection (
postgres, apgormysql2pool, also through Hyperdrive) needs one connection and one instance per request.perRequestfrom@entro314labs/entro-auth-cloudflareopens the connection, builds the instance on it, runs the work and closes it. See Database Connections.
perRequest is not tied to the CapyDB adapter. Kysely over postgres.js has run this way in a deployed Worker, with the client opened per request with max: 1 and prepare: false against a pooled endpoint over TLS.
For Cloudflare D1, the binding can be passed as database. The CLI cannot reach D1, so migrations run from an endpoint in the Worker; see the D1 example.
Give the CLI a configuration of its own
A per-request setup exports no auth instance for the schema generator to read. Export one from a separate file, built on a client that never connects, and pass it with --config. See The schema generator.
Read the visitor's address from Cloudflare
Add the cloudflare() plugin. It reads CF-Connecting-IP; without it, rate limiting puts every visitor in one bucket per path. Use it only where Cloudflare is in front of the server. See Client IP.
Get the settings into the Worker
Set ENTRO_AUTH_SECRET and the other secrets on the Worker. With version 2 of @cloudflare/vite-plugin and its cloudflare.config.ts, a value reaches process.env only when it is declared under env, and one that is missing is absent with no warning. That behavior was checked on release candidates of the plugin and the cf CLI only. See Environment Variables.
Keep page views off the database
- Turn on
session.cookieCache. - Set
disableSettingJwtHeader: trueon the JWT plugin unless you readset-auth-jwt. - Store the rate limit in the database or a secondary storage, set
rateLimit.enabled: true, and leave cheap, frequent paths out withcustomRules. - Pass
waitUntilasadvanced.backgroundTasks.handlerfor work that can finish after the response.
See What a request costs.
Pitfalls
- Setting a secret deploys. It creates a new version of the live Worker and routes traffic to it. Set several secrets in one call, and when rolling back, the version to go back to is the one the secret created, not the one before it.
- A deploy fails while a declared secret has no value. Set the values before the first deploy.
- Workers KV cannot be the secondary storage. It is eventually consistent, cannot expire a key sooner than 60 seconds, and has no atomic operations.
- The rate limit is on in production only unless
enabledis set. With database storage andenabledunset, a missingrateLimittable shows up only after the deploy. - The cost of a connection from the edge is not measured. A connection per request costs about seven round trips before the first query (TCP, TLS and the database login). How much that is from a Worker, with and without Hyperdrive, has not been measured.
- Bundle size. Measured on
1.8.0-rc.9with the Drizzle adapter (withdrizzle-ormitself left out of the count):@entro314labs/entro-auth/minimalwith no plugins was about 161 KB compressed; with email OTP, two-factor, organization and JWT about 200 KB; with the passkey plugin added about 299 KB, most of the increase from the passkey plugin's dependencies. The main entry in place of/minimaladds Kysely, about 186 KB minified.
Reference
- Cloudflare guide
- TanStack Start integration:
authHandlersfor a per-request instance - Rate Limit
- CapyDB adapter