Skip to main content

Edge-First Architecture

Learning Focus

By the end of this lesson you will understand the "Edge-First" mental model, how to choose the right storage for your application, and how to design for global distribution.

The Edge-First Mental Model

In traditional architecture, your logic and database live in a single region (e.g., us-east-1). In Edge-First Architecture, your code and data live everywhere.

FeatureTraditional AppEdge-First App
DeploymentSingle region (US, EU, or Asia)Global (330+ cities)
Latency200ms+ (global avg)<20ms (global avg)
ScalabilityVertical/Horizontal scaling serversAutomatic (serverless)
DataCentral SQL databaseDistributed (D1, KV, R2)
flowchart TD
subgraph Traditional["Traditional (Single Region)"]
T_USER["User (Global)"] --> T_LB["Load Balancer"]
T_LB --> T_SRV["App Server (US)"]
T_SRV --> T_DB["Database (US)"]
end

subgraph Edge["Edge-First (Global)"]
E_USER["User (Global)"] --> E_W["Worker (Local Edge)"]
E_W --> E_DB["D1 / KV / R2 (Edge Storage)"]
end

style T_SRV fill:#dc2626,color:#fff,stroke:#b91c1c
style E_W fill:#16a34a,color:#fff,stroke:#15803d

Choosing the Right Storage

Designing for the edge means choosing the right storage for your data's consistency and access patterns.

NeedStorage ServiceBest For
Relational DataD1 DatabaseBlogs, CMS, User metadata, SQL apps
Simple Key-ValueWorkers KVFeature flags, configuration, long-lived sessions
Large FilesR2 StorageImages, videos, backups, logs
Strict ConsistencyDurable ObjectsReal-time chat, counters, collaboration
Asynchronous WorkQueuesLog processing, email sending, retries

Example Architecture: SaaS Blog Platform

flowchart TD
USER["Reader"] --> PAGES["Cloudflare Pages\n(Frontend)"]
PAGES --> FUNCTIONS["Pages Functions\n(API logic)"]
FUNCTIONS --> D1["D1 Database\n(Posts, Comments)"]
FUNCTIONS --> R2["R2 Storage\n(Uploaded Images)"]
FUNCTIONS --> KV["KV Store\n(Settings, Banned IPs)"]

ADMIN["Admin"] --> FUNCTIONS

style PAGES fill:#2563eb,color:#fff,stroke:#1e40af
style D1 fill:#7c3aed,color:#fff,stroke:#6d28d9

Anti-Patterns to Avoid

  • Backhauling Data: Don't put your Worker on the edge but connect to a database in a single region (like RDS in AWS us-east-1). This defeats the performance gains of the edge. Use D1 or KV instead.
  • Large State in Memory: Workers are ephemeral. Don't store important data in local variables; it will disappear when the isolate is reclaimed. Use Durable Objects for state.
  • Heavy CPU Work: Workers are CPU-limited (10ms free). Don't perform heavy video transcoding or complex PDF generation in a Worker. Offload to a background service via Queues.

Key Takeaways

  • Edge-First means deploying logic and data globally by default.
  • Minimize latency by keeping data close to workers.
  • Match the storage service (D1, KV, R2) to your data's consistency requirements.
  • Avoid backhauling to centralized databases — use Cloudflare's serverless storage.
  • Design for ephemeral execution — use persistent storage for any state.

What's Next