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.
SecurityBảo mật Same trio as SQS: in-flight HTTPS, at-rest KMS, optional client-side encryption. IAM policies gate the SNS API; SNS access policies (like S3 bucket policies) handle cross-account access and let other services (S3…) write to a topic. Bộ ba giống SQS: in-flight HTTPS, at-rest KMS, client-side encryption nếu muốn. IAM policy chặn ở SNS API; SNS access policy (giống bucket policy của S3) lo cross-account và cho service khác (S3…) ghi vào topic.

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.
Application: S3 events → multiple queuesỨng dụng: S3 events → nhiều queue S3 allows only one event rule per combination of event type (e.g. object create) and prefix (e.g. 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.
Application: SNS → Firehose → S3Ứng dụng: SNS → Firehose → S3 SNS can deliver into Kinesis Data Firehose, so events land in S3 (or any KDF destination) with no consumer code. This is the "archive everything that flows through the topic" answer. SNS deliver được vào Kinesis Data Firehose, nên event rơi vào S3 (hoặc bất kỳ đích KDF nào) mà không cần code consumer. Đây là đáp án cho "archive mọi thứ chảy qua topic".

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.
FIFO throughput — the number movedThroughput FIFO — con số đã đổi The old figure — "same throughput as SQS FIFO", 300/s — is out of date. Since Nov 2023 the per-topic default is 3,000 msg/s, and high-throughput mode (Jan 2025) goes far beyond. The per-group 300/s limit is what remains. Con số cũ — "throughput như SQS FIFO", 300/s — đã lỗi thời. Từ 11/2023 mặc định mỗi topic là 3,000 msg/s, và high-throughput mode (1/2025) còn cao hơn nhiều. Giới hạn 300/s mỗi group là cái còn lại.

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ằng FilterPolicyScope.
  • 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ó.