Serverless Architecture Patterns Explained
A practical guide to common serverless patterns for building scalable, cost-effective applications.
What is Serverless Architecture?
Serverless architecture lets you build applications without managing servers. The cloud provider handles infrastructure, scaling, and maintenance. You pay only for actual compute time used.
Key AWS serverless services include:
- AWS Lambda — function execution
- API Gateway — HTTP endpoints
- DynamoDB — NoSQL database
- S3 — object storage
- SQS / SNS — messaging
- EventBridge — event routing
- Step Functions — workflow orchestration
Pattern 1: REST API Backend
The most common serverless pattern: an API that handles CRUD operations.
API Gateway -> Lambda -> DynamoDB
Components:
- API Gateway: Routes HTTP requests
- Lambda: Business logic
- DynamoDB: Data persistence
When to use
- Mobile and web application backends
- Microservices with variable traffic
- APIs with unpredictable load patterns
Infragram prompt
REST API with API Gateway routing to Lambda functions
for users, products, and orders. Each function connects
to its own DynamoDB table. Include Cognito for auth.
Pattern 2: Event-Driven Processing
React to events as they occur, processing data asynchronously.
S3 Upload -> Lambda -> Process -> DynamoDB
|
v
SNS Topic -> Multiple Subscribers
When to use
- File processing (images, documents, videos)
- Real-time data transformation
- IoT data ingestion
Infragram prompt
Image processing pipeline: S3 bucket triggers Lambda
on upload. Lambda resizes images, extracts metadata,
stores results in DynamoDB, and publishes to SNS
for downstream consumers.
Pattern 3: Fan-Out / Fan-In
Distribute work across multiple Lambda functions, then aggregate results.
Request -> Lambda (Coordinator)
|
+---------+---------+
v v v
Lambda Lambda Lambda
| | |
+---------+---------+
v
Aggregator -> Response
When to use
- Parallel data processing
- Map-reduce operations
- Batch processing with time constraints
Pro tip: Use Step Functions to orchestrate fan-out/fan-in patterns. It handles coordination, error handling, and retries automatically.
Pattern 4: Saga Pattern for Distributed Transactions
Manage complex business transactions across multiple services.
Step Functions orchestrates:
1. Reserve Inventory
2. Process Payment
3. Ship Order
4. Send Notification
If step fails -> Compensating transactions
When to use
- E-commerce order processing
- Multi-step business workflows
- Operations requiring rollback capability
Infragram prompt
Order processing saga with Step Functions.
Steps: validate order, reserve inventory, charge payment,
create shipment, send confirmation. Include error
handling with compensation for each step.
Pattern 5: CQRS (Command Query Responsibility Segregation)
Separate read and write operations for optimal performance.
Writes:
API Gateway -> Lambda -> DynamoDB
Reads:
API Gateway -> Lambda -> ElastiCache/OpenSearch
DynamoDB Streams -> Lambda -> Update Read Store
When to use
- Read-heavy applications
- Complex querying requirements
- When read and write patterns differ significantly
Pattern 6: Strangler Fig (Migration)
Gradually migrate from monolith to serverless.
API Gateway
|
+-> /new-feature -> Lambda (new)
|
+-> /legacy/* -> ALB -> EC2 (existing)
When to use
- Migrating legacy applications
- Reducing risk of big-bang rewrites
- Gradual modernization
Pattern 7: Real-Time Data Streaming
Process streaming data at scale.
Data Sources -> Kinesis Data Streams
|
+--------------+--------------+
v v v
Lambda Kinesis S3
(real-time) Firehose (archive)
| |
v v
DynamoDB Redshift
(hot data) (analytics)
When to use
- Log aggregation and analysis
- Clickstream analytics
- IoT data processing
- Real-time dashboards
Best Practices Across All Patterns
- Keep functions focused — single responsibility principle
- Use environment variables — configuration outside code
- Implement idempotency — handle duplicate events gracefully
- Add observability — CloudWatch, X-Ray for tracing
- Set appropriate timeouts — balance cost and reliability
- Use reserved concurrency — protect downstream services
Choosing the Right Pattern
| Requirement | Recommended Pattern |
|---|---|
| Simple CRUD API | REST API Backend |
| File processing | Event-Driven |
| Complex workflows | Saga with Step Functions |
| High read volume | CQRS |
| Real-time analytics | Streaming |