Amazon DynamoDB is a fully managed, highly scalable distributed database service. In this technical talk, we show you how to use DynamoDB to build high-scale applications like social gaming, chat, and voting. We show you how to use building blocks such as secondary indexes, conditional writes, consistent reads, and batch operations to build the higher-level functionality such as multi-item atomic writes and join queries. We also discuss best practices such as index projections, item sharding, and parallel scan for maximum scalability.
39. GameId
Date
Host
Opponent
Status
d9bl3
2014-10-02
David
Alice
DONE
72f49
2014-09-30
Alice
Bob
PENDING
o2pnb
2014-10-08
Bob
Carol
IN_PROGRESS
b932s
2014-10-03
Carol
Bob
PENDING
ef9ca
2014-10-03
David
Bob
IN_PROGRESS
Games Table
40. GameId
Date
Host
Opponent
Status
d9bl3
2014-10-02
David
Alice
DONE
72f49
2014-09-30
Alice
Bob
PENDING
o2pnb
2014-10-08
Bob
Carol
IN_PROGRESS
b932s
2014-10-03
Carol
Bob
PENDING
ef9ca
2014-10-03
David
Bob
IN_PROGRESS
Games Table
43. Secondary Index
Bob
Opponent
Status
GameId
Date
Host
Alice
DONE
d9bl3
2014-10-02
David
Carol
IN_PROGRESS
o2pnb
2014-10-08
Bob
Bob
IN_PROGRESS
ef9ca
2014-10-03
David
Bob
PENDING
72f49
2014-10-03
Alice
Bob
PENDING
b932s
2014-09-30
Carol
44. Secondary Index
Opponent
Status
GameId
Date
Host
Alice
DONE
d9bl3
2014-10-02
David
Carol
IN_PROGRESS
o2pnb
2014-10-08
Bob
Bob
IN_PROGRESS
ef9ca
2014-10-03
David
Bob
PENDING
72f49
2014-10-03
Alice
Bob
PENDING
b932s
2014-09-30
Carol
Bob
45. Secondary Index
Opponent
Status
GameId
Date
Host
Alice
DONE
d9bl3
2014-10-02
David
Carol
IN_PROGRESS
o2pnb
2014-10-08
Bob
Bob
IN_PROGRESS
ef9ca
2014-10-03
David
Bob
PENDING
72f49
2014-10-03
Alice
Bob
PENDING
b932s
2014-09-30
Carol
Bob
SELECT * FROM Game
WHERE Opponent='Bob'
AND Status='PENDING'
46. Bob
SELECT * FROM Game
WHERE Opponent='Bob'
AND Status='PENDING'
ORDER BY Date?
Opponent
Status
GameId
Date
Host
Alice
DONE
d9bl3
2014-10-02
David
Carol
IN_PROGRESS
o2pnb
2014-10-08
Bob
Bob
IN_PROGRESS
ef9ca
2014-10-03
David
Bob
PENDING
72f49
2014-10-03
Alice
Bob
PENDING
b932s
2014-09-30
Carol
Secondary Index
47. SELECT * FROM Game
WHERE Opponent='Bob'
AND Status='IN_PROGRESS'
ORDER BY Date?
(hash)
(range)
(?)
48. 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
Bob
49. Secondary Index
Bob
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
SELECT * FROM Game
WHERE Opponent='Bob'
ORDER BY Date DESC
FILTER ON Status='PENDING'
(filtered out)
54. StatusDate
DONE_2014-10-02
IN_PROGRESS_2014-10-08
IN_PROGRESS_2014-10-03
PENDING_2014-09-30
PENDING_2014-10-03
Status
DONE
IN_PROGRESS
IN_PROGRESS
PENDING
PENDING
Date
2014-10-02
2014-10-08
2014-10-03
2014-10-03
2014-09-30
55. 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
56. 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
Secondary Index
Bob
SELECT * FROM Game
WHERE Opponent='Bob'
AND StatusDate STARTS_WITH 'PENDING'
61. David
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)
MessagesTable
62. 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
MessagesTable
SELECT *
FROM Messages
WHERE Recipient='David'
LIMIT 50
ORDER BY Date DESC
63. 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
MessagesTable
Cost of query?
SELECT *
FROM Messages
WHERE Recipient='David'
LIMIT 50
ORDER BY Date DESC
64. 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)
MessagesTable
Average item size = 256 KB
Large message bodies
Attachments?
David
SELECT *
FROM Messages
WHERE Recipient='David'
LIMIT 50
ORDER BY Date DESC
65. 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
MessagesTable
50 items ×256 KB each
SELECT *
FROM Messages
WHERE Recipient='David'
LIMIT 50
ORDER BY Date DESC
66. Items evaluated by query
Average item size
Conversion ratio
Eventually consistent reads
68. 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 Table
Messages Table
MsgId
Body
9d2b
…
3kf8
…
ct7r
…
afed
…
(Bulk message data)
(Message metadata, indexed by recipient)
69. 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 Table
Messages Table
MsgId
Body
9d2b
…
3kf8
…
ct7r
…
afed
…
David
1.Query Inbox: 1 RCU
(50 sequential items at 128 bytes)
70. 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 Table
Messages Table
MsgId
Body
9d2b
…
3kf8
…
ct7r
…
afed
…
David
1.Query Inbox: 1 RCU
2.BatchGetItem Messages: 1600 RCU
(50 separate items at 256 KB)
(50 sequential items at 128 bytes)
79. User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
Friends Table
HighScoresTable
User
Friend
Bob
Alice
Bob
Chuck
Alice
Bob
Alice
Chuck
Alice
Erin
80. User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
Friends Table
HighScoresTable
User
Friend
Bob
Alice
Bob
Chuck
Alice
Bob
Alice
Chuck
Alice
Erin
Alice
81. User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
Friends Table
HighScoresTable
User
Friend
Bob
Alice
Bob
Chuck
Alice
Bob
Alice
Chuck
Alice
Erin
1. Query
2. BatchGetItem
Alice
82.
83. User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
20
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
Table
FriendScoresTable
HighScoresTable
84. User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
20
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
User
H.S.
Friend
Bob
20
Chuck
Bob
1000
Alice
Alice
20
Chuck
Alice
470
Bob
Alice
580
Erin
Table
Local Secondary Index
FriendScoresTable
HighScoresTable
85. User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
20
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
User
HighScore
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
User
H.S.
Friend
Bob
20
Chuck
Bob
1000
Alice
Alice
20
Chuck
Alice
470
Bob
Alice
580
Erin
Table
Local Secondary Index
FriendScoresTable
HighScoresTable
Query
FROM FriendScores-Index
WHERE User=Alice, DESC, LIMIT 10
Alice
87. User
HighScore
Chuck
30
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
30
Alice
Erin
580
FriendScoresTable
HighScoresTable
1. UpdateItem
2. UpdateItem(s)
Chuck
88. User
HighScore
Chuck
30
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
FriendScoresTable
HighScoresTable
1. UpdateItem
2. UpdateItem(s)
Chuck
89. User
H.S.
Updating
Chuck
20
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
20
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
FriendScoresTable
HighScoresTable
truewhile updating the high score
90. User
H.S.
Updating
Chuck
30
true
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
20
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
FriendScoresTable
HighScoresTable
1. UpdateItem
Chuck
91. User
H.S.
Updating
Chuck
30
true
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
FriendScoresTable
HighScoresTable
Chuck
2. UpdateItem(s)
92. User
H.S.
Updating
Chuck
30
true
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
20
Alice
Erin
580
FriendScoresTable
HighScoresTable
Chuck
3. GetItem
93. User
H.S.
Updating
Chuck
30
true
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
30
Alice
Erin
580
FriendScoresTable
HighScoresTable
Chuck
4. UpdateItem(s)
94. User
H.S.
Updating
Chuck
30
Alice
1000
Bob
470
Dave
850
Erin
580
User
Friend
H.S.
Bob
Alice
1000
Bob
Chuck
30
Alice
Bob
470
Alice
Chuck
30
Alice
Erin
580
FriendScoresTable
HighScoresTable
5. UpdateItem
Chuck
103. User
H.S.
Chuck
20
Alice
100
HighScoresTable
Chuck
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
HighScoresStream
104. User
H.S.
Chuck
30
Alice
100
HighScoresTable
Chuck
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
HighScoresStream
105. User
H.S.
Chuck
30
Alice
100
HighScoresTable
Chuck
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
106. User
H.S.
Chuck
30
Alice
100
HighScoresTable
Chuck
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
107. User
H.S.
Chuck
30
Alice
100
HighScoresTable
User
Friend
H.S.
Bob
Chuck
20
Alice
Chuck
20
FriendScoresTable
Friend
User
Chuck
Alice
Chuck
Bob
Table
Global Secondary Index
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
Your
Kinesis-enabled app
108. User
H.S.
Chuck
30
Alice
100
HighScoresTable
User
Friend
H.S.
Bob
Chuck
20
Alice
Chuck
20
FriendScoresTable
Friend
User
Chuck
Alice
Chuck
Bob
Table
Global Secondary Index
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
Your
Kinesis-enabled app
109. User
H.S.
Chuck
30
Alice
100
HighScoresTable
User
Friend
H.S.
Bob
Chuck
30
Alice
Chuck
30
FriendScoresTable
Friend
User
Chuck
Alice
Chuck
Bob
Table
Global Secondary Index
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
Your
Kinesis-enabled app
110. User
H.S.
Chuck
30
Alice
100
HighScores Table
User
Friend
H.S.
Bob
Chuck
30
Alice
Chuck
30
FriendScores Table
Friend
User
Chuck
Alice
Chuck
Bob
Table
Global Secondary Index
Seq
User
Old
New
1
Chuck
10
20
2
Alice
80
90
3
Alice
90
100
4
Chuck
20
30
HighScoresStream
Your
Kinesis-enabled app
Streams Checkpoint Table
125. UserId
Candidate
Date
Alice
A
2013-10-02
Bob
B
2013-10-02
Eve
B
2013-10-02
Chuck
A
2013-10-02
RawVotesTable
Voter
126. UserId
Candidate
Date
Alice
A
2013-10-02
Bob
B
2013-10-02
Eve
B
2013-10-02
Chuck
A
2013-10-02
RawVotesTable
Segment
Votes
A_1
23
B_2
12
B_1
14
A_2
25
AggregateVotesTable
Voter
1. Record vote and de-dupe, retry
2. Increment candidate counter
127. UserId
Candidate
Date
Alice
A
2013-10-02
Bob
B
2013-10-02
Eve
B
2013-10-02
Chuck
A
2013-10-02
RawVotesTable
Segment
Votes
A_1
23
B_2
12
B_1
14
A_2
25
AggregateVotesTable
Voter