Edge Computing Guide
Edge vs Origin
| Edge | Origin Server | |
|---|---|---|
| Latency | <50ms globally | 100–500ms |
| Memory | 128MB typical | GBs |
| Runtime | V8 isolates (JS/WASM) | Node/Go/Python/Java |
| Cold start | <5ms | 100ms–2s |
| Networking | Limited (no raw TCP) | Full |
| File system | None | ✓ |
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
| Product | Type | Best For |
|---|---|---|
| Cloudflare KV | Key-Value | Config, sessions, cached data |
| Cloudflare D1 | SQLite | Relational data at edge |
| Turso | LibSQL/SQLite | Geo-distributed SQLite |
| Upstash Redis | Redis | Rate limiting, pub/sub |
| Deno KV | Key-Value | Deno Deploy apps |
| PlanetScale | MySQL | Branching, global reads |