1.58k likes | 1.87k Views
Data Modeling and Best Practices on Amazon DynamoDB. David Yanacek, Sr. Software Development Engineer, Amazon DynamoDB. Plan. Basics Basic Game State Save Games Social Gaming Advanced Social Gaming Replication Voting Social Leaderboard Global Leaderboard. Plan. Basics
E N D
Data Modeling and Best Practices on Amazon DynamoDB David Yanacek, Sr. Software Development Engineer, Amazon DynamoDB
Plan • Basics • Basic Game State • Save Games • Social Gaming • Advanced • Social Gaming • Replication • Voting • Social Leaderboard • Global Leaderboard
Plan • Basics • Basic Game State (conditional writes) • Save Games (hash + range) • Social Gaming (secondary indexes) • Advanced • Social Gaming (transactions) • Replication (cross-region, cross-data-store) • Voting (write sharding) • Social Leaderboard (hash + range, complex transactions) • Global Leaderboard (scatter-gather query)
Basic Game State conditional writes Basics
Tic Tac Toe Basics
Tic Tac Toe Alice Bob Your App DynamoDB Basics
Tic Tac Toe Table Game Table Basics
Tic Tac Toe Table Game Table Basics
Tic Tac Toe Table Item Basics
Tic Tac Toe Table Attribute Basics
Tic Tac Toe Table Primary Key Basics
Tic Tac Toe Table Binary Set String Number Basics
Tic Tac Toe Table { "Data" : [ [ "X", null, "O" ], [ null, "O", null], [ "O", null, "X" ] ] } Basics
State Transitions with Conditional Writes { "Id" : "abecd", "Players" : [ "Alice", "Bob" ], "State" : "STARTED", "Turn" : "Bob", "Top-Right" : "O" } Basics
State Transitions with Conditional Writes Alice Bob DynamoDB Basics
State Transitions with Conditional Writes Alice Bob UpdateItem: Top-Right = O Turn = Bob DynamoDB Basics
State Transitions with Conditional Writes Alice Bob UpdateItem: Top-Left = X Turn = Alice DynamoDB Basics
State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics
State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics
State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics
State Transitions with Conditional Writes Bob (3) Bob (1) Bob (2) State : STARTED, Turn : Bob, Top-Right : O DynamoDB Basics
State Transitions with Conditional Writes Update: Turn : Alice Top-Left : X Update: Turn : Alice Mid : X Bob (3) Bob (1) Bob (2) Update: Turn : Alice Low-Right : X State : STARTED, Turn : Bob, Top-Right : O DynamoDB Basics
State Transitions with Conditional Writes Update: Turn : Alice Top-Left : X Update: Turn : Alice Mid : X Bob (3) Bob (1) Bob (2) Update: Turn : Alice Low-Right : X State : STARTED, Turn : Alice, Top-Right : O, Top-Left : X, Mid: X, Low-Right: X DynamoDB Basics
Conditional Writes • Apply an update only if values are as expected • Otherwise reject the write Basics
Conditional Writes UpdateItemId=abecd Updates: { Turn : Alice, Top-Left: X } Expected: { Turn : Bob, Top-Left : null, State : STARTED } Game Item { Id : abecd, Players : [ Alice, Bob ], State : STARTED, Turn : Bob, Top-Right: O } Basics
State Transitions with Conditional Writes Update: Turn : Alice Top-Left : X Expect: Turn : Bob Top-Left : null Update: Turn : Alice Mid : X Expect: Turn : Bob Mid : null Bob (3) Bob (1) Bob (2) Update: Turn : Alice Low-Right : X Expect: Turn : Bob Low-Right : null State : STARTED, Turn : Bob, Top-Right : O DynamoDB Basics
State Transitions with Conditional Writes Update: Turn : Alice Top-Left : X Expect: Turn : Bob Top-Left : null Update: Turn : Alice Mid : X Expect: Turn : Bob Mid : null Bob (3) Bob (1) Bob (2) Update: Turn : Alice Low-Right : X Expect: Turn : Bob Low-Right : null State : STARTED, Turn : Bob, Top-Right : O DynamoDB Basics
State Transitions with Conditional Writes Update: Turn : Alice Top-Left : X Expect: Turn : Bob Top-Left : null Update: Turn : Alice Mid : X Expect: Turn : Bob Mid : null Bob (3) Bob (1) Bob (2) Update: Turn : Alice Low-Right : X Expect: Turn : Bob Low-Right : null State : STARTED, Turn : Alice, Top-Right : O, Top-Left : X DynamoDB Basics
Alternative: Read / Modify / Write { Id : abecd, Players : [ Alice, Bob ], State : STARTED, Turn : Bob, Top-Right: O, Version : 2 } Basics
Pattern: Read / Modify / Write • Read item, remember Version (Vo) • Validate state transition • Construct new Item, with Version = V1 • Replace item • Expected: Version = Vo • If ConditionalCheckFailed, go to 1 Basics
Read / Modify / Write Example Quality: Good Value >= 0? Value: ? Quality: Bad Value < 0 Basics
Read / Modify / Write Client 1 Version: 1 Value: 15 Time Client 2 Basics
Read / Modify / Write Read Version: 1 Value: 15 Client 1 Version: 1 Value: 15 Version: 1 Value: 15 Read Time Client 2 Basics
Read / Modify / Write Read Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Client 1 Version: 1 Value: 15 Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Read Time Client 2 Basics
Read / Modify / Write Put If V=1 Read Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Client 1 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Put If V=1 Read Time Client 2 Basics
Read / Modify / Write with Deletes Client 1 Version: 1 Value: 15 Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Read Client 1 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Read Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Delete If V=2 Read Read Client 1 (Deleted) Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Read Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Put If Not Exists Delete If V=2 Read Read Read Client 1 (Deleted) Version: 1 Value: -20 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Read Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Put If Not Exists Delete If V=2 Read Read Read Client 1 (Deleted) Version: 1 Value: -20 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Version: 1 Value: 15 Read Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Put If Not Exists Delete If V=2 Read Read Read Client 1 (Deleted) Version: 1 Value: -20 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Read Time Client 2 Basics
Read / Modify / Write with Deletes Update Quality=Good If V=1 Put If Not Exists Delete If V=2 Read Read Read Client 1 (Deleted) Version: 1 Value: -20 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Version: 1 Value: 15 Version: 2 Value: 15 Quality: Good Read Put If V=1 Time Client 2 Basics
Read / Modify / Write • Version can be numeric or GUID • Version: 1 • Version: 550e8400-e29b-41d4-a716-446655440000 • If Numeric, choose random seed or introduce an “epoch” GUID • Version: 1 • Epoch: 550e8400-e29b-41d4-a716-446655440000 Basics
Read / Modify / Write with Epoch Delete if V=2, Epoch=ab4f Put Quality=Good If V=1, Epoch=ab4f Put Epoch=39da If Not Exists Read Read Read Client 1 (Deleted) Version: 1 Value: -20 Epoch: 39da Version: 2 Value: 15 Quality: Good Epoch: ab4f Version: 1 Value: 15 Epoch: ab4f Version: 1 Value: 15 Epoch: ab4f Version: 2 Value: 15 Quality: GoodEpoch: ab4f Read Put If V=1, Epoch=ab4f Time Client 2 Basics
Save Games hash + range Save Games
Save Games Save Games
Primary Key Schemas Hash Key Schema Primary Key
Primary Key Schemas Hash and Range Key Schema Primary Key Save Games
Primary Key Schemas Primary Key Save Games
Primary Key Schemas • Hash-only • Key/value lookups only • Hash and Range • Given a hash key value, query for items by range key • Items are sorted by range key within each hash key Save Games