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
| Platform | Timeout | Memory | Bundle size | Concurrency |
|---|---|---|---|---|
| AWS Lambda | 15 min | 128MB–10GB | 250MB unzipped | 1000 default (soft) |
| Vercel Functions | 300s (Pro) | 3GB | 250MB | Unlimited (managed) |
| Cloudflare Workers | 30s | 128MB | 1MB (compressed) | Unlimited |
| Netlify Functions | 26s | 1GB | 50MB | Managed |
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