Amazon SNS
Amazon Simple Notification Service (SNS) is a fully managed pub/sub messaging service that enables you to decouple microservices, distributed systems, and serverless applications.
Key Concepts
- Topics: Communication channels for publishing messages
- Publishers: Services that send messages to topics
- Subscribers: Endpoints that receive messages from topics
- Messages: Up to 256 KB of data in various formats
Subscription Types
| Endpoint | Use Case |
|---|---|
| Lambda | Serverless processing |
| SQS | Queue-based processing |
| HTTP/HTTPS | Webhook integrations |
| Notifications to people | |
| SMS | Text message alerts |
| Mobile push | iOS, Android notifications |
Common Patterns
Fan-out
One message to multiple consumers:
Publisher → SNS Topic → [SQS Queue 1, SQS Queue 2, Lambda]
Alerting
CloudWatch Alarm → SNS → [Email, SMS, PagerDuty webhook]
Event Broadcasting
Application → SNS → Multiple microservices
Example: Publishing a Message
const sns = new AWS.SNS();
await sns.publish({
TopicArn: 'arn:aws:sns:us-east-1:123456789:my-topic',
Message: JSON.stringify({ event: 'order_placed', orderId: '12345' }),
MessageAttributes: {
eventType: { DataType: 'String', StringValue: 'order_placed' }
}
}).promise();
What We Like
- Simplicity: Easy to set up and use
- Fan-out: One publish, many subscribers
- Durability: Messages are stored across multiple AZs
- Filtering: Route messages to subscribers based on attributes
What We Don't Like
- No replay: Messages can't be re-read once consumed (unlike EventBridge)
- Limited retention: Failed deliveries have limited retry windows
- Ordering: Standard topics don't guarantee order (FIFO topics do, with limitations)
SNS vs SQS vs EventBridge
| Feature | SNS | SQS | EventBridge |
|---|---|---|---|
| Pattern | Pub/Sub | Queue | Event bus |
| Consumers | Many | One (per message) | Many |
| Replay | No | No | Yes (archive) |
| Filtering | Basic | No | Advanced |