Amazon SNS とは
Amazon Simple Notification Service (SNS) は、AWSが提供するフルマネージドなメッセージングサービス
出典: What is Amazon SNS? - Amazon Simple Notification Service
Amazon Simple Notification Service (SNS) is a fully managed messaging service that enables asynchronous communication between publishers and subscribers.
密結合から疎結合へ:SNSによるFanoutアーキテクチャ
密結合システムの問題
密結合したシステムを疎結合にする際、ポーリング方式を採用すると以下の問題が発生する
┌─────────────┐ polling ┌─────────────┐
│ Consumer A │ ◀──────────────▶│ │
├─────────────┤ polling │ 保留場所 │
│ Consumer B │ ◀──────────────▶│ (Queue) │
├─────────────┤ polling │ │
│ Consumer C │ ◀──────────────▶│ │
└─────────────┘ └─────────────┘
↑
ポーリング負荷が増大
Push型アーキテクチャによる解決(Fanout)
SNSはPush型のアーキテクチャを採用し、メッセージ受信時のみ配信を行う
出典: Amazon SQS, Amazon SNS, or Amazon EventBridge?
Amazon SNS uses a push-based communication model, where subscribers receive messages in real-time as they are published, enabling immediate message delivery to multiple subscribers.
┌─────────────┐
push │ Consumer A │
┌───────────┐ ┌───────┴───▶└─────────────┘
│ Publisher │───▶│ SNS │push ┌─────────────┐
└───────────┘ │ Topic │────▶│ Consumer B │
└───────┬───▶└─────────────┘
push │ Consumer C │
└─────────────┘
ポーリングとの違い:
- Polling: 各Consumerが定期的に保留場所を確認しに行く
- Push (SNS): メッセージ発行時のみ、SNSが各Subscriberに配信
Pub/Subモデル
SNSはFanoutの実現にPub/Sub(Publish-Subscribe)パターンを採用
出典: Publish-subscribe pattern - AWS Prescriptive Guidance
The publish-subscribe pattern is a messaging pattern that decouples message senders (publishers) from interested receivers (subscribers) by using an intermediary known as a message broker or router.
| 役割 | 説明 |
|---|---|
| Publisher | メッセージを発行する側 |
| Topic | メッセージの中継点(ブローカー) |
| Subscriber | メッセージを受信する側(Lambda, SQS, HTTP等) |
SNSの代表的機能
- Pub/Sub: 多数のサブスクライバーへの一括配信(Lambda, SQS, HTTP/S等)
- Mobile Push: iOS/Androidアプリへのプッシュ通知
出典: Send Fanout Event Notifications
When a message is published to the topic, identical notifications are sent to each subscribed queue, enabling parallel and asynchronous processing of the message by the subscribed services.
サポートされるエンドポイント
エンドポイントとは、SNSの文脈では「通知を受け取る宛先」のこと
出典: Subscribe - Amazon Simple Notification Service API Reference
The endpoint that you want to receive notifications. Endpoints vary by protocol
| プロトコル | エンドポイント |
|---|---|
| http | http:// で始まるURL |
| https | https:// で始まるURL |
| メールアドレス | |
| sms | SMS対応デバイスの電話番号 |
| sqs | Amazon SQSキューのARN |
| lambda | AWS Lambda関数のARN |
| application | モバイルアプリのEndpointArn |
| firehose | Amazon Data Firehose配信ストリームのARN |
2種類のトピック
SNS offers two types of topics: Standard and FIFO. Standard topics support high throughput, best-effort ordering, and best-effort deduplication, allowing messages to be delivered to various endpoints, including Amazon SQS, Amazon Data Firehose, AWS Lambda, HTTPS, SMS, mobile push, and email. FIFO topics, designed for applications where order and deduplication are critical, offer strict ordering, strict deduplication, and message delivery to Amazon SQS queues.
| タイプ | 特徴 |
|---|---|
| Standard | 高スループット、ベストエフォート順序保証、多様なエンドポイント対応 |
| FIFO | 厳密な順序保証、厳密な重複排除、SQSキューのみ対応 |
出典: Amazon SNS message delivery for FIFO topics
Amazon SNS FIFO topics cannot deliver messages to customer-managed endpoints like email addresses, mobile apps, phone numbers, or HTTP(S) endpoints due to their inability to preserve strict message ordering.
FIFOトピックがSQSキューのみ対応する理由:email、モバイルアプリ、電話番号、HTTP(S)エンドポイントは厳密なメッセージ順序を保証できないため
SNS と SQS の違い
両者は補完関係にあり、併用されることが多い
出典: Amazon SQS, Amazon SNS, or Amazon EventBridge?
| 観点 | SNS | SQS |
|---|---|---|
| 通信モデル | Push型(リアルタイム配信) | Pull型(Consumerがポーリング) |
| 配信先 | 複数のSubscriberに同時配信 | 1つのConsumerが1メッセージを処理 |
| メッセージ永続化 | なし(配信したら終わり) | あり(Consumerが取得するまで保持) |
| 主な用途 | Fanout、通知、ブロードキャスト | 非同期処理、バッファリング、デカップリング |
Amazon SQS uses a pull-based communication model, where consumers actively poll messages from the queue, allowing for fine-grained control over message processing rates and independent scaling of consumers.
Amazon SNS is often used for fanout notifications, pub/sub messaging, and mobile push notifications, facilitating the broadcasting of messages to multiple subscribers simultaneously.
併用パターン(SNS + SQS)
出典: Fanout Amazon SNS notifications to Amazon SQS queues
By subscribing an Amazon SQS queue to an Amazon SNS topic, messages can be delivered to applications requiring immediate notification and also persisted in an Amazon SQS queue for later processing.
┌─────────┐ polling ┌──────────┐
push │ SQS A │◀──────────────│ Worker A │
┌───────────┐ ┌───────┴───▶└─────────┘ └──────────┘
│ Publisher │───▶│ SNS │push ┌─────────┐ polling ┌──────────┐
└───────────┘ │ Topic │────▶│ SQS B │◀──────────────│ Worker B │
└───────┴───▶└─────────┘ └──────────┘
push │ Lambda │(直接実行)
└─────────┘
この構成のメリット:
- SNSでFanout(一括配信)
- SQSでメッセージを永続化・バッファリング
- 各Workerは自分のペースで処理可能
料金体系
- 初期費用なし、従量課金制
- 無料枠: 月100万メッセージ、10万通知
出典: Amazon SNS FAQs
SNS offers a free tier, which includes 1 million messages per month and 100,000 notifications per month.