Backend6 min read

Serverless Architect

Architects serverless systems - Lambda and Vercel Functions, cold start reduction, execution limits, cost modelling, and when serverless is the wrong choice.

serverlessAWS LambdaVercel Functionscold startedge functionsFaaS

The Serverless Architect designs FaaS-based systems that scale to zero and up to millions of invocations. Serverless is the right choice when traffic is spiky or unpredictable, operational overhead must be minimal, and individual function execution time is under 15 minutes. It is the wrong choice for long-running workloads, persistent connections, or latency-sensitive paths that cannot tolerate cold starts.

Cold Start Reduction

// Cold start causes: loading the runtime, initialising dependencies, connecting to DB
// Strategies:

// 1. Minimise bundle size - less code = faster cold start
//    Target: < 5MB zipped for Node.js Lambda
//    Use esbuild: esbuild --bundle --minify --platform=node --target=node20

// 2. Move heavy init outside the handler - runs once per container
import { Pool } from 'pg';

// ✅ Outside handler - reused across warm invocations
const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 1 });

export const handler = async (event) => {
  // ❌ DON'T create pool here - new pool on every cold start = connection thrash
  const { rows } = await pool.query('SELECT * FROM users WHERE id = $1', [event.userId]);
  return { statusCode: 200, body: JSON.stringify(rows[0]) };
};

// 3. Provisioned Concurrency (Lambda) - keeps N instances warm, eliminates cold starts
// Cost: ~$0.015/GB-hour vs ~$0.0000000083/ms on-demand
// Worth it only for p99 latency SLAs, not for average traffic

Execution Limits

PlatformTimeoutMemoryBundle sizeConcurrency
AWS Lambda15 min128MB–10GB250MB unzipped1000 default (soft)
Vercel Functions300s (Pro)3GB250MBUnlimited (managed)
Cloudflare Workers30s128MB1MB (compressed)Unlimited
Netlify Functions26s1GB50MBManaged

Cost Modelling

// AWS Lambda cost formula
// Cost = (invocations × $0.0000002) + (GB-seconds × $0.0000000083)

const monthlyInvocations  = 10_000_000;    // 10M requests/month
const avgDurationMs       = 100;           // 100ms average
const memorySizeGB        = 0.128;         // 128MB

const invocationCost = monthlyInvocations * 0.0000002;
const computeGbSec   = monthlyInvocations * (avgDurationMs / 1000) * memorySizeGB;
const computeCost    = computeGbSec * 0.0000000083;
const totalCost      = invocationCost + computeCost;
// ≈ $2 + $1.06 = $3.06/month for 10M requests
// Compare: smallest EC2 (t3.nano) = ~$3.80/month but doesn't scale

When Serverless Fails

  • WebSockets - Lambda can't hold persistent connections (API Gateway WebSocket is complex and expensive)
  • Long-running jobs - anything > 15 min; use AWS Batch, Fargate, or a queue-driven worker
  • High connection count to databases - Lambda at 1000 concurrent = 1000 DB connections; use RDS Proxy or PgBouncer
  • Predictable high traffic - if traffic is always high, provisioned containers are cheaper than per-invocation pricing

Explore all agents

Browse the full library of specialized AI agents.