1 / 156

Data Modeling and Best Practices on Amazon DynamoDB

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

opal
Download Presentation

Data Modeling and Best Practices on Amazon DynamoDB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Modeling and Best Practices on Amazon DynamoDB David Yanacek, Sr. Software Development Engineer, Amazon DynamoDB

  2. Plan • Basics • Basic Game State • Save Games • Social Gaming • Advanced • Social Gaming • Replication • Voting • Social Leaderboard • Global Leaderboard

  3. 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)

  4. Basic Game State conditional writes Basics

  5. Tic Tac Toe Basics

  6. Tic Tac Toe Alice Bob Your App DynamoDB Basics

  7. Tic Tac Toe Table Game Table Basics

  8. Tic Tac Toe Table Game Table Basics

  9. Tic Tac Toe Table Item Basics

  10. Tic Tac Toe Table Attribute Basics

  11. Tic Tac Toe Table Primary Key Basics

  12. Tic Tac Toe Table Binary Set String Number Basics

  13. Tic Tac Toe Table { "Data" : [ [ "X", null, "O" ], [ null, "O", null], [ "O", null, "X" ] ] } Basics

  14. State Transitions with Conditional Writes { "Id" : "abecd", "Players" : [ "Alice", "Bob" ], "State" : "STARTED", "Turn" : "Bob", "Top-Right" : "O" } Basics

  15. State Transitions with Conditional Writes Alice Bob DynamoDB Basics

  16. State Transitions with Conditional Writes Alice Bob UpdateItem: Top-Right = O Turn = Bob DynamoDB Basics

  17. State Transitions with Conditional Writes Alice Bob UpdateItem: Top-Left = X Turn = Alice DynamoDB Basics

  18. State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics

  19. State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics

  20. State Transitions with Conditional Writes Alice Bob (1) Bob (2) Bob (3) DynamoDB Basics

  21. State Transitions with Conditional Writes Bob (3) Bob (1) Bob (2) State : STARTED, Turn : Bob, Top-Right : O DynamoDB Basics

  22. 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

  23. 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

  24. Conditional Writes • Apply an update only if values are as expected • Otherwise reject the write Basics

  25. 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

  26. 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

  27. 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

  28. 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

  29. Alternative: Read / Modify / Write { Id : abecd, Players : [ Alice, Bob ], State : STARTED, Turn : Bob, Top-Right: O, Version : 2 } Basics

  30. 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

  31. Read / Modify / Write Example Quality: Good Value >= 0? Value: ? Quality: Bad Value < 0 Basics

  32. Read / Modify / Write Client 1 Version: 1 Value: 15 Time Client 2 Basics

  33. Read / Modify / Write Read Version: 1 Value: 15 Client 1 Version: 1 Value: 15 Version: 1 Value: 15 Read Time Client 2 Basics

  34. 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

  35. 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

  36. Read / Modify / Write with Deletes Client 1 Version: 1 Value: 15 Time Client 2 Basics

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. Save Games hash + range Save Games

  46. Save Games Save Games

  47. Primary Key Schemas Hash Key Schema Primary Key

  48. Primary Key Schemas Hash and Range Key Schema Primary Key Save Games

  49. Primary Key Schemas Primary Key Save Games

  50. 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

More Related