Edge Workers & Geo-Caching
โฑ๏ธ ~3-minute bite ยท solve the sandbox to master
5-Year-Old Metaphor
โ The physical, real-world picture. No jargon.๐ช Edge computing = local stores stocked from a distant warehouse.
The retail analogy
Your origin server is a warehouse in one city. Everything is there, but getting to it from far away takes time (latency = distance).
CDN PoPs are local stores near customers. Pre-stocked with popular items (cached assets). Customer buys locally in 5 minutes instead of ordering from the warehouse (500ms).
Edge Functions are local employees authorized to make decisions without calling HQ. They can check your membership card (JWT), give you a store-specific discount (A/B test), and redirect you to the right aisle (geo-routing) โ all without a phone call to the warehouse.
CDN = Pre-stocked local store
- โข Cache static assets near users
- โข Cache hit: 20โ40ms
- โข Cache miss: back to origin
- โข Cache ratio is the key metric
Edge Function = Local authority
- โข V8 isolate, zero cold start
- โข Run logic before cache check
- โข JWT auth, A/B, geo-routing
- โข No Node.js APIs
Interactive Sandbox
โ Move something, see it react instantly.Pattern
Origin (Virginia)
User in Tokyo requests a page. Packet must travel ~11,000km to the origin server in Virginia. Physical speed-of-light limit: ~250ms one-way RTT.
User (Tokyo)
Origin generates the response (DB query, template render) and sends it back. Another 250ms travel time. Total: 500ms minimum just for the round trip โ before any computation.
Challenge
Visit all 5 edge computing patterns to understand the latency tradeoffs.
Why Should I Care?
โ The exact interview question + the bug it kills.Interview questions
Q: What runtime limitations exist for edge functions?
Edge functions run in V8 isolates, not full Node.js environments. You have access to Web APIs: fetch, URL, crypto (Web Crypto API), TextEncoder, Request/Response. You do NOT have: fs, path, Buffer, or any native Node.js module. No Prisma, no Sequelize, no bcrypt (uses native). RAM limits (128MB Cloudflare, 4MB Vercel script size), CPU time limits (Cloudflare: 10ms CPU per request).
Q: When does edge computing not help (or hurt)?
Edge doesn't help when your bottleneck is the database โ which still sits in one region. An edge function that queries a Postgres DB in Virginia adds edge latency (20ms) on top of DB latency (250ms RTT + query time). You've made it worse. Edge helps for: logic that doesn't need the DB (auth token verification, redirects, static content decisions), or when you have edge-native data stores (CF KV, Durable Objects).
Q: What is the difference between an edge function and a CDN?
A CDN is a distributed cache โ it stores and serves pre-computed responses. No code runs per-request (or minimal cache-lookup logic). An edge function runs custom code per-request at a PoP. You can combine them: the edge function runs first (auth check, A/B routing), then the result may be served from CDN cache. Cloudflare Workers can both run code AND access the CDN cache via the caches API.
The Deep Dive
โ Spec refs, engine internals, the minutiae.V8 isolates vs containers
Traditional serverless (Lambda, Cloud Functions) spins up a container per invocation. A container takes 100msโ1s to start from cold. V8 isolates share a single V8 process between thousands of tenants, each sandboxed by V8's security model. Isolates start in <1ms. This is why Cloudflare Workers can have essentially zero cold start โ no container to provision, no OS to boot.
| Property | Container (Lambda) | V8 Isolate (CF Workers) |
|---|---|---|
| Cold start | 100msโ1s+ | <1ms |
| Memory | 128MBโ10GB | 128MB max |
| Runtime | Full Node.js | V8 (Web APIs only) |
| Isolation | OS-level | V8 sandbox |
| Density | Low (1 per container) | High (thousands per process) |
Cloudflare KV and Durable Objects
CF KV is an eventually consistent global key-value store. Writes propagate to all PoPs in ~60 seconds. Perfect for: feature flags, configuration, user preferences. Not for: counters, locks, real-time state.
Durable Objectsare strongly consistent, single-instance actors. Each DO lives in exactly one PoP. Uses: presence systems (who's online), collaborative editing (single source of truth), rate limiters (exact request counts).
Latency budget analysis
A 100ms target for time-to-first-byte (TTFB) breaks down roughly as:
Interview Questions
โ Real questions from real interviews โ with answers.Edge functions run in V8 isolates with Web APIs only โ no Node.js built-ins, limited RAM and CPU time.
Isolates share a single V8 process and start in microseconds; containers require OS boot and runtime init.
When the bottleneck is the database โ edge moves compute but not data, adding latency on top of the DB round trip.
KV is eventually consistent and globally replicated; Durable Objects are strongly consistent single-instance actors.
Middleware runs at the edge before the cache and before any page renders; API routes run in the Node.js runtime.
Memory Game
โ Quick quiz โ lock the concept in long-term memory.Which Web API do edge functions use to parse and construct URLs?