MidnightTokensdeveloper portal
Sign In
Unit Study Document

Edge Functions & CDN Caching

6 min readβ€’Visual explainer included
Global Latency VisualizerCompare models
Central Server (Virginia)
200ms LatencyCross-ocean roundtrips
VS
Edge Nodes (Global)
12ms LatencyCalculated at closest node

Computing at the Edge

Traditional cloud architectures deploy servers in a central region (e.g., US-East-1 in Virginia). If a user in Tokyo requests a dynamic page, the request travels thousands of miles across undersea cables, introducing 150ms+ of physical network latency. Edge Computing resolves this by running code directly on CDN edge nodes located globally.

Edge vs Serverless: Regular serverless functions (like AWS Lambda) run in a single regional datacenter and suffer from heavy cold-start delays. Edge functions use lightweight V8 isolate engines (similar to browser tabs) that boot in under 5 milliseconds globally.

Writing an Edge Middleware Function

Modern platforms like Vercel and Cloudflare use Edge Middleware to inspect and modify requests before they reach the main origin server:

// Vercel Edge Middleware Example
import { NextResponse } from 'next/server';

export const config = {
 matcher: '/api/v1/:path*',
};

export function middleware(request) {
 const geo = request.geo || {};
 const country = geo.country || 'US';
 
 // Geolocation-based routing directly at the edge node
 const response = NextResponse.next();
 response.headers.set('x-user-country', country);
 
 return response;
}

Edge Data Access Strategies

Because code runs globally, queries to a central database will re-introduce latency. To scale data at the edge, engineers use:

  • Edge Caching: Storing read-heavy APIs in regional CDN memory with stale-while-revalidate headers.
  • Distributed Key-Value Stores: Storing sessions and config parameters in globally replicated databases (like Cloudflare KV or Upstash Redis).
  • Serverless DB Proxies: Using WebSocket pooling proxies (like Prisma Accelerate or Neon serverless driver) to keep connections open.
Fast Drill

Active Recalls

Card 1 of 1
Question

What is Edge Computing?

Tap card to flip
Answer

Running computation at the edge of the network, closest to where the users are.

Mastery: 0%
Knowledge Check

Quiz Practice

Question 1 of 1
What is the primary advantage of Edge functions?

Chapter Scratchpad

Auto-saves immediately

Active Recall Cards

Review core concepts before doing the quiz

Fast Drill

Active Recalls

Card 1 of 1
Question

What is Edge Computing?

Tap card to flip
Answer

Running computation at the edge of the network, closest to where the users are.

Mastery: 0%

AI Study Buddy

Always online

Hi! I'm Spooky, your study buddy! Let's learn together.