Edge Workers Reward Strict Boundaries
When building Vibemotion, a mobile-first PWA for multi-account content production, running on Cloudflare Workers meant near-zero idle hosting cost and instant global responses. However, serverless isolates enforce tight CPU and memory ceilings: buffering multi-megabyte voice-over audio or video previews inside Worker JSON payloads quickly hurts reliability.
Instead of reaching for a traditional stateful VPS too early, we split storage responsibilities cleanly across Cloudflare primitives.
5-layer serverless routing with direct R2 binary offload
Keeping multi-megabyte voice-overs out of Worker RAM and SQLite.
Three Edge Storage Roles
- Cloudflare KV (
SESSION) + Better Auth: stores session tokens at the edge for instant authenticated workspace switches across creator accounts. - Cloudflare D1 (
planner-db) + Drizzle ORM: tracks structured metadata only: account brand rules, AI Prompt presets, production statuses (PRODUCTION_STATUSES), andaudio_assetR2 object keys. - Cloudflare R2 Object Storage (
planner-audio): receives uploads via presigned S3 PUT (aws4fetch) when configured, or falls back to zero-buffer Worker stream forwarding (PUT /api/audio/$contentIdcheckingMAX_AUDIO_BYTES = 50MB&audio/*) plus HTTP RangeGETplayback.
// Verified PRODUCTION_STATUSES & R2 audio validation (src/db/schema.ts & audio.fn.ts)
import { z } from "zod";
export const PRODUCTION_STATUSES = [
"not_started",
"ready",
"in_progress",
"review",
"ready_to_publish",
"scheduled",
"published",
"needs_revision",
] as const;
export const AudioUploadMetaSchema = z.object({
contentId: z.string().min(1),
filename: z.string().trim().min(1).max(200),
size: z.number().int().positive().max(50 * 1024 * 1024),
mime: z.string().regex(/^audio\//),
});Validating at the Server Function Boundary
With TanStack Start, every edge server function validates inputs with Zod before touching Drizzle queries or presigned R2 operations. That prevents malformed mobile requests from leaking inconsistent state between production stages and attached media files.
Key Takeaway
Edge serverless works effortlessly for full-stack SaaS apps once you treat SQLite (D1) strictly as a metadata catalog and delegate heavy media payloads directly to object storage (R2).