Amazon SNS (Simple Notification Service)
Simple Notification Service, Pub/Sub, Fan-out, Message Filtering
Tổng quan
Amazon SNS là fully managed pub/sub messaging service để gửi notifications đến nhiều subscribers cùng lúc.
SNS vs SQS
| SNS | SQS | |
|---|---|---|
| Model | Pub/Sub (push) | Queue (pull) |
| Message Delivery | 1 message → ALL subscribers | 1 message → 1 consumer |
| Persistence | No (instant delivery) | Yes (up to 14 days) |
| Consumer | Passive (receive push) | Active (poll messages) |
1. Core Concepts
1.1 Topic
1.2 Message Structure
1.3 Publishers
| Publisher | Mô tả |
|---|---|
| AWS SDK | Publish API từ applications |
| CloudWatch Alarms | Alert notifications |
| S3 Events | Bucket notifications |
| Lambda | Function triggers |
| Many AWS Services | EventBridge, CodePipeline... |
2. Subscription Types
2.1 Tất cả Subscription Protocols
2.2 Protocol Details
| Protocol | Use Case | Confirmation |
|---|---|---|
| SQS | Decouple processing | Auto-confirmed |
| Lambda | Serverless processing | Auto-confirmed |
| HTTP/HTTPS | Webhooks, APIs | Must confirm subscription |
| User alerts | Must confirm via link | |
| SMS | Mobile alerts | No confirmation needed |
| Mobile Push | App notifications | Device registration |
2.3 Subscription Confirmation
3. Message Filtering
3.1 Filter Policy
3.2 Filter Policy Operators
| Operator | Example | Matches |
|---|---|---|
| Exact match | {"color": ["red"]} | color = "red" |
| OR | {"color": ["red", "blue"]} | color = "red" OR "blue" |
| Prefix | {"region": [{"prefix": "us-"}]} | region starts with "us-" |
| Numeric | {"price": [{"numeric": [">", 100]}]} | price > 100 |
| Exists | {"color": [{"exists": true}]} | color attribute exists |
| Anything-but | {"color": [{"anything-but": "red"}]} | color != "red" |
3.3 Filter Policy Scope
4. SNS + SQS Fan-out
4.1 Vấn đề và Giải pháp
Vấn đề: Một event cần trigger nhiều xử lý độc lập
Giải pháp: Fan-out = "Phân nhánh" message ra nhiều hướng
4.2 Tại sao dùng SQS thay vì Lambda trực tiếp?
| SNS → Lambda | SNS → SQS → Consumer | |
|---|---|---|
| Buffering | ❌ Lambda phải sẵn sàng | ✅ SQS giữ message khi app down |
| Retry | Lambda tự handle | ✅ SQS có built-in retry |
| DLQ | Phức tạp hơn | ✅ DLQ dễ config |
| Rate limiting | ❌ Lambda bị overwhelm | ✅ Consumer xử lý theo tốc độ |
4.3 Fan-out với Message Filtering
⚠️ QUAN TRỌNG: Filter xảy ra ở tầng SNS, TRƯỚC KHI gửi đến SQS
Tại sao filter ở SNS tốt hơn filter ở SQS/Consumer?
| Filter ở Consumer | Filter ở SNS | |
|---|---|---|
| Flow | SQS nhận TẤT CẢ → Consumer đọc rồi bỏ | SNS chỉ gửi message match |
| Cost | ❌ Tốn tiền SQS requests | ✅ Tiết kiệm |
| Load | ❌ Consumer xử lý message không cần | ✅ Giảm load |
4.4 Cross-Region Fan-out
5. Spring Boot Integration
5.1 Spring Boot có thể nhận SNS messages?
Có! Nhưng có 2 cách:
5.2 Option 1: HTTP Endpoint
5.3 Option 2: SNS → SQS → Spring Boot (Recommended)
5.4 So sánh các patterns
| Pattern | Pros | Cons |
|---|---|---|
| SNS → HTTP | Simple, direct | Need public URL, handle retries |
| SNS → SQS → App | Decoupled, buffering, DLQ | Extra hop, slight delay |
| SNS → Lambda | Serverless, auto-scale | Cold start, 15 min timeout |
6. FIFO Topics
6.1 FIFO Topic Features
6.2 Message Group ID
7. Limits & Quotas
Nguồn: AWS General Reference — Amazon SNS endpoints and quotas. Các quota publish là per account, per Region và có thể thay đổi theo Region.
7.1 Resource quotas
| Resource | Default quota |
|---|---|
| Standard topics | 100,000 topics/account |
| FIFO topics | 1,000 topics/account |
| Standard topic subscriptions | 12,500,000 subscriptions/topic |
| FIFO topic subscriptions | 100 subscriptions/topic |
| Pending subscriptions | 5,000/account |
| Message size | 256 KiB |
| Message header size | 16 KiB |
Messages per PublishBatch request | 10 messages |
| Email / email-json delivery rate | 10 messages/second/subscription — hard limit |
| Promotional SMS delivery rate | 20 messages/second |
| Transactional SMS delivery rate | 20 messages/second |
7.2 Publish throughput quota
SNS Publish/PublishBatch throughput quota tính theo messages published per second, không chỉ số API calls. PublishBatch có thể gửi tối đa 10 messages/request, nên ví dụ quota 30,000 msg/s có thể đạt bằng:
Publish: 30,000 API requests/s × 1 messagePublishBatch: 3,000 API requests/s × 10 messages- kết hợp
PublishvàPublishBatch, miễn tổng message/s không vượt quota Region
| AWS Region group | Standard topics | FIFO topics |
|---|---|---|
US East (N. Virginia) us-east-1 | 30,000 msg/s | 30,000 msg/s |
US West (Oregon) us-west-2, Europe (Ireland) eu-west-1 | 9,000 msg/s | 9,000 msg/s |
| US East (Ohio), US West (N. California), Mumbai, Seoul, Singapore, Sydney, Tokyo, Frankfurt | 1,500 msg/s | 3,000 msg/s |
| All other supported Regions | 300 msg/s | 3,000 msg/s |
7.3 FIFO-specific throughput notes
| Scope | Quota / behavior |
|---|---|
| Per message group | Up to 300 messages/second |
| Per FIFO topic default | 3,000 messages/second or 20 MB/second, whichever comes first, when FifoThroughputScope = Topic |
| High-throughput FIFO | Có thể đạt cao hơn khi dùng message-group-level throughput và request quota increase theo Region |
| Ordering tradeoff | Cross-Region deliveries có thể giảm throughput trong một message group vì phải giữ strict ordering |
7.4 API throttling examples
Một số API throttling là hard quota và không tăng được:
| API | Transactions/second |
|---|---|
Subscribe | 100 |
Unsubscribe | 100 |
ListTopics | 30 |
ListSubscriptions | 30 |
ListSubscriptionsByTopic | 30 |
AddPermission / RemovePermission | 10 |
GetSMSAttributes | 20 |
SetSMSAttributes | 1 |
Các API quản trị topic/subscription như CreateTopic, DeleteTopic, GetTopicAttributes, SetTopicAttributes, ConfirmSubscription có quota khác nhau theo Region, ví dụ:
| Region group | Transactions/second |
|---|---|
| US East (N. Virginia) | 3,000 |
| US West (Oregon), Europe (Ireland) | 900 |
| Một số Region lớn khác | 150 |
| Các Region còn lại | 30 |
7.5 Exam notes
- Standard SNS không có “unlimited throughput” tuyệt đối — publish throughput vẫn bị quota theo account/Region.
- Quota publish là soft quota: có thể request tăng qua Service Quotas/AWS Support.
- Email delivery 10 msg/s/subscription là hard limit: không tăng được.
- Nếu cần throughput cao, ưu tiên
PublishBatch, chọn Region phù hợp, và request quota increase trước peak traffic.
8. Security
8.1 Encryption
8.2 Access Control
SNS Access Policy Example (Allow S3 to publish):
9. Best Practices
9.1 Design Patterns
| Pattern | Description |
|---|---|
| Fan-out | SNS → Multiple SQS queues |
| Event-driven | SNS → Lambda |
| Cross-account | SNS topic in Account A, SQS in Account B |
| Cross-region | SNS topic in region A, SQS in region B |
9.2 Message Filtering Best Practices
| Practice | Recommendation |
|---|---|
| Filter at SNS | Don't filter in Lambda/Consumer |
| Use MessageAttributes | More efficient than body filtering |
| Keep policies simple | Complex policies = slower processing |
9.3 Reliability
9.4 Cost Optimization
| Strategy | Description |
|---|---|
| Message Filtering | Reduce unnecessary deliveries |
| Batch Publishing | PublishBatch API (up to 10 msgs) |
| Right Protocol | SQS cheaper than SMS for alerts |
10. SNS vs SQS vs EventBridge
| Feature | SNS | SQS | EventBridge |
|---|---|---|---|
| Model | Pub/Sub | Queue | Event Bus |
| Filtering | Simple (attributes) | None | Advanced (100+ rules) |
| Persistence | No | Yes | Archive/Replay |
| Targets | Many protocols | Consumer apps | AWS services |
| Transform | No | No | Yes |
| Use Case | Fan-out | Decouple | Event-driven |
11. Common Exam Questions
Q1: SNS vs SQS delivery model?
A: SNS = Push to all subscribers. SQS = Consumer pulls messages, 1 per message.
Q2: SNS FIFO can subscribe to what?
A: Only SQS FIFO queues. Cannot subscribe to Lambda or Standard SQS.
Q3: Message filtering là gì?
A: Filter policy trên subscription để chỉ nhận messages matching criteria.
Q4: Max subscriptions per topic?
A: 12,500,000 subscriptions
Q5: SNS message max size?
A: 256 KB
Q6: How to ensure SQS receives from SNS?
A: Configure SQS access policy cho phép SNS topic gửi messages.