40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/...

42
© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 40分で分かる DynamoDBでのApp開発 Follow @DynamoDB on

Upload: others

Post on 31-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

40分で分かる

DynamoDBでのApp開発Follow @DynamoDB on

Page 2: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

自己紹介

• 成田 俊 id:oranie

• 経歴– 前職のWeb系会社でインフラエンジニアを担当、主にMySQLや

Cassandraの運用などに携わる

– Cassandra summit JPN 2014, 2017 スピーカー登壇

• AWSJでの担当– NoSQLサービス、DynamoDBをメインに技術支援を担当

Page 3: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark

はじめに

Page 4: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

• Introduction to Amazon DynamoDB

• ちょっとしたAppをどう作ったか見てみる

• CIをどうするか

• まとめ

今日の流れ

Page 5: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

• あくまでも開発はじめ

• どのライブラリを使うかとか開発環境構築について• 好きな環境、好きな言語でaws-sdkをベースに好きなlibを

• Amazon DynamoDB Accelerator (DAX)クライアントを使うか

• 2019/9/30 時点でJava、Javascript、.NET、Python、Goがサポート

• データモデリングのベストプラクティス、Tips、パターン• Advanced Design Pattern Amazon DynamoDBなどのコンテンツを是非

今日細かく話さないこと

Page 6: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark

Introduction to Amazon DynamoDB

Page 7: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Amazon DynamoDBFully managed nonrelational database for any scale

高速で一貫した性能

事実上無制限のスループット

ストレージ容量も制限なし

通信と保存データの暗号化

柔軟な権限管理

PCI, HIPAA, FIPS140-2など認証

SLAの提供

メンテナンスフリー

サーバレス

Auto scaling

バックアップ/リストア

Global tables

Page 8: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Page 9: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

メンテナンスフリー

もしこれらを管理するコストが無ければ他に何が出来るか?

セキュリティ

OSパッチ適用

DBパッチ適用

アクセスコントロール

監査

暗号化

コンプライアンス対応

耐久性

サーバ, ラック, データセンタ維持

HW障害に伴うデータコピー

バックアップ・リストア

可用性

高可用性を実現する設計

モニタリング

クロスリージョンレプリケーション

性能

パフォーマンスチューニング

インデックス設計、作成

In-memory上でキャッシュ維持

拡張性

キャパシティプランニング

ホスト構築、投入作業

障害ホストの修復、撤去作業

Page 10: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Table構造Table

Items

Attributes

PartitionKey

Sort Key

必須Key-value access patternデータの分布を決定

オプションModel 1:N relationshipsQueryによる幅広い探索で利用

All items for key==, <, >, >=, <=“begins with”“between”“contains”“in”sorted resultscountstop/bottom N values

Page 11: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Partation keyのみ sort key なし

Partition Key

Page 12: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Partation keyと sort key両方あり

Sort KeyPartition Key

Page 13: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

NoSQL Workbench for Amazon DynamoDB(preview)

• Data modeler

• Visualizer

• Operation builder

の3つの機能を提供

GUIによるデータモデルの構築、視覚化、データ操作を支援

Win/Mac共に対応

オペレーションと同じ処理をするコードを生成する機能も対応(現在Python,Javascript,Javaに対応予定)

Page 14: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Operation builder

Build operationsでは

• Update Item

• Put Item

• Delete Item

• Query

• Scan

• TransactWriteItems

の操作が可能

それぞれの操作で必要なオプションを設定する事も可能

複数条件などの設定も可能

Page 15: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark

Sample App 解説

Page 16: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Page 17: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Page 18: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

デモアプリ全体構成

Amazon DynamoDBAWS LambdaAmazon API Gateway

AWS Cloud

※Chalice frameworkで作成

Page 19: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

チャットを実現させるために

• Commentの書き込み

• 最新25件のComment取得

• 全てのComment取得

• 時系列位置を指定して取得

という操作と、データ構造としてリアルタイムチャットなので時系列を表現するデータと名前、コメントデータがあれば簡単なリアルタイムチャットが実現出来る、と仮定

デモについて

Page 20: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

例えばRedisだったら

• Stream型 XADD、XRANGE、XREVRANGE を使うと今想定している機能は実現出来る

• 実装例 : xadd(chatroom_id, *, name, name_value, comment, comment_value):

• SortedSetでも実現は可能• 実装例:zadd(chatroom_id(key), unixtime(score),コメントデータ

(member))

• Primary keyはchatroom_idをkeyとして書き込む

実現するには?

Page 21: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

DynamoDB• RedisのStream型、SortedSet型と同じような形式をそのまま当てはめて表現することも出来るが、DynamoDBの特性を活かしより書き込みに対してスケーラブルなパターンを今回想定

実現するには?

Page 22: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Table

‘name’: name, ← PK‘time’: now, ← SortKey'comment': comment,'chat_room': chat_room

DynamoDB側のモデリング解説

GSI

‘name’: name, ‘time’: now, ← SortKey'comment': comment,'chat_room': chat_room ← PK

Tableの構造だけではchat_room指定のQuery探索が出来ないので別途GSIを作成

• あえてchatroom_idをpartition keyには採用しない• 今回はカーディナリティが高いnameをpartition keyに採用

Page 23: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

DynamoDB側のモデリング解説(Table)

