AWS SNS/SQS
Amazon SNS (Simple Notification Service) and SQS (Simple Queue Service) provide managed messaging for event sourcing on AWS.
Why SNS/SQS
Section titled “Why SNS/SQS”| Strength | Benefit |
|---|---|
| Fully managed | No infrastructure to operate |
| Serverless | Lambda integration |
| Cost effective | Pay per request |
| High availability | Multi-AZ by default |
| Fan-out | SNS → multiple SQS queues |
Trade-offs
Section titled “Trade-offs”| Concern | Consideration |
|---|---|
| AWS lock-in | Not portable to other clouds |
| 256 KB limit | Large events need S3 offloading |
| Ordering | FIFO queues required for ordering |
Configuration
Section titled “Configuration”[bus]backend = "sns_sqs"
[bus.sns_sqs]region = "us-east-1"topic_prefix = "angzarr"Environment Variables
Section titled “Environment Variables”export AWS_REGION="us-east-1"export SNS_SQS_TOPIC_PREFIX="angzarr"export BUS_BACKEND="sns_sqs"# AWS credentials via standard mechanismsTopology
Section titled “Topology”SNS provides fan-out to SQS queues:
flowchart LR
TP["SNS Topic: angzarr-events-player"] --> PP[SQS: player-projector-queue]
TP --> OP1[SQS: output-projector-queue]
TH["SNS Topic: angzarr-events-hand"] --> HS[SQS: hand-saga-queue]
TH --> OP2[SQS: output-projector-queue]
FIFO vs Standard
Section titled “FIFO vs Standard”Standard Queues
Section titled “Standard Queues”- At-least-once delivery
- Best-effort ordering
- Higher throughput
FIFO Queues
Section titled “FIFO Queues”- Exactly-once processing
- Strict ordering within message groups
- 300 TPS limit (3000 with batching)
For event sourcing, FIFO queues with message group ID = aggregate root:
sqs.send_message() .queue_url(&fifo_queue_url) .message_body(&event_json) .message_group_id(&format!("{}#{}", domain, root)) .message_deduplication_id(&event_id) .send() .await?;Large Event Handling
Section titled “Large Event Handling”Events exceeding 256 KB use S3 for payload storage:
1. Store payload in S32. Publish reference to SNS/SQS3. Consumer fetches from S3This is the “claim check” pattern, handled automatically by OffloadingEventBus.
Dead Letter Queues
Section titled “Dead Letter Queues”Configure DLQs for failed messages:
# CloudFormation/TerraformPlayerProjectorDLQ: Type: AWS::SQS::Queue Properties: QueueName: player-projector-dlq
PlayerProjectorQueue: Type: AWS::SQS::Queue Properties: QueueName: player-projector-queue RedrivePolicy: deadLetterTargetArn: !GetAtt PlayerProjectorDLQ.Arn maxReceiveCount: 5# Create SNS topicsaws sns create-topic --name angzarr-events-playeraws sns create-topic --name angzarr-events-hand
# Create SQS queuesaws sqs create-queue --queue-name player-projector-queue
# Subscribe queue to topicaws sns subscribe \ --topic-arn arn:aws:sns:us-east-1:123456789:angzarr-events-player \ --protocol sqs \ --notification-endpoint arn:aws:sqs:us-east-1:123456789:player-projector-queueHelm Deployment
Section titled “Helm Deployment”bus: backend: sns_sqs
sns_sqs: enabled: true region: us-east-1 topicPrefix: angzarr # IAM role-based auth recommendedWhen to Use SNS/SQS
Section titled “When to Use SNS/SQS”- AWS native — Already on AWS
- Lambda integration — Serverless event handlers
- Cost sensitive — Pay-per-use model
- Managed — No operational overhead