Edge Computing Guide

Edge vs Origin

EdgeOrigin Server
Latency<50ms globally100โ€“500ms
Memory128MB typicalGBs
RuntimeV8 isolates (JS/WASM)Node/Go/Python/Java
Cold start<5ms100msโ€“2s
NetworkingLimited (no raw TCP)Full
File systemNoneโœ“

Cloudflare Workers Example

// Cloudflare Workers (runs in 200+ PoPs globally)
export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url)

    // A/B testing at the edge
    if (url.pathname === '/') {
      const variant = Math.random() < 0.5 ? 'A' : 'B'
      const response = await fetch(`https://origin.example.com/?variant=${variant}`)
      return new Response(response.body, {
        headers: {
          ...Object.fromEntries(response.headers),
          'X-AB-Variant': variant,
        }
      })
    }

    // KV store for caching
    const cached = await env.MY_KV.get(url.pathname)
    if (cached) return new Response(cached, { headers: { 'Content-Type': 'application/json' } })

    const data = await fetch('https://api.example.com' + url.pathname).then(r => r.text())
    await env.MY_KV.put(url.pathname, data, { expirationTtl: 3600 })
    return new Response(data)
  }
}

Edge Database Options

ProductTypeBest For
Cloudflare KVKey-ValueConfig, sessions, cached data
Cloudflare D1SQLiteRelational data at edge
TursoLibSQL/SQLiteGeo-distributed SQLite
Upstash RedisRedisRate limiting, pub/sub
Deno KVKey-ValueDeno Deploy apps
PlanetScaleMySQLBranching, global reads