<<

DynamoDB deep dive: Advanced design patterns

© 2020, , Inc. or its Affiliates. 自己紹介

成田 俊 id:oranie

経歴 • 前職のWeb系会社でインフラエンジニアを担当、主にMySQLや Cassandra運用などに携わる

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

© 2020, Amazon Web Services, Inc. or its Affiliates. Agenda

• Overview of Amazon DynamoDB • Scaling NoSQL • NoSQL data modeling • 正規化と非正規化 • デザインパターン • 複合key, 階層的構造のデータ, リレーショナルなデータの表現 • 実際のアプリケーションにおけるモデリング

© 2020, Amazon Web Services, Inc. or its Affiliates. Amazon DynamoDB あらゆる規模に対応する高速で柔軟なkey-value データベースサービス

Performance Serverless architecture Enterprise Global replication at scale security

あらゆる規模で一貫した1桁 ハードウェアのプロビジョニン すべてのデータを暗号化 複数の AWS リージョン間で ミリ秒の応答時間。事実上無 グ、ソフトウェアのパッチ適用、 AWS IAMとの完全統合によ 簡単にテーブルをレプリ 制限のスループットでアプリ アップグレードなし、自動ス り、堅牢なセキュリティを ケートすることで、ローカ ケーションを構築 ケールアップまたはスケールダ 実現 ルデータにすばやくアクセ ウン、データの継続的なバック スできるグローバルアプリ アップ ケーションを構築可能

© 2020, Amazon Web Services, Inc. or its Affiliates. © 2020, Amazon Web Services, Inc. or its Affiliates. メンテナンスフリー

もしこれらを管理するコストが無ければ他に何が出来るか? セキュリティ 可用性 OSパッチ適用 高可用性を実現する設計 DBパッチ適用 モニタリング アクセスコントロール クロスリージョンレプリケーション 監査 性能 暗号化 パフォーマンスチューニング コンプライアンス対応 インデックス設計、作成 耐久性 In-memory上でキャッシュ維持 サーバ, ラック, データセンタ維持 拡張性 HW障害に伴うデータコピー キャパシティプランニング バックアップ・リストア ホスト構築、投入作業 障害ホストの修復、撤去作業

© 2020, Amazon Web Services, Inc. or its Affiliates. Table Table GSI1PK GSI2PK GSI3PK Items

GSI1PK Attributes GSI1PK GSI2PK

Partition Sort All items for key ==, <, >, >=, <= key key “begins with” “between” “contains” “in” 必須 オプション sorted results Key-value アクセスパターン 1:Nの関係性を表現 counts を実現 データの分布を決定 豊富なクエリでの条件指定が可能 top/bottom N values

© 2020, Amazon Web Services, Inc. or its Affiliates. Partition overloading -> 一つのPKに複数の定義を紐付ける

同一PKだが、SKで複数のコンテキストを分割 するため、色んな値を入れられる様に汎用的な key名を使用

SELECT * WHERE PK=Customer_1 AND SK > 2019-10-29

© 2020, Amazon Web Services, Inc. or its Affiliates. Secondary indexes Online indexing

PK/SKだけではない 二次的なアクセスパターンもサポート TableのPartition keyを横断して探索可能 複合

RCU/WCUは元のテーブル A1 Table A2 A3 A4 A5 とは独立して管理 (partition)

A2 A1 KEYS_ONLY (partition) (itemkey)

Indexes A5 A4 A1 A3 INCLUDE A3 (partition) (sort) (item key) (projected)

A4 A5 A1 A2 A3 ALL (partition) (sort) (item key) (projected) (projected) © 2020, Amazon Web Services, Inc. or its Affiliates. Partition/shard keys in NoSQL Partition/shard key は順不同なハッシュインデックスを生成するのに利用 テーブルをスケールさせるため内部ではパーティションで分割

Id = 1 Id = 2 Id = 3 Name = Jim Name = Andy Name = Kim Dept = Eng Dept = Ops Hash (1) = 7B Hash (2) = 48 Hash (3) = CD

00 54 55 Key space A9 AA FF

© 2020, Amazon Web Services, Inc. or its Affiliates. Partition、Scaleの仕組みについて

