Skip to content

Edge computing moves processing closer to users, reducing latency and improving performance. Let’s explore modern edge platforms and use cases.

What is Edge Computing?

Code runs in data centers geographically close to users:

  • Lower latency (< 50ms)
  • Reduced bandwidth costs
  • Better user experience
  • Geographic compliance

Edge Platforms

Cloudflare Workers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const geo = request.cf.country
  
  return new Response(`Hello from ${geo}!`, {
    headers: { 'content-type': 'text/plain' },
  })
}

Vercel Edge Functions

1
2
3
4
5
6
7
8
export const config = {
  runtime: 'edge',
}

export default async function handler(req: Request) {
  const { geo } = req
  return new Response(`Your location: ${geo.city}, ${geo.country}`)
}

AWS Lambda@Edge

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
exports.handler = async (event) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    
    // Modify request based on location
    if (headers['cloudfront-viewer-country'][0].value === 'US') {
        request.uri = '/us' + request.uri;
    }
    
    return request;
};

Use Cases

Personalization

1
2
3
4
5
6
// Serve content based on location
export default async function handler(req) {
  const country = req.geo.country
  const content = await getLocalizedContent(country)
  return new Response(content)
}

A/B Testing

1
2
3
// Edge-based A/B testing
const variant = Math.random() < 0.5 ? 'A' : 'B'
response.headers.set('X-Variant', variant)

Authentication

1
2
3
4
5
6
7
// Verify JWT at edge
import { verify } from '@tsndr/cloudflare-worker-jwt'

const isValid = await verify(token, SECRET)
if (!isValid) {
  return new Response('Unauthorized', { status: 401 })
}

Image Optimization

1
2
3
4
5
6
// Resize images on-the-fly
const width = url.searchParams.get('w')
const optimized = await resizeImage(imageUrl, width)
return new Response(optimized, {
  headers: { 'content-type': 'image/webp' }
})

Performance Benefits

Traditional Architecture

User → CDN → Origin Server (500ms)

Edge Architecture

User → Edge (20ms)

Edge Databases

Cloudflare D1

1
2
3
const result = await env.DB.prepare(
  'SELECT * FROM users WHERE id = ?'
).bind(userId).first()

Vercel Edge Config

1
2
3
import { get } from '@vercel/edge-config'

const featureFlags = await get('flags')

Best Practices

  1. Keep edge functions small
  2. Cache aggressively
  3. Monitor cold starts
  4. Handle errors gracefully
  5. Use edge-compatible libraries
  6. Test across regions

Limitations

  • CPU/memory constraints
  • Limited runtime
  • No filesystem access
  • Cold starts possible
  • Vendor lock-in

Conclusion

Edge computing significantly improves user experience by reducing latency. Use it for personalization, security, and optimization at the network edge.