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)

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​

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​