Chapter 9 — Serverless & Edge

Serverless means you write functions, not servers. The cloud provider handles all infrastructure — you pay only when your code runs.

How Serverless Works

Traditional Server: Serverless: ┌─────────────────────┐ ┌─────────────────────┐ │ Server running 24/7 │ │ No server running │ │ (paying even idle) │ │ (paying $0 idle) │ │ │ │ │ │ Request → Process │ │ Request → Cold Start│ │ Request → Process │ │ → Process │ │ ...idle... │ │ Request → Process │ │ ...idle... │ │ (warm) │ │ ...idle... │ │ ...nothing... │ │ Request → Process │ │ Request → Cold Start│ └─────────────────────┘ └─────────────────────┘ Cost: $$$$ (always on) Cost: $ (per invocation)

AWS Lambda Example

// handler.js — AWS Lambda function
exports.handler = async (event) => {
    const body = JSON.parse(event.body || '{}');

    // Your business logic here
    const result = await processRequest(body);

    return {
        statusCode: 200,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(result)
    };
};

// Deploy with AWS SAM or Serverless Framework:
# serverless.yml
service: myapi
provider:
  name: aws
  runtime: nodejs20.x
  region: eu-west-1
functions:
  api:
    handler: handler.handler
    events:
      - httpApi:
          path: /api/{proxy+}
          method: ANY
    memorySize: 256
    timeout: 10

Cloudflare Workers (Edge Computing)

// worker.js — runs at 300+ edge locations worldwide
export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    if (url.pathname === '/api/hello') {
      return new Response(JSON.stringify({ message: 'Hello from the edge!' }), {
        headers: { 'Content-Type': 'application/json' }
      });
    }

    // Proxy to origin for other routes
    return fetch(request);
  }
};

Serverless Comparison

PlatformCold StartMax DurationFree TierBest For
AWS Lambda100-500ms15 min1M requests/moFull backend APIs, event processing
Cloudflare Workers~0ms (no cold start)30s (free), 15min (paid)100K requests/dayEdge logic, fast APIs
Vercel Functions~250ms10-60s100GB-hrs/moNext.js apps, frontend teams
Netlify Functions~200ms10-26s125K requests/moJAMstack backends
Google Cloud Functions100-400ms9-60 min2M invocations/moGCP ecosystem, event-driven

When Serverless Fits

Use serverless when:
• Traffic is sporadic/unpredictable (pay-per-use saves money)
• Individual requests are short-lived (<30s)
• You want zero infrastructure management
• Event-driven workloads (file uploads, webhooks, scheduled tasks)
• API endpoints with variable traffic

Avoid serverless when:
• Consistent high traffic (a server is cheaper)
• Long-running processes (video encoding, ML training)
• WebSocket connections needed
• You need local filesystem or persistent state
• Cold starts are unacceptable (real-time systems)

Edge Computing

Edge computing runs your code at CDN points-of-presence (PoPs) close to users, reducing latency from ~100ms to ~10ms.

The hybrid pattern: Use edge functions for authentication, A/B testing, geolocation routing, and caching logic. Keep heavy business logic in a traditional server or Lambda. This gives you the best of both worlds — fast edge responses with powerful backend processing.