ENGINEERINGAugust 15, 20267 MIN READBY STAINLESS SYSTEMS GROUP
Architecting Low-Latency Global Edge Portals with Cloudflare Workers, Better-Auth, and D1 SQLite
How we architected a global client portal with sub-25ms TTFB using Cloudflare Workers edge compute, Better-Auth session tokens, and D1 SQLite replication.
An edge-first client portal architecture runs authentication, session validation, database querying, and static asset delivery across hundreds of distributed data centers worldwide instead of routing every request to a centralized origin server. By pairing Cloudflare Workers V8 isolates with D1 SQLite read replication and lightweight authentication protocols like Better-Auth, global users experience Time to First Byte (TTFB) latencies under 25 milliseconds regardless of geographic location.
Traditional Centralized Origin (Virginia us-east-1):
[User in Frankfurt / Tokyo / Sydney] ββ(220ms RTT)βββΊ [Origin Server] βββΊ [PostgreSQL]
Edge-First Cloudflare Workers + D1 Architecture:
[User in Tokyo] ββ(12ms RTT)βββΊ [Cloudflare Tokyo PoP]
When building high-traffic client portals for venture-backed B2B startups, international latency is often the silent killer of user experience. If a founder in London or Singapore logs into their dashboard and waits 800 milliseconds for every milestone tab switch, the product feels sluggish.
Here is the exact architecture we deployed for the Stainless Studio client portal to achieve sub-25ms global response times with zero maintenance overhead.
The Stack: Why Cloudflare Workers, D1, and Better-Auth?
Three specific engineering requirements dictated our stack choices:
Zero Cold Starts: Traditional serverless lambdas on AWS or Google Cloud suffer from 300ms to 1.5s cold starts when spinning up container environments. Cloudflare Workers run on V8 isolates, starting execution in under 5 milliseconds.
Embedded SQL at the Edge: Cloudflare D1 provides serverless SQLite databases with automatic read replication across global points of presence (PoPs).
Framework-Agnostic Authentication: Better-Auth runs directly inside edge worker runtimes without requiring heavy Node.js standard library polyfills.
Global Latency Benchmarks (TTFB for Authenticated Dashboard Query)
Request Origin
Centralized AWS us-east-1 (Node.js + RDS)
Cloudflare Edge (Workers + D1 SQLite)
Speedup Factor
New York, USA
45ms
18ms
2.5x faster
Frankfurt, Germany
165ms
21ms
7.8x faster
Tokyo, Japan
210ms
24ms
8.7x faster
Sydney, Australia
265ms
28ms
9.4x faster
SΓ£o Paulo, Brazil
195ms
23ms
8.4x faster
Configuring Better-Auth on Cloudflare Workers
Better-Auth provides clean TypeScript type safety and built-in SQLite support. Here is our edge worker entry point handling session verification and route dispatching:
Because SQLite runs at the edge, transactions that require multiple roundtrips to an external database are eliminated. However, you must write idempotent migrations and avoid unbounded table scans.
We keep our D1 migrations versioned in SQL files and apply them through wrangler d1 migrations apply:
-- migrations/0001_init_portal_schema.sql
CREATETABLEIFNOTEXISTS studio_milestones (
id TEXTPRIMARY KEY,
team_id TEXTNOT NULL,
title TEXTNOT NULL,
statusTEXTCHECK(statusIN ('backlog', 'in_progress', 'ready_for_review', 'deployed')) NOT NULLDEFAULT'backlog',
pr_url TEXT,
preview_url TEXT,
allocated_hours REALNOT NULLDEFAULT0.0,
created_at TEXTNOT NULLDEFAULT (datetime('now')),
updated_at TEXTNOT NULLDEFAULT (datetime('now'))
);
CREATEINDEXIFNOTEXISTS idx_milestones_team ON studio_milestones(team_id);
Three Hard Rules for Edge Backend Development
Avoid Heavy Node Polyfills: Stick to Web Standard APIs (fetch, Request, Response, Web Crypto, ReadableStream). If a package relies on fs or native C++ addons, it will fail in V8 isolate environments.
Cache Session Verification in Cookies: Validating session tokens against the database on every micro-request adds unnecessary CPU time. Enable cryptographic cookie caching with a 5-minute TTL.
Use Prepared Statements for SQL: Always parameterize your SQL queries with Kysely or D1βs native prepared statements to benefit from SQLiteβs compiled query cache.