Page 24: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

DynamoDB側のモデリング解説(GSI)

Page 25: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Table

‘name’: name, ‘time’: now, ← SortKey'comment': comment,'chat_room': chat_room ← PK

書き込みが少ない時は問題無いが、多くなってきた時に単一chat_roomへの負荷が多いとスロットリングに引っかかる可能性が

はじめからGSI側のモデリングで良いのでは?

Page 26: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Table

‘name’: name, ← PK‘time’: now, ← SortKey'comment': comment,'chat_room': chat_room

DynamoDB側のモデリング解説

GSI

‘name’: name, ‘time’: now, ← SortKey'comment': comment,'chat_room': chat_room ← PK

もし読み込みが増えてきたらGSIをDynamoDB StreamsとRedisを組み合わせて負荷

をオフロードする事で永続化と参照負荷対応を分離する、全文検索ならElasticsearchに入れるなどユースケース違いも対応

Page 27: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

• Commentの書き込み• putItem:念の為existing check付与

• 最新25件のComment取得• Query: chatroom id指定かつ時系列降順最大25件の条件付き

• 全てのComment取得• Query:chatroom idのみ指定

• 位置を指定して取得• Query:chatroom id 指定かつ時系列の指定時刻より少ないものを指定

実際のコードで確認を

デモについて簡単な解説

Page 28: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Redisなどとの組み合わせパターン

Amazon DynamoDB

Amazon ElastiCache

AWS Lambda

DynamoDB Streamsの更新データを取得

更新データを元にXADD or ZADDなど

コメント書き込み

コメント読み込みやPub/Sub

Page 29: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

def handler(event, context):

〜〜〜〜〜〜〜〜〜〜〜〜〜〜

new = event['Records'][0]['dynamodb']['NewImage’]

〜〜〜〜〜〜〜〜〜〜〜〜〜〜

userId = new['UserId']['S']

newScore = new['Score']['N']

gameId = new['GameId']['N']

t_stats = '{"userId":"%s", "newScore":"%s", "gameId":"%s"}¥n' % (userId,newScore,gameId,)

print(t_stats)

rc.zadd(gameId,newScore,userId)

print("ranking data is ",rc.zrange(gameId,0,1))

response = boto3.client('kinesis').put_record(

StreamName = 'ranking',

Data = t_stats,

PartitionKey = userId,

)

Lambda側実装例

handlerが受け取ったdynamodb streamsのeventから、新しいデータを取り出す

新しいデータをredisに書き込む

dynamodb streamsのeventを別のStreamsに投げる事でData Lakeに連携させる事も容易に可能

Page 30: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark

CIをどうするか

Page 31: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

CI(テスト)をどうするか?

App

DDBTable

Build project

Container

App

Build project

Container

OK ? NG ?

Page 32: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

CI(テスト)をどうするか?

• Table名をbuild projectごとに分ける

• 並列に実行されるbuildが少なければ問題なし

• 多いとアカウントあたりのlimitに抵触するかもしれない

• DynamoDBでの制限、API 固有の制限などに記述

AppDDBTable

Build project

Container

App

Build project

Container

DDBTable

Page 33: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

CI(テスト)をどうするか?

AppDDBLocal

Build project

Container Container

App+DDBLocal

App+DDBLocal

App+DDBLocal

App+DDB

Local development / pull req

STG release

Page 34: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

CI(テスト)をどうするか?

• CodeBuild、CircleCI、GithubActionsでそれぞれ同じテストを実行するためのサンプルを紹介

• AppとDynamoDB Localのコンテナを同一build projectで同時起動

• 同一コンテナ内にDynamoDB localプロセスを同居

AppDDBLocal

Build project

Build project

Container

Container Container

App DDBLocal

Page 35: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Confidential and Trademark

まとめ

Page 36: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

• DynamoDB開発でデータモデリング、Item操作の記述に迷ったらNoSQL Workbench for Amazon DynamoDBでテストを

• DynamoDB StreamsなどでDynamoDBと他のコンポーネントを組み合わせるなどもアーキテクティングでは重要

• CIなどDynamoDB Localで担保する部分との使い分けでよりリーズナブルに

まとめ

Page 37: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Page 38: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.

アンケートご協力のお願いお手持ちの受講票のアンケート用QRコードから

アンケートにお答えいただくと、記念品を差し上げております。

※イメージです

プレゼントの引き換えは、神田明神の会場出口付近(屋外)までお越しください

IT情報安全守護

Page 39: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

2019 年 10 月 1 日〜11 月 5 日開催amzn.to/AWSInnovateJP

申し込み受付中

AWS 最新アップデート / コンテナ / AIML / AWS アンチパターン /

認定試験対策講座など60セッション

オンラインで参加できるカンファレンス、全 60 セッション

10/9, 10/15 ライブ配信

AWS エキスパートへのQ&A

修了証明書の発行業務時間に視聴

Page 40: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

2019 年 10 月 1 日〜11 月 5 日開催amzn.to/AWSInnovateJP

Application DevelopmentContainerServerless

オンラインで参加できるカンファレンス、全 60 セッション

おすすめセッション

Page 41: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Thank you!

Page 42: 40分で分かる - pages.awscloud.com · aiml / aws アンチパターン/ 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全60

© 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.