https://www.slideshare.net/AmazonWebServices/amazon-dynamodb-under- the-hood-how-we-built-a-hyperscale-database-dat321-aws-reinvent-2018

© 2020, Amazon Web Services, Inc. or its Affiliates. Write sharding GSI上の高密度集約をサポートするためのソル トインデックス付きキー

© 2020, Amazon Web Services, Inc. or its Affiliates. Index overloading

複数のアクセスパターンに対応したインデック スを使用するために、汎用的な名前のAttribute を使用する

© 2020, Amazon Web Services, Inc. or its Affiliates. Index overloading

SELECT * WHERE PK=ONLINE#0 AND SK=US … SELECT * WHERE PK=ONLINE#N AND SK=US

SELECT * WHERE PK=B07G6CQQYG#0 AND SK=PROCESSING … SELECT * WHERE PK=B07G6CQQYG#N AND SK=PROCESSING

© 2020, Amazon Web Services, Inc. or its Affiliates. Scaling NoSQL “We are stuck with technology when what we really want is just stuff that works.”

© 2020, Amazon Web Services, Inc. or its Affiliates. What bad NoSQL looks like

Heat Partition

Time

© 2020, Amazon Web Services, Inc. or its Affiliates. Getting the most out of DynamoDB throughput

I/O リクエストを均等に分散しないパーティションキー設計では、「ホット」パー ティションが作成される場合があります。これにより、スロットリングが発生した り、プロビジョニングされた I/O キャパシティーを効率的に使用されないことが あります —DynamoDB Developer Guide

Space: Access is evenly spread over the key space 空間内に均等にアク セスが来るように Time: Requests arrive evenly spaced in time 等間隔なアクセスが望ましい

© 2020, Amazon Web Services, Inc. or its Affiliates. Much better picture

© 2020, Amazon Web Services, Inc. or its Affiliates. PK分散の補足

パーティションキーの値 均一性

ユーザー ID(アプリケーションに多くのユーザーがある場合) 良い

ステータスコード(可能性のあるステータスコードが少しだけある 不良 場合) 項目の作成日。直近の期間 (日、時、分) に切り上げられます。 不良

デバイス ID(各デバイスが比較的類似した間隔でデータにアクセス 良い する場合). デバイス ID (追跡中のデバイスが多数あり、そのうちの 1 つのデバ 不良 イスが他のすべてのデバイスよりもずっと人気がある場合)

https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide /bp-partition-key-design.html

© 2020, Amazon Web Services, Inc. or its Affiliates. Auto scaling

Throughput automatically adapts to your actual traffic

Without auto scaling With auto scaling

© 2020, Amazon Web Services, Inc. or its Affiliates. Performance at any scale

High request volume Consistent low latency

テーブルあたり何百万 秒ものリクエスト 1 / Millisecond レベルの一貫し たレイテンシ

© 2020, Amazon Web Services, Inc. or its Affiliates. Global-scale events: Elastic is the new normal

Super Bowl Peak Consumed Capacity

Provisioned Capacity Write capacity units/sec

© 2020, Amazon Web Services, Inc. or its Affiliates. NoSQL data modeling “データがあるならデータを見よう 意見しかな いなら、私の意見で”

© 2020, Amazon Web Services, Inc. or its Affiliates. SQL vs. NoSQL design pattern

© 2020, Amazon Web Services, Inc. or its Affiliates. Ad hoc “joins” in SQL

SELECT * FROM PRODUCTS INNER JOIN BOOKS ON productId = productId WHERE name = “Book Title” SELECT * FROM PRODUCTS INNER JOIN ALBUMS ON productId = productId INNER JOIN TRACKS ON albumId = albumId WHERE name = “Album Title” SELECT * FROM PRODUCTS INNER JOIN VIDEOS ON productId = productId INNER JOIN ACTORVIDEO ON videoId = videoId INNER JOIN ACTORS ON actorId = actorId WHERE name = “Movie Title”

© 2020, Amazon Web Services, Inc. or its Affiliates. Modeled “joins” in NoSQL

SELECT * WHERE PK=“Book Title”

SELECT * WHERE PK=“Album Title”

SELECT * WHERE PK=“Movie Title”

