Amazon SNS
Pub/sub: one producer publishes to a topic, every subscriber gets the message pushed to it. The exam angle is almost always the fan-out pattern — SNS in front of multiple SQS queues — plus the S3 one-event-rule workaround, FIFO topics, and filter policies.
Pub/sub: một producer publish vào topic, mọi subscriber được push message. Góc ra đề gần như luôn là pattern fan-out — SNS đứng trước nhiều queue SQS — cộng với trick S3 one-event-rule, FIFO topic, và filter policy.
Reach for itNên dùng
- One event, many consumers — fan-out to SQS / Lambda / FirehoseMột event, nhiều consumer — fan-out sang SQS / Lambda / Firehose
- Alerting: CloudWatch Alarms, Budgets, ASG notifications → email/Slack hookAlerting: CloudWatch Alarms, Budgets, notification ASG → email/Slack hook
- Push to people (A2P): email, SMS, mobile pushPush tới người (A2P): email, SMS, mobile push
- Archive events to S3 with zero code: SNS → Firehose → S3Archive event vào S3 không cần code: SNS → Firehose → S3
Not the right toolKhông hợp
- One consumer needing persistence / retries / delay → SQS aloneMột consumer cần persistence / retry / delay → SQS là đủ
- Streaming with replay and long retention → KinesisStreaming có replay và retention dài → Kinesis
- SNS doesn't persist: no subscriber at delivery time = message goneSNS không lưu: không có subscriber lúc deliver = message mất
- Strict ordering straight to Lambda/HTTP — FIFO topics only feed SQSOrdering chặt thẳng tới Lambda/HTTP — FIFO topic chỉ đổ vào SQS
Pub/Sub modelMô hình Pub/Sub
Without SNS, a "buying service" integrates directly with email, fraud, shipping, SQS… — N point-to-point links. With SNS, it publishes once to a topic and every subscriber receives it. Producer and receivers never know about each other. Không có SNS, một "buying service" phải nối trực tiếp với email, fraud, shipping, SQS… — N đường point-to-point. Có SNS, nó publish một lần vào topic và mọi subscriber đều nhận. Producer và receiver không cần biết nhau.
- Subscriber types (standard topic): SQS, Lambda, Kinesis Data Firehose, HTTP(S) endpoints, email, SMS & mobile push.Loại subscriber (topic standard): SQS, Lambda, Kinesis Data Firehose, HTTP(S) endpoint, email, SMS & mobile push.
- Many AWS services publish to SNS natively: CloudWatch Alarms, AWS Budgets, ASG notifications, S3 events, DynamoDB, CloudFormation state changes, DMS, RDS events…Nhiều service AWS publish thẳng vào SNS: CloudWatch Alarms, AWS Budgets, notification ASG, S3 events, DynamoDB, CloudFormation state change, DMS, RDS events…
- Two publish modes: Topic Publish (SDK: create topic → subscriptions → publish) and Direct Publish (mobile push: platform application → platform endpoint → publish; works with Google FCM, Apple APNS, Amazon ADM).Hai kiểu publish: Topic Publish (SDK: tạo topic → subscription → publish) và Direct Publish (mobile push: platform application → platform endpoint → publish; chạy với Google FCM, Apple APNS, Amazon ADM).
- Delivery is push — subscribers don't poll. Failed HTTP deliveries retry per the delivery policy; a subscription can have its own redrive policy → SQS DLQ for messages that still fail.Delivery là push — subscriber không poll. HTTP deliver fail sẽ retry theo delivery policy; mỗi subscription có thể gắn redrive policy → DLQ SQS cho message vẫn fail.
SNS + SQS: Fan-outSNS + SQS: Fan-out
The pattern to memorise: push once into SNS, every subscribed SQS queue receives a copy. Fully decoupled, no data loss — and each queue adds back what SNS lacks: persistence, delayed processing, retries. Pattern phải thuộc: push một lần vào SNS, mọi queue SQS đã subscribe nhận một bản. Decoupled hoàn toàn, không mất data — và mỗi queue bù lại thứ SNS thiếu: persistence, delayed processing, retry.
- Add more SQS subscribers over time without touching the producer.Thêm subscriber SQS theo thời gian mà không đụng producer.
- Make sure the SQS queue access policy allows SNS to write — the classic silent failure.Nhớ cho access policy của queue SQS quyền để SNS ghi — lỗi im lặng kinh điển.
- Cross-region delivery works — SNS can push to SQS queues in other regions.Delivery cross-region chạy được — SNS push sang queue SQS ở region khác.
images/). To send the same event to many SQS queues (and a Lambda), point the rule at an SNS topic and fan out.
S3 chỉ cho một event rule mỗi tổ hợp event type (vd object create) và prefix (vd images/). Muốn gửi cùng event tới nhiều queue SQS (và Lambda), trỏ rule vào topic SNS rồi fan-out.
FIFO topicsFIFO topic
Same idea as SQS FIFO, applied to pub/sub: ordering by MessageGroupId, dedup by MessageDeduplicationId or content hash. Use SNS FIFO → SQS FIFO when you need fan-out + ordering + deduplication at once.
Cùng ý tưởng SQS FIFO, áp cho pub/sub: ordering theo MessageGroupId, dedup bằng MessageDeduplicationId hoặc hash content. Dùng SNS FIFO → SQS FIFO khi cần fan-out + ordering + dedup cùng lúc.
- Subscribers can only be SQS queues — both Standard and FIFO. Email/SMS/HTTP/mobile can't guarantee ordering, so subscribing them errors out. Lambda needs an SQS hop in between.Subscriber chỉ có thể là queue SQS — cả Standard lẫn FIFO. Email/SMS/HTTP/mobile không đảm bảo được ordering nên subscribe là lỗi. Lambda phải đi vòng qua SQS.
- Throughput: 300 msg/s per message group · 3,000 msg/s or 20 MB/s per topic (default). High-throughput mode (
FifoThroughputScope: MessageGroup) raises it to standard-topic levels — dedup scope shrinks to the message group.Throughput: 300 msg/s mỗi message group · 3,000 msg/s hoặc 20 MB/s mỗi topic (mặc định). High-throughput mode (FifoThroughputScope: MessageGroup) nâng lên ngang topic standard — dedup thu về phạm vi message group. - FIFO topics support message archiving & replay — subscribers can replay past messages without producer involvement.FIFO topic hỗ trợ message archiving & replay — subscriber replay lại message cũ mà không cần producer.
Message filteringMessage filtering
A JSON filter policy per subscription. No policy = the subscription receives every message. With one, SNS only delivers matches — e.g. one topic for orders, separate queues for State: Placed, Cancelled, Declined, plus an unfiltered "all" queue.
Một filter policy JSON mỗi subscription. Không có policy = subscription nhận mọi message. Có policy, SNS chỉ deliver message khớp — vd một topic cho order, queue riêng cho State: Placed, Cancelled, Declined, cộng một queue "all" không filter.
- Filter scope: message attributes (default) or the message body (payload-based filtering) — set per subscription with
FilterPolicyScope.Phạm vi filter: message attributes (mặc định) hoặc message body (payload-based filtering) — set mỗi subscription bằngFilterPolicyScope. - Filtering replaces the anti-pattern of one topic per consumer type — one topic, N filtered subscriptions.Filtering thay cho anti-pattern mỗi loại consumer một topic — một topic, N subscription có filter.
- Non-matching messages aren't retried or dead-lettered for that subscription — they're simply not delivered to it.Message không khớp không retry hay vào DLQ cho subscription đó — đơn giản là không deliver tới nó.