Skill — Serverless Patterns
When this skill activates
Any task involving serverless function design, cold start optimization,
function composition, event-driven architectures using FaaS platforms,
or cost modeling for serverless workloads.
Mandatory actions when this skill is active
Before writing any code
- Identify the trigger type (HTTP, queue, schedule, storage event, stream).
- Determine state requirements and where state will live (DynamoDB, Redis, S3).
- Estimate invocation volume and duration for cost projection.
- Decide composition pattern (Step Functions vs choreography vs direct invoke).
During implementation
- Keep functions focused (single responsibility).
- Externalize all state (no reliance on local filesystem or memory between invocations).
- Implement idempotency keys for retry-safe operations.
- Set appropriate timeout values (not max — just enough + buffer).
- Use structured logging with correlation IDs for distributed tracing.
- Checkpoint long-running work before timeout boundary.
After implementation
- Verify cold start latency meets SLA requirements.
- Confirm cost model aligns with budget (invocations × duration × memory).
- Test retry and failure scenarios end-to-end.
- Monitor concurrency limits and throttling metrics.
Cold Start Mitigation
Techniques
- Provisioned concurrency — Pre-warm N instances (cost trade-off).
- Bundle optimization — Smaller deployment = faster init.
- Avoid VPC — VPC attachment adds 5-10s cold start (only if DB needed).
- Lazy initialization — Defer heavy setup until first request needs it.
- Connection pooling — Use RDS Proxy or connection pool service.
- Language choice — Go/Rust cold start < Python < Java/Node.
When cold start matters
- Synchronous user-facing APIs (matters a lot).
- Async queue processors (usually doesn't matter).
- Scheduled jobs (doesn't matter at all).
Composition Patterns
Step Functions (Orchestration)
- Central coordinator manages workflow state.
- Built-in retry, catch, timeout per step.
- Visual debugging of execution history.
- Best for: complex workflows, human approval steps, long-running processes.
Choreography (Event-Driven)
- Each function emits events, others react.
- No single point of failure.
- Harder to debug end-to-end.
- Best for: loosely coupled, independent scaling per step.
Fan-Out / Fan-In
- Dispatch N parallel tasks → aggregate results.
- Use SQS/SNS for fan-out, DynamoDB for aggregation.
- Handle partial failures gracefully.
State Management
| State Type |
Solution |
Use When |
| Session state |
DynamoDB / Redis |
Auth tokens, cart |
| Workflow state |
Step Functions |
Multi-step processes |
| Cache |
ElastiCache / DAX |
Repeated reads |
| File state |
S3 |
Large objects |
| Event state |
Event carried |
Pass between functions |
Cost Model
Monthly cost = (invocations × $0.20/1M) + (GB-seconds × $0.0000166667)
Cost comparison triggers
- If >1M invocations/hour sustained → consider containers.
- If function runs >15min → containers or batch.
- If always-on with predictable load → containers cheaper.
- If spiky/unpredictable → serverless wins on cost.
Trigger Patterns
| Trigger |
Pattern |
Key Concern |
| HTTP (API Gateway) |
Request/response |
Cold start latency |
| SQS |
Queue consumer |
Batch size, visibility timeout |
| Schedule (cron) |
Periodic job |
Idempotency on overlap |
| S3 event |
File processor |
Duplicate events possible |
| DynamoDB stream |
Change capture |
Ordering guarantees |
| Kinesis |
Stream processor |
Shard iterator, checkpointing |
Timeout Strategy
- Set timeout = expected p99 duration + 20% buffer.
- Checkpoint work before 80% of timeout.
- Implement dead-letter queues for timed-out invocations.
- Never set timeout to maximum "just in case."
Self-check
1---2name: serverless-patterns3description: Skill — Serverless Patterns4---56# Skill — Serverless Patterns78## When this skill activates9Any task involving serverless function design, cold start optimization,10function composition, event-driven architectures using FaaS platforms,11or cost modeling for serverless workloads.1213## Mandatory actions when this skill is active1415### Before writing any code161. Identify the trigger type (HTTP, queue, schedule, storage event, stream).172. Determine state requirements and where state will live (DynamoDB, Redis, S3).183. Estimate invocation volume and duration for cost projection.194. Decide composition pattern (Step Functions vs choreography vs direct invoke).2021### During implementation22- Keep functions focused (single responsibility).23- Externalize all state (no reliance on local filesystem or memory between invocations).24- Implement idempotency keys for retry-safe operations.25- Set appropriate timeout values (not max — just enough + buffer).26- Use structured logging with correlation IDs for distributed tracing.27- Checkpoint long-running work before timeout boundary.2829### After implementation30- Verify cold start latency meets SLA requirements.31- Confirm cost model aligns with budget (invocations × duration × memory).32- Test retry and failure scenarios end-to-end.33- Monitor concurrency limits and throttling metrics.3435## Cold Start Mitigation3637### Techniques381. **Provisioned concurrency** — Pre-warm N instances (cost trade-off).392. **Bundle optimization** — Smaller deployment = faster init.403. **Avoid VPC** — VPC attachment adds 5-10s cold start (only if DB needed).414. **Lazy initialization** — Defer heavy setup until first request needs it.425. **Connection pooling** — Use RDS Proxy or connection pool service.436. **Language choice** — Go/Rust cold start < Python < Java/Node.4445### When cold start matters46- Synchronous user-facing APIs (matters a lot).47- Async queue processors (usually doesn't matter).48- Scheduled jobs (doesn't matter at all).4950## Composition Patterns5152### Step Functions (Orchestration)53- Central coordinator manages workflow state.54- Built-in retry, catch, timeout per step.55- Visual debugging of execution history.56- Best for: complex workflows, human approval steps, long-running processes.5758### Choreography (Event-Driven)59- Each function emits events, others react.60- No single point of failure.61- Harder to debug end-to-end.62- Best for: loosely coupled, independent scaling per step.6364### Fan-Out / Fan-In65- Dispatch N parallel tasks → aggregate results.66- Use SQS/SNS for fan-out, DynamoDB for aggregation.67- Handle partial failures gracefully.6869## State Management7071| State Type | Solution | Use When |72|-----------|----------|----------|73| Session state | DynamoDB / Redis | Auth tokens, cart |74| Workflow state | Step Functions | Multi-step processes |75| Cache | ElastiCache / DAX | Repeated reads |76| File state | S3 | Large objects |77| Event state | Event carried | Pass between functions |7879## Cost Model8081```82Monthly cost = (invocations × $0.20/1M) + (GB-seconds × $0.0000166667)83```8485### Cost comparison triggers86- If >1M invocations/hour sustained → consider containers.87- If function runs >15min → containers or batch.88- If always-on with predictable load → containers cheaper.89- If spiky/unpredictable → serverless wins on cost.9091## Trigger Patterns9293| Trigger | Pattern | Key Concern |94|---------|---------|-------------|95| HTTP (API Gateway) | Request/response | Cold start latency |96| SQS | Queue consumer | Batch size, visibility timeout |97| Schedule (cron) | Periodic job | Idempotency on overlap |98| S3 event | File processor | Duplicate events possible |99| DynamoDB stream | Change capture | Ordering guarantees |100| Kinesis | Stream processor | Shard iterator, checkpointing |101102## Timeout Strategy103- Set timeout = expected p99 duration + 20% buffer.104- Checkpoint work before 80% of timeout.105- Implement dead-letter queues for timed-out invocations.106- Never set timeout to maximum "just in case."107108## Self-check109- [ ] Function is idempotent (safe to retry).110- [ ] State externalized (no local filesystem reliance).111- [ ] Timeout set appropriately (not max).112- [ ] Cold start measured and within SLA.113- [ ] Cost model validated against expected traffic.114- [ ] Dead-letter queue configured for failures.115- [ ] Correlation IDs propagated for tracing.