© 2020, Amazon Web Services, Inc. or its Affiliates. Modeled “joins” in NoSQL

Swap PK and SK on index

SELECT * WHERE SK=“Author Name”

SELECT * WHERE SK=“Song Title”

SELECT * WHERE SK=“Actor Name”

SELECT * WHERE SK=“Director Name”

SELECT * WHERE SK=“Musician”

© 2020, Amazon Web Services, Inc. or its Affiliates. 複雑なクエリをどう実現するか “Computers are useless. They can only give you answers.”

© 2020, Amazon Web Services, Inc. or its Affiliates. DynamoDBにとって複雑なクエリとは

例えば • ある単語を元に含まれるItemを検索するような全文検索 • 全てのItemを元に様々な集計処理を行いレポートを作成 • 複数のtable など 現在これらの機能は組み込まれていないため、もしDynamoDBだけで実現する ならApp側で多くの実装が必要となるケースがある

© 2020, Amazon Web Services, Inc. or its Affiliates. Serverless & event driven architecture

DynamoDB streams

データ書き込みイベント

リアルタイム 集計結果の保存

Amazon ES

変更通知 Kinesis Firehose AWS Lambda Athena (Parquet)

© 2020, Amazon Web Services, Inc. or its Affiliates. 複合key(Composite keys)

“Hierarchies are celestial. In hell all are equal.”

© 2020, Amazon Web Services, Inc. or its Affiliates. 複数の値をソート・フィルタ

Partition key Sort key Bob

Secondary index

Opponent Date GameId Status Host Alice 2014-10-02 d9bl3 DONE David Carol 2014-10-08 o2pnb IN_PROGRESS Bob Bob 2014-09-30 72f49 PENDING Alice Bob 2014-10-03 b932s PENDING Carol Bob 2014-10-03 ef9ca IN_PROGRESS David

© 2020, Amazon Web Services, Inc. or its Affiliates. Approach 1: Query filter

SELECT * FROM Game

WHERE Opponent='Bob' Bob ORDER BY Date DESC FILTER ON Status='PENDING' Secondary index Opponent Date GameId Status Host Alice 2014-10-02 d9bl3 DONE David Carol 2014-10-08 o2pnb IN_PROGRESS Bob Bob 2014-09-30 72f49 PENDING Alice Bob 2014-10-03 b932s PENDING Carol Bob 2014-10-03 ef9ca IN_PROGRESS David (Filtered out)

© 2020, Amazon Web Services, Inc. or its Affiliates. Approach 2: 複合key(Composite key)

Status Date StatusDate DONE 2014-10-02 DONE_2014-10-02 IN_PROGRESS 2014-10-08 IN_PROGRESS_2014-10-08 IN_PROGRESS + 2014-10-03 = IN_PROGRESS_2014-10-03 PENDING 2014-10-03 PENDING_2014-09-30 PENDING 2014-09-30 PENDING_2014-10-03

© 2020, Amazon Web Services, Inc. or its Affiliates. Approach 2: 複合key(Composite key)

Partition key Sort key

Secondary index

Opponent StatusDate GameId Host Alice DONE_2014-10-02 d9bl3 David Carol IN_PROGRESS_2014-10-08 o2pnb Bob Bob IN_PROGRESS_2014-10-03 ef9ca David Bob PENDING_2014-09-30 72f49 Alice Bob PENDING_2014-10-03 b932s Carol

© 2020, Amazon Web Services, Inc. or its Affiliates. Approach 2: 複合key(Composite key)

SELECT * FROM Game WHERE Opponent='Bob' Bob AND StatusDate BEGINS_WITH 'PENDING'

Secondary index

Opponent StatusDate GameId Host Alice DONE_2014-10-02 d9bl3 David Carol IN_PROGRESS_2014-10-08 o2pnb Bob Bob IN_PROGRESS_2014-10-03 ef9ca David Bob PENDING_2014-09-30 72f49 Alice Bob PENDING_2014-10-03 b932s Carol

© 2020, Amazon Web Services, Inc. or its Affiliates. Modeling relational data

“Dude, where’s my lookup table?”

© 2020, Amazon Web Services, Inc. or its Affiliates. 複数のリレーショナルを必要とするモデリング

