More Related Content
Similar to [AWSマイスターシリーズ] Amazon SQS / SNS
Similar to [AWSマイスターシリーズ] Amazon SQS / SNS (20)
More from Amazon Web Services Japan
More from Amazon Web Services Japan (20)
[AWSマイスターシリーズ] Amazon SQS / SNS
- 1. re:G
ene
AWS マイスターシリーズ
Amazon SQS / SNS
rate
アマゾン データサービス ジャパン株式会社
ソリューションアーキテクト
⼋八⽊木橋 徹平 篠原 英治 ⾈舟崎 健治
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 2. Agenda
! Amazon SQSとは
• SQSの動作イメージ
• SQSの機能の詳細
! Amazon SNSとは
• SNSの動作イメージ
• SNS Mobile Push
! SQS/SNSを使った構成例例
! まとめ
! Appendix
2
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 3. そもそもメッセージキューとは?
! サーバ間、プロセス間、スレッド間での通信に使われる
ソフトウェアコンポーネント
• ⾮非同期型通信を提供
メッセージ
キュー
送信者
3
・送信側と受信側がメッセージキュー
に同時にやりとりしなくても良い
・受信側がメッセージを取り出すまで
格納されたまま
受信者
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 4. Introduction
! 「分割できないものは、スケールできない」
by Randy Shoup (eBay)
• スケールするには疎結合なアーキテクチャにする必要がある
• 疎結合アーキテクチャには⾮非同期処理理が不不可⽋欠
• ⾮非同期処理理の典型例例がキューシステム
! ⾃自分で⾼高い耐障害性を持つキューシステムを作るのは困難
4
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 5. Amazon SQSとは
5
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 6. Amazon SQS
(Simple Queue Service)とは
! 分散マネージドキュー
! ⾼高い信頼性とスケーラブルなキュー
送信者
Queue
受信者
受信者
6
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 13. SQSの動作イメージ
! タスク実⾏行行
• 受信後デフォルト30秒間は他の受信者
は受信できない(Visibility Timeout
機能)
メッセージ
送信者
Inflightメッセージ
(=Visibility Timeoutによっ
て他から見えない状態の
メッセージ)
メッセージ
受信者
メッセージ
受信者
メッセージ
受信者
13
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 17. SQSの動作イメージ
! 複数の送信者
メッセージ
受信者
A C
B
メッセージ
受信者
メッセージ
送信者
メッセージ
受信者
17
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 19. SQSの動作イメージ
! 1度度に最⼤大10通送信可能
x 10
メッセージ
受信者
x 10
x 10
メッセージ
受信者
メッセージ
送信者
メッセージ
受信者
19
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 20. SQSの動作イメージ
! 1度度に最⼤大10通受信可能
x 10 メッセージ
受信者
x 10
メッセージ
x 10 受信者
メッセージ
送信者
メッセージ
受信者
20
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 22. SQS利利⽤用上の注意点
! 最低1度度のメッセージ到達を保障
• 2度度以上同じメッセージを受信することがある
何回同じメッセージを受信しても同じ結果になるように実装する
! メッセージの順序は保障しない
• 後に送ったメッセージが先に受信されることがある
受信順序が違っても同じ結果になるように実装する
22
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 23. SQSの機能の詳細
23
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 24. SQS Long Polling機能
! メッセージが有効になるまで待つことが出来る機能
• タイムアウトがなければ、ReceiveMessageコールにより、最
低1つ、または1コールで取得可能な最⼤大数のメッセージを取得
可能(メッセージが存在する場合)
! 空もしくは取得失敗のレスポンスを削減可能
! ReceiveMessageコールのWaitTimeSecondsの値を変
更更することでPolling可能
• 0〜~20秒の間
24
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 25. Long PollingとShort Pollingの
使い分けについて
! 多くの場合は、Long Pollingを推奨
• 複数のキューを使う場合は、マルチスレッドでPollingする
Polling処理 スレッド1
Polling処理 スレッド2
! Short Pollingを使う必要がある場合
• RecieveMessage呼び出し後直ちに応答が必要な場合
• (例例)複数のキューを単⼀一スレッドポーリングする場合、Long
Pollingタイムアウトするまで待機するため、処理理が遅れてしまう。
Polling処理
Polling処理 スレッド1
25
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 26. SQS Delay Queue機能
! キューに送られた新しいメッセージをある⼀一定秒の間⾒見見
えなくすることが可能
• 0〜~900秒に設定可能
• 設定すると、そのキューに送信されるメッセージ全てに適⽤用
• Visibility Timeoutとは異異なる
26
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 27. SQS Message Timers機能
! 個々のメッセージが送信されてから⾒見見えるようになるま
での時間を設定可能
• デフォルトは0秒。すぐ⾒見見えるようになる
• キュー全体ではなく、メッセージ単体に適⽤用
• Message TimersがDelay Queueの遅延時間の設定を上書きす
る
メッセージが見える
ようになる時間
Send Message
Request
Message Timers 60秒
Delay Queue 30秒
27
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 28. SQS Dead Letter Queue機能
! キューから指定回数受信後に⾃自動で別のDead Letter
Queueにメッセージが移動される機能
• デフォルトは無効
• 事前に作成したキューをDead Letter Queueとして指定する
• ずっと未処理理メッセージが残り続ける状況を回避するのに有効
キュー
④メッセージが
指定回数受信
後に移動
①メッセージ受信
②タスク失敗
③メッセージ削除されずに
キューに残り続ける
Dead Letter Queue
28
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 29. SQS Batch API actions
! 1コールで最⼤大10リクエスト処理理が可能
• SendMessageBatch
• DeleteMessageBatch
• ChangeMessageVisibilityBatch
! 通常メッセージ1通あたり256KBまで送信可能
• 注意:SendMessageBatchシングルコールで送れるメッセージ
の合計サイズも256KBまで。
29
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 30. Client-‐‑‒side BufferingおよびRequest
Batching
! Client-‐‑‒sideバッファが可能
• AmazonSQSBufferedAsyncClient
! 以下のリクエストをバッチに纏めることが可能
• SendMessage
• DeleteMessage
• ChangeMessageVisibility
! 10リクエストまでバッファ可能
! Batchリクエストとして送信するため、SQSへのリクエ
スト数を削減し、コスト削減が可能
30
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 31. SQSのスケーリング
! SQSキューは⾼高いスループットを提供できる
• 秒間数千メッセージ
! ⾼高いスループットを出す⽅方法
• Batch APIにより1度度に10メッセージを処理理する
• 多くのメッセージ送信者と受信者を配置可能
• AWSSDK for Javaではデフォルト50コネクション
• 以下のようにMaxConnectionsの値を変更更する
AmazonSQS sqsClient = new AmazonSQSClient(credentials,
new ClientConfiguration().withMaxConnections(
producerCount + cousumerCount));
31
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 32. SQSのアクセスコントロール
! キューへのアクセスコント
ロールが可能。以下その例例
アクセスコントロール設定例(JSON形式)
{
"Version":"2012-11-05",
"Id":"cd3ad3d9-2776-4ef1-a904-4c229d1642ee",
"Statement" : [
{
"Sid":"1",
"Effect":"Allow",
"Principal" : {
"aws": "111122223333"
},
"Action":["sqs:SendMessage","sqs:ReceiveMessage"],
"Resource": "arn:aws:sqs:us-east-1:444455556666:queue2",
"Condition" : {
"IpAddress" : {
"aws:SourceIp":"10.52.176.0/24"
},
"DateLessThan" : {
"aws:CurrentTime":"2009-06-30T12:00Z"
}
}
}
]
• 他のAWSアカウントに
SendMessageのみ許可
• 特定の時間のみアクセス許可
• 特定のAWSアカウントのア
クセスを拒否
• EC2インスタンスからのアク
セスのみ許可
32
}
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 33. Amazon CloudWatchを使ったSQS監視
! 以下のメトリックスを利利⽤用可能
NumberOfMessageSent
キューに追加されたメッセージ数
SentMessageSize
キューに追加されたメッセージの合計サイズ
NumberOfMessageReceived
ReceiveMessageコールによって返されたメッセージ数
NumberOfEmptyReceives
ReceiveMessageによって返さなかったメッセージ数
NumberOfMessagesDeleted
キューから削除されたメッセージ数
ApproximateNumberOfMessage
Delayed
Delayされすぐに読み込みができなかったメッセージ数。
Delay Queueまたはメッセージ送信時のDelay設定によ
るもの
ApproximateNumberOfMessage
Visible
キューから利用可能になったメッセージ数
ApproximateNumberOfMessage
NotVisible
クライアントから送信されたが削除されていないか、
visibility winodwのendまで到達していないメッセージ数
33
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 34. SQS Best Practice(1)
! 1度度のリクエストで複数のメッセージを送信または受信
する⽅方が速い
• 最⼤大10通まで
! 1つのキューに対する送信者または受信者は複数⽴立立てた
⽅方が全体の処理理速度度が向上する
! 複数のキューにLong Pollingするときには、キューごと
に1スレッドを使う
• 最⼤大20秒まで待つことができる
34
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 35. SQS Best Practice(2)
! 何度度も同じメッセージを受信しても同じ結果になるよう
に受信者側を実装する
• SQSは最低1度度のメッセージ到達を保証
! メッセージの受信順序が違ってても同じ結果になるよう
に受信側を実装する
• SQSはメッセージの順序を保証しない
! メッセージ受信側でPolling処理理を継続して実⾏行行するよう
な仕組みの実装が必要
• (例例)supervisordを使えば、複数プロセスで定期的にpolling
ができ、かつ、プロセスが落落ちても⾃自動で⽴立立ち上げてくれる
35
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 37. SQSの価格
! 無料料利利⽤用枠
• 既にご利利⽤用中、初めてご利利⽤用かに関わらず、毎⽉月キューイングリクエスト100
万件まで無料料
! SQSリクエスト100万件につき0.50 USD
• 1件あたり0.0000005 USD
• 複数のメッセージを1つのリクエストとしてバッチ送信する場合も、単⼀一メッ
セージの時と同じ
! データ転送
• 受信(イン):0 USD
• 送信(アウト):
•
•
•
•
•
•
•
37
最初の1GB/⽉月 :0 USD
10TBまで /⽉月 :0.201 USD GBあたり
次の40TBまで /⽉月 :0.158 USD GBあたり
次の100TBまで /⽉月 :0.137 USD GBあたり
次の350TBまで /⽉月 :0.127 USD GBあたり
350TBを越える場合の価格はお問い合わせください
同⼀一リージョン内のSQSとEC2インスタンスのデータ転送は無料料
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 38. Amazon SNSとは
38
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 39. Simple Notification Service
! 柔軟で⾼高速なフルマネージドメッセージングサービス
! マルチプロトコルで簡単にメッセージを通知
! 安価な従量量課⾦金金制
2. マルチプロトコルで通知
1.Topicに
メッセージを送信
HTTP(S)
Amazon SNS
EMAIL
Publish
Publisher
Topic
SQS
Mobile Push
Subscriber
39
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 40. Amazon SNSの価格
! 無料料利利⽤用枠
•
•
•
•
モバイルプッシュ通知: 100万件
Email/Email-‐‑‒JSON: 1,000件
HTTP/HTTPS: 100,000件
Simple Queue Service(SQS): 無料料
•
•
•
•
モバイルプッシュ通知: 100万件あたり0.5 USD
Email/Email-‐‑‒JSON: 100,000件あたり2 USD
HTTP/HTTPS: 100万件あたり0.6 USD
Simple Queue Service(SQS): 無料料
•
•
受信(イン):0 USD
送信(アウト):
! SNSリクエスト単価(64 KB のチャンクごとに 1 リクエストとして課⾦金金)
! データ転送
•
•
•
•
•
•
•
最初の1GB/⽉月 :0 USD
10TBまで /⽉月 :0.201 USD GBあたり
次の40TBまで /⽉月 :0.158 USD GBあたり
次の100TBまで /⽉月 :0.137 USD GBあたり
次の350TBまで /⽉月 :0.127 USD GBあたり
350TBを越える場合の価格はお問い合わせください
同⼀一リージョン内のSNSとEC2インスタンスのデータ転送は無料料
40
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 41. Amazon SNSの特徴
! AWSの様々なサービスと連携して通知可能
• 利利⽤用例例
CloudWatch
設定したしきい値を超えた時の通知
SES
Bounce/Complaintのフィードバック通知
Elastic Transcoder
動画変換処理理完了了/失敗時の通知
AutoScaling
AutoScaling Groupの状態が変わった時の通知
CloudTrail
ログファイル配送に関する通知
41
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 42. SNSの動作イメージ
! 1. Topicの作成
! 2. TopicをSubscribe
! 3. Topicへ向けてメッセージをPublish
! 4. CloudWatchとの連携例例
42
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 43. 1. Topicの作成
! Topic Amazon Resource Name(ARN) を作成
43
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 44. 2. TopicのSubscribe
! プロトコルを選択: HTTP(S), Email, SQS, Application
! Endpointへのメッセージ送信にはConfirmが必要
44
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 45. 3. Topicへ向けてメッセージをPublish
! 作成したTopicに向けてブラウザからpublish
! 想定通りにメッセージが受け取れることを確認
プロトコルでEmailを選択した場合
45
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 46. 4. CloudWatchとの連携例例
! Alarmのしきい値を超えた場合のActionに通知を設定
46
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 47. 4. CloudWatchとの連携例例
! Alarmのしきい値を超えた場合に通知
プロトコルでEmailを選択した場合
47
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 48. Mobile Push (2013年年8⽉月リリース)
! シンプルにクロスプラットフォームなPUSH通知を実現
! 堅牢牢性と信頼性とスケーラビリティを兼備
Apple Devices
Google Devices
Amazon SNS
Mobile Push
Amazon Kindle Fire
Devices
48
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 49. Mobile Push (2013年年8⽉月リリース)
! Amazon SNS モバイルプッシュ通知セミナー資料料
http://aws.amazon.com/jp/aws-‐‑‒jp-‐‑‒introduction/
49
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 50. SQS/SNSを使った構成例例
50
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 53. バッファ&バッチオペレーション
! メッセージ ≒ オペレーション
! DBなどのサービスの前⾯面にキューを配置
! SQSのスケーリングを活⽤用して、安定したパフォーマン
スと⼤大量量のバックログを出⼒力力
• 例例
• S3上の少数ファイルに少量量の書き込み
• DynamoDBのバッファ
• RDSへのバルクインサート
53
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 57. まとめ
57
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 58. SQS/SNSのまとめ
! SQS
• キューイングシステムによる⾮非同期処理理を⽤用いることで、ス
ケーラブルなシステムを構築できる
• ⾼高い耐久性を持つキューイングシステムにはマネージドなSQS
を⽤用いると良良い
• SQSを使えば、⼤大量量のメッセージを最低1回の到達性で保障でき
る
• SQSのスケーリングによって⾼高いスループットを確保できる。
• ワークキューやバッファ&バッチオペレーションにも最適
• スパイクアクセスにも対応できる
! SNS
• マルチプロトコルのプッシュ通知を簡単に実現できる
• 柔軟で⾼高速なマネージドメッセージングサービス
58
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 59. Link
! Amazon SQS Document
• http://aws.amazon.com/jp/documentation/sqs/
! EAI Pattern
• http://www.eaipatterns.com/
! Amazon SNS Document
• http://aws.amazon.com/jp/documentation/sns/
! Amazon SNS Mobile Push セミナー資料料
• http://www.slideshare.net/AmazonWebServicesJapan/
20130917-‐‑‒aws-‐‑‒
meisterregenerateextrasnsmobilepushpublic
59
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 60. Appendix
60
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 61. SQS利利⽤用時のコードサンプル
AWS SDK for Java利利⽤用時
! AmazonSQSClientの作成
AmazonSQS sqs = new AmazonSQSClient(new PropertiesCredentials(xxx));
sqs.setEndpoint("http://sqs.ap-northeast-1.amazonaws.com");
! メッセージ送信
sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."));
! メッセージ受信
List<Message> messages =
sqs.receiveMessage(new ReceiveMessageRequest(myQueueUrl)).getMessages();
61
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.