Deep Dive: Amazon DynamoDB (db tech showcase 2016)
- 1. Deep Dive: Amazon DynamoDB
Takashi Narita
Solutions Architect
Amazon Web Services Japan K.K.
- 2. 自己紹���
• 成田 俊(なりた たかし)
– アマゾン ウェブ サービス ジャパン株式会社
– ソリューションアーキテクト
• 担当
– 主にWebプラットフォーム系のお客様の技術支援
• 経歴
– 主にWeb系会社でインフラエンジニアを担当
– Cassandra Summit 2014 JPN スピーカー
- 7. Gaming use case
Nexon delivers unparalleled mobile gaming with DynamoDB
Nexon is a leading South Korean video game developer
and a pioneer in the world of interactive entertainment.
By using AWS, we
decreased our initial
investment costs, and only
pay for what we use.
Chunghoon Ryu
Department Manager, Nexon
”
“ • Nexon ではゲームでDynamoDBを
Primary DBとして採用
• 韓国で最もヒットしたモバイルゲームでは
初日に200万の登録ユーザーがいた
• Nexonは快適なゲーム環境の為に10ms
以下の平均レスポンスを同時接続17万人
で実現出来る要件でDynamoDBを採用
- 8. Analytics use case
Expedia’s real-time analytics application uses DynamoDB
Expedia is a leader in the $1 trillion travel industry, with an
extensive portfolio that includes some of the world’s most
trusted travel brands.
With DynamoDB, we were up
and running in a less than
day, and there is no need for
a team to maintain.
Kuldeep Chowhan
Engineering Manager, Expedia
”
“ • Expediaではリアルタイム分析の為の
データ保存先としてDynamoDBを採
用
• この分析では200万件のメッセージが
日次で生成され保存する必要があっ
た。
• セットアップ、モニタリングの容易さ、
およびスケーリングがDynamoDBを
選ぶ際に重要な要因だった
- 11. • CreateTable
• UpdateTable
• DeleteTable
• DescribeTable
• ListTables
• GetItem
• PutItem
• UpdateItem
• DeleteItem
• Query
• Scan
• BatchGetItem
• BatchWriteItem
• Liststreams
• DescribeStream
• GetShardIterator
• GetRecords
Table API
Stream API
DynamoDB
- 12. AWS SDKs and CLI
• 各種言語むけのオフィシャルSDKやCLIを利用
Java Python PHP .NET Ruby nodeJS
iOS Android
Javascript
in the Browser AWS CLI
- 13. Data Types
• String (S)
• Number (N)
• Binary (B)
• String Set (SS)
• Number Set (NS)
• Binary Set (BS)
• Boolean (BOOL)
• Null (NULL)
• List (L)
• Map (M)
JSON用に定義
- 14. 00 55 A954 AA FF
Partition Table
• Partition key は単体でプライマリキーとして利用
• 順序を指定しないハッシュインデックスを構築するためのキー
• テーブルは、性能を確保するために分割(パーティショニング)される場合あり
00 FF
Id = 1
Name = Jim
Partition (1) = 7B
Id = 2
Name = Andy
Dept = Engg
Partition (2) = 48
Id = 3
Name = Kim
Dept = Ops
Partition (3) = CD
Key Space
- 15. データは3箇所にレプリケーション
Id = 2
Name = Andy
Dept = Engg
Id = 3
Name = Kim
Dept = Ops
Id = 1
Name = Jim
Id = 2
Name = Andy
Dept = Engg
Id = 3
Name = Kim
Dept = Ops
Id = 1
Name = Jim
Id = 2
Name = Andy
Dept = Engg
Id = 3
Name = Kim
Dept = Ops
Id = 1
Name = Jim
Replica 1
Replica 2
Replica 3
Partition 1 Partition 2 Partition N
- 16. Partition-Sort Table
• Partition + Sortでプライマリキーとすることもできる(RDBMSでいう複合キー)
• Partition keyに該当する複数のデータの順序を保証するためにSort keyが使われる
• Partition Keyの数に上限はありません
(Local Secondary Indexesを使用時は上限あり)
00:0 FF:∞
Partition (2) = 48
Customer# = 2
Order# = 10
Item = Pen
Customer# = 2
Order# = 11
Item = Shoes
Customer# = 1
Order# = 10
Item = Toy
Customer# = 1
Order# = 11
Item = Boots
Partition (1) = 7B
Customer# = 3
Order# = 10
Item = Book
Customer# = 3
Order# = 11
Item = Paper
Partition (3) = CD
55 A9:∞54:∞ AA
Partition 1 Partition 2 Partition 3
- 19. Local Secondary Index (LSI)
• Sort key以外に絞り込み検索を行うkeyを持つことができる
• Partition keyが同一で、他のアイテムからの検索のために利用
• すべての要素(テーブルとインデックス)の合計サイズを、各ハッ
シュキーごとに 10 GB に制限
A1
(PK)
A3
(Sort)
A2
(table key)
A1
(PK)
A2
(Sort)
A3 A4 A5
LSIs A1
(PK)
A4
(Sort)
A2
(table key)
A3
(projected)
Table
KEYS_ONLY
INCLUDE A3
A1
(PK)
A5
(Sort)
A2
(table key)
A3
(projected)
A4
(projected)
ALL
- 21. Global Secondary Index (GSI)
• Partition Key属性の代わりとなる
• Partition Keyをまたいで検索を行うためのインデックス
A1
(PK)
A2 A3 A4 A5
GSIs
A5
(PK)
A4
(Sort)
A1
(table key)
A3
(projected)
Table
INCLUDE A3
A4
(PK)
A5
(Sort)
A1
(table key)
A2
(projected)
A3
(projected) ALL
A2
(PK)
A1
(table key)
KEYS_ONLY
- 26. スループット
• テーブルレベルによってプロビジョニング
– Write Capacity Units (WCU)
• 1 秒あたりの項目書き込み回数 x 項目のサイズ (1 KB ブロック)
– Read Capacity Units (RCU)
• 1 秒あたりの読み込み回数 x 項目のサイズ (4 KB ブロック)
• 結果整合性のある読み込みをする場合はスループットが 2 倍
• 読み込みと書き込みのスループットはそれぞれ独立
WCURCU
- 28. パーティショニング算出例
# 𝑜𝑓 𝑃𝑎𝑟𝑡𝑖𝑡𝑖𝑜𝑛𝑠 =
M NO
PQ NO
= 0.8 = 1
(𝑓𝑜𝑟 𝑠𝑖𝑧𝑒)
# 𝑜𝑓 𝑃𝑎𝑟𝑡𝑖𝑡𝑖𝑜𝑛𝑠
(𝑓𝑜𝑟 𝑡ℎ𝑟𝑜𝑢𝑔ℎ𝑝𝑢𝑡)
=
RQQQSTU
VQQQ WXY
+
RQQZTU
PQQQ [XY
= 2.17 = 3
Table size = 8 GB, RCUs = 5000, WCUs = 500
(𝑡𝑜𝑡𝑎𝑙)
RCUs per partition = 5000/3 = 1666.67
WCUs per partition = 500/3 = 166.67
Data/partition = 8/3 = 2.66 GB
RCUとWCU の値は均一に各パー
テションに割り当てられます
• スループット
• ストレージサイズ
• 大きいほうを採用
- 34. Burst capacity is built-in
0
400
800
1200
1600
CapacityUnits
Time
Provisioned Consumed
使われなかったキャパシティ分を保持
保持していたキャパシティを使用
バーストキャパシティ: 300秒
(1200 × 300 = 3600 CU)
- 35. Burst capacity may not be sufficient
0
400
800
1200
1600
CapacityUnits
Time
Provisioned Consumed Attempted
バーストキャパシティ: 300秒
(1200 × 300 = 3600 CU)
超過したリクエスト
バーストキャパシティの機能に依存しないように。
- 37. 1:1 リレーション or キー・バリュー型
• Partition keyを使ったテーブルまたはGSI
• GetItem かBatchGetItem APIを使用
例: UserIDやEmailから要素を抽出する場合
Users Table
Partition key Attributes
UserId = bob Email = bob@gmail.com, JoinDate = 2011-11-15
UserId = fred Email = fred@yahoo.com, JoinDate = 2011-12-01
Users-email-GSI
Partition key Attributes
Email = bob@gmail.com UserId = bob, JoinDate = 2011-11-15
Email = fred@yahoo.com UserId = fred, JoinDate = 2011-12-01
- 38. 1:N リレーション or 親子関係
• Partition key とSort key を使ったテーブル、GSI
• Query APIを使ってアクセス
例:1ユーザでN個のゲームをプレイしている場合
User-Games
Partition Key Sort key Attributes
UserId = bob GameId = Game1 HighScore = 10500, ScoreDate = 2011-10-20
UserId = fred GameId = Game2 HIghScore = 12000, ScoreDate = 2012-01-10
UserId = bob GameId = Game3 HighScore = 20000, ScoreDate = 2012-02-12
- 39. N:M リレーション
• table と GSI を使用してPartition key とSort key の
要素をスイッチして設計
• Query API を用いてアクセス
例: 1ユーザが複数のゲームをプレイし,
1ゲームで複数のプレイヤーがゲームをしている場合
User-Games-Table
Partition Key Sort key
UserId = bob GameId = Game1
UserId = fred GameId = Game2
UserId = bob GameId = Game3
Game-Users-GSI
Partition Key Sort key
GameId = Game1 UserId = bob
GameId = Game2 UserId = fred
GameId = Game3 UserId = bob
- 40. Document (JSON)
• 新データタイプ (M, L, BOOL,
NULL) としてJSONをサポート
• Document SDKs
– 単純なプログラミングモデル
– JSONから、JSONへの変換
– Java, JavaScript, Ruby, .NET
Javascript DynamoDB
string S
number N
boolean BOOL
null NULL
array L
object M
- 43. Time Series Tables
Events_table_2015_June
Event_id
(Partition
key)
Timestamp
(Sort key)
Attribute1 …. Attribute N
Events_table_2015_May
Event_id
(Partition
key)
Timestamp
(Sort key)
Attribute1 …. Attribute N
Events_table_2015_April
Event_id
(Partition
key)
Timestamp
(Sort key)
Attribute1 …. Attribute N
Events_table_2015_March
Event_id
(Partition
key)
Timestamp
(Sort key)
Attribute1 …. Attribute N
RCU = 1000
WCU = 100
RCU = 10000
WCU = 10000
RCU = 100
WCU = 10
RCU = 10
WCU = 1
Current table
Older tables
HotdataColddata
ホットデータとコールドデータは混在させない
アーカイブしたコールドデータはS3へ
- 46. Recipient Date Sender Message
David 2014-10-02 Bob …
… 48 more messages for David …
David 2014-10-03 Alice …
Alice 2014-09-28 Bob …
Alice 2014-10-01 Carol …
大小のデータが混在
(Many more messages)
David
Messages Table
50 items × 平均 256 KB
大きなメッセージボディーを
格納
SELECT *
FROM Messages
WHERE Recipient='David'
LIMIT 50
ORDER BY Date DESC
Inbox
- 48. Recipient Date Sender Subject MsgId
David 2014-10-02 Bob Hi!… afed
David 2014-10-03 Alice RE: The… 3kf8
Alice 2014-09-28 Bob FW: Ok… 9d2b
Alice 2014-10-01 Carol Hi!... ct7r
大きいデータを分けて配置
Inbox-GSI Messages Table
MsgId Body
9d2b …
3kf8 …
ct7r …
afed …
David
1. Query Inbox-GSI: 1 RCU
2. BatchGetItem Messages: 1600 RCU
(50 separate items at 256 KB)
(50 sequential items at 128 bytes)
均等に大きいアイテムを読むように配置
(Recipientをindex,
Message メタデータを格納)
- 53. What is DynamoDB Stream?
It is a stream of updates
Scales with your table
DynamoDB StreamDynamoDB
- 55. View Type Destination
Old Image – 更新前の情報 Name = John, Destination = Mars
New Image – 更新後の情報 Name = John, Destination = Pluto
Old and New Images Name = John, Destination = Mars
Name = John, Destination = Pluto
Keys Only Name = John
View types
更新情報(Name = John, Destination = Mars)
⇒(Name = John, Destination = Pluto)
- 56. Stream
Table
Partition 1
Partition 2
Partition 3
Partition 4
Partition 5
テーブル
Shard 1
Shard 2
Shard 3
Shard 4
KCL
Worker
KCL
Worker
KCL
Worker
KCL
Worker
Amazon Kinesis Client
Library Application
DynamoDB
クライアント
アプリケーション 更新
DynamoDB Stream and
Amazon Kinesis Client Library
- 57. DynamoDB Stream
Open Source Cross-
Region Replication Library
Asia Pacific (Sydney) EU (Ireland) Replica
US East (N. Virginia)
クロスリージョンレプリケーション