© 2020, Amazon Web Services, Inc. or its Affiliates. The table

© 2020, Amazon Web Services, Inc. or its Affiliates. The table

© 2020, Amazon Web Services, Inc. or its Affiliates. The index schema (GSI1)

© 2020, Amazon Web Services, Inc. or its Affiliates. The index schema (GSI2)

© 2020, Amazon Web Services, Inc. or its Affiliates. The index schema (GSI3)

© 2020, Amazon Web Services, Inc. or its Affiliates. 各処理と探索条件まとめ

© 2020, Amazon Web Services, Inc. or its Affiliates. Item分割 “To understand is to perceive patterns.”

© 2020, Amazon Web Services, Inc. or its Affiliates. アクセスパターンによる問題

• 保険見積もりサービス

• すべてのバージョンを保存

• 1つの見積もりにつき200 以上の属性

• 平均Itemサイズ50KB

• ピーク時800/分レベルの アクセス

• 1000 WCUが必要だった

© 2020, Amazon Web Services, Inc. or its Affiliates. Optimized for writes

• カテゴリが更新されたとき のバージョン項目

• 照会されたときにすべての バージョンを送信

• クライアント側ロジックに よる処理

• 50 WCU で済むように改善

© 2020, Amazon Web Services, Inc. or its Affiliates. NoSQL Workbench for DynamoDB

• AWSスペシャリストSAチームが 設計したツールを使用する • データのモデル化、デザインの 可視化、コードの生成 • https://docs.aws.amazon.com/a mazondynamodb/latest/develo perguide/workbench.html

© 2020, Amazon Web Services, Inc. or its Affiliates. まとめ

• NoSQLは非リレーショナルを意味しない • ER図での整理、アプリケーション機能条 件の整理はNoSQLでも重要 • RDBMS/DWHなどとの併用はNGではない

• OLAPにRDBMS/DWHを使用する • OLTPなワークロードにはNoSQLを使用し スケールを実現

© 2020, Amazon Web Services, Inc. or its Affiliates. © 2020, Amazon Web Services, Inc. or its Affiliates. Learn databases with AWS Training and Certification Resources created by the experts at AWS to help you build and validate database skills

25+ free digital training courses cover topics and services related to databases, including: • • Amazon ElastiCache • • Amazon DocumentDB • Amazon RDS • Amazon DynamoDB

Validate expertise with the new AWS Certified Database

Visit aws.training

© 2020, Amazon Web Services, Inc. or its Affiliates. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. Get started today

Major languages supported through the AWS SDK, such as Java, .NET, See DynamoDB.com Python, Node.js, and Go

Use AWS Database Migration Service Follow @DynamoDB to migrate your data to DynamoDB on

© 2020, Amazon Web Services, Inc. or its Affiliates. Appendix

DynamoDBの過去セッション Amazon Blackbelt webinar https://aws.amazon.com/jp/blogs/news/webinar-bb-dynamodb-advanced- design-pattern-2018/

Amazon DynamoDB Deep Dive | AWS Summit Tokyo 2019 https://www.youtube.com/watch?v=16RYHfe89WY

Re:Invent 2019 session DynamoDB セッション紹介ブログ https://aws.amazon.com/jp/blogs/news/amazon-dynamodb-related-videos- ©and 2020, Amazon-slide Web Services,-decks Inc. or its Affiliates.-from -aws-reinvent-2019/ Appendix

aws.training以外のオンライントレーニング Linux Academy Amazon DynamoDB Deep Dive https://linuxacademy.com/course/amazon-dynamo-db-deep-dive/ Amazon DynamoDB Data Modeling https://linuxacademy.com/course/amazon-dynamo-db-data-modeling/ 他にもDynamoDBを利用したAppなどのトレーニングあり

© 2020, Amazon Web Services, Inc. or its Affiliates. Appendix

AWS sample repository https://github.com/aws-samples

2019 DevDayで公開したサンプル https://github.com/aws-samples/amazon-dynamodb-chat-sample

他にも様々なユースケースや言語のサンプルがあるのでまずは動くもので勉強し たいなどには最適

© 2020, Amazon Web Services, Inc. or its Affiliates.