๐Ÿณ IntuitiveFE
Login
โ† All concepts

Edge Workers & Geo-Caching

โฑ๏ธ ~3-minute bite ยท solve the sandbox to master

0%lesson
๐Ÿง’

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

Total latency:500ms
User (Tokyo)Origin (Virginia)
User (Tokyo)โ†’250ms

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.

Origin (Virginia)โ†’250ms

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.

โš ๏ธ Gotcha: Even with a fast server, geographic distance creates a hard latency floor. A 250ms RTT user in Tokyo can never get a sub-250ms response from Virginia no matter how fast the server is.
๐Ÿ’ก Insight: This is why CDNs exist. The speed of light is not a bug โ€” it is a fundamental physical constraint. The only way to reduce it is to move compute closer to the user.
Visited:๐Ÿญโšก๐Ÿ”ง๐Ÿ”€๐Ÿ“Š
๐ŸŽฏ

Challenge

Visit all 5 edge computing patterns to understand the latency tradeoffs.

Try it
๐ŸŽฏ

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.

PropertyContainer (Lambda)V8 Isolate (CF Workers)
Cold start100msโ€“1s+<1ms
Memory128MBโ€“10GB128MB max
RuntimeFull Node.jsV8 (Web APIs only)
IsolationOS-levelV8 sandbox
DensityLow (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:

DNS lookup5msCached after first visit
TCP + TLS handshake25ms1 RTT to nearest PoP
Edge processing2msMiddleware, auth, routing
Cache lookup3msCDN cache hit
Transfer15msFirst packet back to client
Total50msWell within 100ms budget
๐ŸŽค

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.
1/4

Which Web API do edge functions use to parse and construct URLs?