Serverless Patterns
Build scalable serverless applications.
Core Patterns
Function Design
- Single responsibility per function
- Stateless (use external storage for state)
- Idempotent handlers (retry-safe)
- Receive event, process, return/forward
Cold Start Mitigation
| Strategy |
Effect |
Tradeoff |
| Provisioned Concurrency |
Zero cold starts |
Cost |
| Smaller runtime (Node/Python) |
Faster start |
Language choice |
| Keep-alive ping |
Reduce frequency |
Extra cost |
| Lambda SnapStart (Java) |
Fast restore |
State constraints |
| Lambda@Edge |
Regional distribution |
1MB response limit |
Event-Driven Design
Event Types
Events:
- UserRegistered { userId, email, timestamp }
- OrderPlaced { orderId, items, total }
- PaymentProcessed { orderId, amount, status }
- EmailSent { to, template, variables }
SQS + Lambda Pattern
API Gateway → Lambda (producer) → SQS → Lambda (consumer) → DynamoDB
Benefits: decoupling, buffering, retry, dead-letter queue.
API Gateway Patterns
- Request validation at gateway level
- Caching for GET endpoints (TTL per route)
- WAF for rate limiting and IP blocks
- Custom domain with ACM certificate
- Usage plans for API keys
Frameworks
- SAM (AWS) — YAML/YML, local testing
- Serverless Framework — Multi-cloud, plugins
- CDK (AWS) — Full programming languages
- Terraform — If already using for other infra
- Chalice (Python) — Flask-like, AWS only
Best Practices
- Function timeout: max 30s for APIs, 15min for background
- Memory: allocate more = more CPU (for compute-heavy, use 1.7GB+)
- Monitor: Duration, Invocations, Error count, Throttles
- Logs: structured JSON to CloudWatch
- Security: least-privilege IAM roles per function
1---2name: serverless-patterns3description: Lambda, cold starts, event-driven design, API Gateway, and serverless frameworks4---5
6# Serverless Patterns
7
8Build scalable serverless applications.
9
10## Core Patterns
11
12### Function Design
13- Single responsibility per function
14- Stateless (use external storage for state)
15- Idempotent handlers (retry-safe)
16- Receive event, process, return/forward
17
18### Cold Start Mitigation
19| Strategy | Effect | Tradeoff |
20|----------|--------|----------|
21| Provisioned Concurrency | Zero cold starts | Cost |
22| Smaller runtime (Node/Python) | Faster start | Language choice |
23| Keep-alive ping | Reduce frequency | Extra cost |
24| Lambda SnapStart (Java) | Fast restore | State constraints |
25| Lambda@Edge | Regional distribution | 1MB response limit |
26
27## Event-Driven Design
28
29### Event Types
30```yaml
31Events:
32 - UserRegistered { userId, email, timestamp }
33 - OrderPlaced { orderId, items, total }
34 - PaymentProcessed { orderId, amount, status }
35 - EmailSent { to, template, variables }
36```
37
38### SQS + Lambda Pattern
39```
40API Gateway → Lambda (producer) → SQS → Lambda (consumer) → DynamoDB
41```
42Benefits: decoupling, buffering, retry, dead-letter queue.
43
44## API Gateway Patterns
45- Request validation at gateway level
46- Caching for GET endpoints (TTL per route)
47- WAF for rate limiting and IP blocks
48- Custom domain with ACM certificate
49- Usage plans for API keys
50
51## Frameworks
52- **SAM** (AWS) — YAML/YML, local testing
53- **Serverless Framework** — Multi-cloud, plugins
54- **CDK** (AWS) — Full programming languages
55- **Terraform** — If already using for other infra
56- **Chalice** (Python) — Flask-like, AWS only
57
58## Best Practices
59- Function timeout: max 30s for APIs, 15min for background
60- Memory: allocate more = more CPU (for compute-heavy, use 1.7GB+)
61- Monitor: Duration, Invocations, Error count, Throttles
62- Logs: structured JSON to CloudWatch
63- Security: least-privilege IAM roles per function