Xoxoftware - XOXO Creative Studio | Web & Mobile App Development | Fred Cheung | Hong Kong
Concepts

Serverless Patterns

Event-driven architecture — common serverless design patterns using Lambda, API Gateway, Step Functions, EventBridge, SQS, and DynamoDB.

Overview

Serverless patterns are architecture blueprints that combine fully managed, event-driven services — Lambda, API Gateway, Step Functions, EventBridge, SQS, SNS, and DynamoDB — to build applications without provisioning or managing servers.


Core Serverless Services

ServiceRole in Serverless Architecture
LambdaCompute — runs code in response to events
API GatewayHTTP entry point — REST, HTTP, and WebSocket APIs
Step FunctionsOrchestration — coordinate multi-step workflows
EventBridgeEvent routing — content-based filtering and fan-out
SQSBuffering — decouple producers and consumers
SNSNotification — fan-out to multiple subscribers
DynamoDBDatabase — single-digit millisecond NoSQL storage
S3Storage — object store and event source
CognitoAuthentication — user pools and identity federation
AppSyncGraphQL API — managed real-time and offline data sync

Pattern 1: Synchronous API

Client → API Gateway → Lambda → DynamoDB
                ↑                    │
                └────── Response ────┘
  • Simplest serverless pattern for CRUD operations
  • API Gateway handles auth (Cognito / Lambda authorizer), throttling, and caching
  • Lambda processes the request and interacts with DynamoDB
  • Response returns synchronously to the client

Pattern 2: Asynchronous Processing

Client → API Gateway → SQS Queue → Lambda (consumer) → DynamoDB

              └── 202 Accepted (immediate)
  • API Gateway sends message to SQS and returns immediately
  • Lambda polls SQS and processes messages at its own pace
  • Decouples request acceptance from processing — handles traffic spikes
  • DLQ captures failed messages for retry or investigation

Pattern 3: Fan-Out

Event Source (S3, API Gateway)
    → SNS Topic
        ├── SQS → Lambda (image resize)
        ├── SQS → Lambda (metadata extract)
        └── Lambda (notification)
  • Single event triggers multiple independent processing paths
  • Each SQS queue provides buffering and independent failure handling
  • SNS filter policies route specific events to specific subscribers

Pattern 4: Event-Driven Pipeline

S3 (object uploaded)
    → EventBridge Rule
        → Step Functions
            ├── Lambda (validate)
            ├── Lambda (transform)
            ├── Lambda (load to DynamoDB)
            └── SNS (notify completion)
  • S3 event triggers EventBridge, which starts a Step Functions workflow
  • Each step has built-in retry and error handling
  • Step Functions provides visual monitoring and execution history

Pattern 5: Choreography vs Orchestration

ApproachDescriptionAWS Service
ChoreographyServices react to events independently; no central coordinatorEventBridge, SNS, SQS
OrchestrationCentral coordinator manages the workflow sequenceStep Functions
Choreography:
  OrderService → publishes "OrderCreated"
  PaymentService → reacts to "OrderCreated"
  InventoryService → reacts to "OrderCreated"
  (each service acts independently; no coordinator)

Orchestration:
  Step Functions → calls PaymentService
                 → calls InventoryService
                 → calls ShippingService
  (central workflow controls the sequence)

When to Choose

CriteriaChoreographyOrchestration
Simple event reactionsPreferredOverkill
Complex multi-step with orderingHarder to managePreferred
Need visibility of full workflowRequires custom correlationBuilt-in with Step Functions
Service independenceHigher (loose coupling)Lower (coordinator dependency)
Error handlingEach service manages its ownCentral retry and catch logic

Pattern 6: Data Lake Ingestion

Kinesis Data Stream (real-time)
    → Lambda (transform)
        → S3 (data lake)
            → Athena (query)

DynamoDB Streams
    → Lambda (aggregate)
        → S3 / OpenSearch
  • Kinesis or DynamoDB Streams provide real-time change feeds
  • Lambda transforms and delivers to S3 or analytics services
  • Fully serverless — no cluster management

Pattern 7: Scheduled Tasks

EventBridge Scheduler (cron)
    → Lambda
        → Generate report → S3
        → Clean up old data → DynamoDB TTL
        → Send digest email → SES
  • Replaces cron jobs on EC2 instances
  • EventBridge Scheduler supports one-time and recurring schedules
  • Lambda handles the task logic; no server to maintain

Serverless Anti-Patterns

Anti-PatternProblemAlternative
Lambda for long-running tasks15-minute timeout; expensive for compute-heavy workECS Fargate, Step Functions
Lambda for persistent connectionsWebSocket needs stateful connection managementAPI Gateway WebSocket, AppSync
Monolithic LambdaSingle large function handling everythingBreak into single-purpose functions
Synchronous chainsLambda → Lambda → Lambda (latency, coupling)SQS between steps or Step Functions
Cold start-sensitive workloadsLatency-critical paths with infrequent invocationsProvisioned Concurrency, Fargate

Quick Reference

PatternKey ServicesWhen to Use
Synchronous APIAPI GW + Lambda + DynamoDBSimple CRUD, low-latency response needed
Async ProcessingAPI GW + SQS + LambdaDecouple ingestion from processing
Fan-OutSNS + SQS + LambdaOne event, multiple independent consumers
Event PipelineEventBridge + Step Functions + LambdaMulti-step processing with orchestration
ChoreographyEventBridge + services reactLoosely coupled event-driven microservices
OrchestrationStep FunctionsComplex ordered workflows with retries
Data Lake IngestionKinesis/DDB Streams + Lambda + S3Real-time analytics pipeline
Scheduled TasksEventBridge Scheduler + LambdaReplace cron jobs

AWS Implementation Options

RequirementServerless OptionNon-Serverless Alternative
HTTP API backendAPI Gateway + LambdaALB + ECS / EC2
Asynchronous task queueSQS + LambdaSQS + ECS worker
Workflow orchestrationStep FunctionsECS + custom state management
Event routingEventBridgeAmazon MQ, self-managed Kafka
Real-time data processingKinesis + LambdaKinesis + KCL on EC2
NoSQL databaseDynamoDB (On-Demand)DynamoDB (Provisioned), MongoDB
File processingS3 event → LambdaS3 event → ECS task

SAA/SAP Exam Tips

SAA Tip: "Fully serverless" or "no infrastructure management" → Lambda + API Gateway + DynamoDB is the standard serverless stack. Add SQS for decoupling and Step Functions for orchestration.

SAP Tip: Know when serverless is NOT the right answer: persistent connections (use WebSocket API or AppSync), compute-heavy tasks > 15 min (use Fargate or Batch), and latency-sensitive workloads with cold start issues (use Provisioned Concurrency or containers).

SAP Tip: Choreography (EventBridge) vs Orchestration (Step Functions) is a common exam question. Choreography for simple event reactions with loose coupling; orchestration when execution order, error handling, and visibility matter.


Built by Fred Cheung @CookedRicer · Powered by Fumadocs & Github Copilot

On this page