Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
40分で分かる DynamoDBでのApp開発
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
oranie
October 03, 2019
Technology
0
570
40分で分かる DynamoDBでのApp開発
DevDay Tokyo 2019での発表資料です。実際のセッションではサンプルコードと一緒に解説をした旨了承ください。
oranie
October 03, 2019
Tweet
Share
More Decks by oranie
See All by oranie
Amazon DynamoDB Deep Dive at AWS Loft Tokyo 2023/9/28
oranie
0
210
Amazon DynamoDB Deep Dive
oranie
0
79
Apache Cassandra on AWS
oranie
1
780
Other Decks in Technology
See All in Technology
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
会社紹介資料 / Sansan Company Profile
sansan33
PRO
15
400k
AIエージェントを開発しよう!-AgentCore活用の勘所-
yukiogawa
0
180
生成AIを活用した音声文字起こしシステムの2つの構築パターンについて
miu_crescent
PRO
3
210
[CV勉強会@関東 World Model 読み会] Orbis: Overcoming Challenges of Long-Horizon Prediction in Driving World Models (Mousakhan+, NeurIPS 2025)
abemii
0
140
GitHub Issue Templates + Coding Agentで簡単みんなでIaC/Easy IaC for Everyone with GitHub Issue Templates + Coding Agent
aeonpeople
1
250
配列に見る bash と zsh の違い
kazzpapa3
3
160
Claude_CodeでSEOを最適化する_AI_Ops_Community_Vol.2__マーケティングx_AIはここまで進化した.pdf
riku_423
2
610
AWS Network Firewall Proxyを触ってみた
nagisa53
1
240
SRE Enabling戦記 - 急成長する組織にSREを浸透させる戦いの歴史
markie1009
0
140
Kiro IDEのドキュメントを全部読んだので地味だけどちょっと嬉しい機能を紹介する
khmoryz
0
210
AI駆動開発を事業のコアに置く
tasukuonizawa
1
320
Featured
See All Featured
Site-Speed That Sticks
csswizardry
13
1.1k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
HDC tutorial
michielstock
1
390
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Building the Perfect Custom Keyboard
takai
2
690
Designing for Timeless Needs
cassininazir
0
130
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
280
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
830
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
160
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
180
Transcript
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. 40分で分かる DynamoDBでのApp開発 Follow @DynamoDB on
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. 自己紹介 • 成田 俊 id:oranie • 経歴 – 前職のWeb系会社でインフラエンジニアを担当、主にMySQLや Cassandraの運用などに携わる – Cassandra summit JPN 2014, 2017 スピーカー登壇 • AWSJでの担当 – NoSQLサービス、DynamoDBをメインに技術支援を担当
© 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 はじめに
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. • Introduction to Amazon DynamoDB • ちょっとしたAppをどう作ったか見てみる • CIをどうするか • まとめ 今日の流れ
© 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などのコンテンツ を是非 今日細かく話さないこと
© 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
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Amazon DynamoDB Fully managed nonrelational database for any scale 高速で一貫した性能 事実上無制限のスループット ストレージ容量も制限なし 通信と保存データの暗号化 柔軟な権限管理 PCI, HIPAA, FIPS140-2など認証 SLAの提供 メンテナンスフリー サーバレス Auto scaling バックアップ/リストア Global tables
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved.
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. メンテナンスフリー もしこれらを管理するコストが無ければ他に何が出来るか? セキュリティ OSパッチ適用 DBパッチ適用 アクセスコントロール 監査 暗号化 コンプライアンス対応 耐久性 サーバ, ラック, データセンタ維持 HW障害に伴うデータコピー バックアップ・リストア 可用性 高可用性を実現する設計 モニタリング クロスリージョンレプリケーション 性能 パフォーマンスチューニング インデックス設計、作成 In-memory上でキャッシュ維持 拡張性 キャパシティプランニング ホスト構築、投入作業 障害ホストの修復、撤去作業
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Table構造 Table Items Attributes Partition Key Sort Key 必須 Key-value access pattern データの分布を決定 オプション Model 1:N relationships Queryによる幅広い探索で利用 All items for key ==, <, >, >=, <= “begins with” “between” “contains” “in” sorted results counts top/bottom N values
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Partation keyのみ sort key なし Partition Key
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Partation keyと sort key両方あり Sort Key Partition Key
© 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に 対応予定)
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Operation builder Build operationsでは • Update Item • Put Item • Delete Item • Query • Scan • TransactWriteItems の操作が可能 それぞれの操作で必要なオプションを設定する 事も可能 複数条件などの設定も可能
© 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 解説
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. デモアプリ全体構成 Amazon DynamoDB AWS Lambda Amazon API Gateway AWS Cloud ※Chalice frameworkで作成
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. チャットを実現させるために • Commentの書き込み • 最新25件のComment取得 • 全てのComment取得 • 時系列位置を指定して取得 という操作と、データ構造としてリアルタイムチャットなの で時系列を表現するデータと名前、コメントデータがあれば 簡単なリアルタイムチャットが実現出来る、と仮定 デモについて
© 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として書き込む 実現するには?
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. DynamoDB • RedisのStream型、SortedSet型と同じような形式をそのまま当 てはめて表現することも出来るが、DynamoDBの特性を活かし より書き込みに対してスケーラブルなパターンを今回想定 実現するには?
© 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に採用
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. DynamoDB側のモデリング解説(Table)
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. DynamoDB側のモデリング解説(GSI)
© 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側のモデリングで良いのでは?
© 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に入れるなどユースケース違いも対応
© 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 指定かつ時系列の指定時刻より少ないものを 指定 実際のコードで確認を デモについて簡単な解説
© 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
© 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に連携させる事も容易に可能
© 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をどうするか
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. CI(テスト)をどうするか? App DDB Table Build project Container App Build project Container OK ? NG ?
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. CI(テスト)をどうするか? • Table名をbuild projectご とに分ける • 並列に実行されるbuildが少 なければ問題なし • 多いとアカウントあたりの limitに抵触するかもしれない • DynamoDBでの制限、API 固 有の制限などに記述 App DDB Table Build project Container App Build project Container DDB Table
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. CI(テスト)をどうするか? App DDB Local Build project Container Container App+DDB Local App+DDB Local App+DDB Local App+DDB Local development / pull req STG release
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. CI(テスト)をどうするか? • CodeBuild、CircleCI、 GithubActionsでそれぞれ 同じテストを実行するため のサンプルを紹介 • AppとDynamoDB Localの コンテナを同一build projectで同時起動 • 同一コンテナ内に DynamoDB localプロセス を同居 App DDB Local Build project Build project Container Container Container App DDB Local
© 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 まとめ
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. • DynamoDB開発でデータモデリング、Item操作の記述に 迷ったらNoSQL Workbench for Amazon DynamoDBでテス トを • DynamoDB StreamsなどでDynamoDBと他のコンポーネン トを組み合わせるなどもアーキテクティングでは重要 • CIなどDynamoDB Localで担保する部分との使い分けでより リーズナブルに まとめ
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved.
© 2019, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アンケートご協⼒のお願い お⼿持ちの受講票のアンケート⽤QRコードから アンケートにお答えいただくと、記念品を差し上げております。 ※イメージです プレゼントの引き換えは、神⽥明神の会場出⼝付近(屋外)までお越しください IT情報安全守護
2019 年 10 ⽉ 1 ⽇〜11 ⽉ 5 ⽇開催 amzn.to/AWSInnovateJP
申し込み受付中 AWS 最新アップデート / コンテナ / AIML / AWS アンチパターン / 認定試験対策講座など 60セッション オンラインで参加できるカンファレンス、全 60 セッション 10/9, 10/15 ライブ配 信 AWS エキスパートへの Q&A 修了証明書の発⾏ 業務時間に視聴
2019 年 10 ⽉ 1 ⽇〜11 ⽉ 5 ⽇開催 amzn.to/AWSInnovateJP
Application Development Container Serverless オンラインで参加できるカンファレンス、全 60 セッション おすすめセッション
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved. Thank you!
© 2019, Amazon Web Services, Inc. or its Affiliates. All
rights reserved.