22. テーブル設計のための基礎知識(2/2)
• Item
– RDBで言ういわゆるレコード
– 1つ以上のAttributeを持つ
• Attributes
– データの中身。RDBで言うカラム。
– Hash key, Range keyに該当するAttributes以外は事前定義不要
– レコード間でAttributesが不揃いであっても問題ない
• Attributesの型
– String
– Number
– Binary
– Set of String
– Set of Number
– Set of Binary
33. ゲームの行動履歴管理データベース
User
(Hash)
Timestamp
(Range)
Opponent Result
Alice 2014-02-21 12:21:20 Bob Lost
Alice 2014-02-21 12:42:01 Bob Won
Alice 2014-02-24 09:48:00 Dan Won
Alice 2014-02-25 16:21:11 Charlie Won
Battle History
自分のバトル履歴を確認するケースを想定
– Userに自分(Alice)を指定し、更にTimestampが7日以内のデータ
をクエリしたりできる
Charlie
02-25 16:21
Won!
Your Battle History
Dan
02-24 09:48
Won!
Alice
02-21 12:42
Won!
35. ソーシャル画像共有アプリ
Home My Posts My Profile
Bob
Steak!
10:18
Carol
BBQ! w/Alice
10:12
Dan
Riajuee…
10:11
Alice
Beer!
10:21
Alice
BBQ! w/Carol
10:12
Alice
Starting BBQ!
10:09
Name:
Alice
Mail: foo
Profile: some texts
37. User
(Hash)
Nicknames
Bob [ Rob, Bobby ]
Alice [ Allie ]
Carol [ Caroline ]
Dan [ Daniel, Danny ]
友達一覧を取得
Users Table
Item
Attribute
(string, number, binary, set)
Primary Key
(Hash)
38. User
(Hash)
Nicknames
Bob [ Rob, Bobby ]
Alice [ Allie ]
Carol [ Caroline ]
Dan [ Daniel, Danny ]
友達一覧を取得
Friends Table
User
(Hash)
Friend
(Range)
Bob Alice
Alice Bob
Alice Carol
Alice Dan
Users Table
Hash + Range
Primary Key
39. Friends Table Users Table
User
(Hash)
Nicknames
Bob [ Rob, Bobby ]
Alice [ Allie ]
Carol [ Caroline ]
Dan [ Daniel, Danny ]
User
(Hash)
Friend
(Range)
Bob Alice
Alice Bob
Alice Carol
Alice Dan
友達一覧を取得
Aliceの友達一覧を取得
1. Query (Table =
Friends, Hash = Alice,
Range = *)
2. BatchGetItem(Bob, Carol, Dan)
40. 投稿画像の保存と検索
Images Table
User
(Hash)
Image
(Range)
Date Link
Bob aed4c 2013-10-01 s3://…
Bob cf2e2 2013-09-05 s3://…
Bob f93bae 2013-10-08 s3://…
Alice ca61a 2013-09-12 s3://…
Bob
Bobの投稿画像一覧を取得
Query (Table=Images,
Hash= Bob,
Range=*)
でもある時刻以降の画像を取得し
たかったら…?
41. ある日時の画像取得
Images Table
User Image Date Link
Bob aed4c 2013-10-01 s3://…
Bob cf2e2 2013-09-05 s3://…
Bob f93bae 2013-10-08 s3://…
Alice ca61a 2013-09-12 s3://…
User Date Image
Bob 2013-09-05 cf2e2
Bob 2013-10-01 aed4c
Bob 2013-10-08 f93bae
Alice 2013-09-12 ca61a
Table ByDate Local Secondary Index
Local Secondary Index をDateに張る
42. 画像にユーザのタグ付け
ImageTags Table
Image User
aed4c Alice
aed4c Bob
f93bae Alice
f93bae Bob
Image f93baeにAliceをタグ付け
PutItem(Table = ImageTags,
Hash = f93bae, Range = Alice)
Bob
でもあるユーザがタグ付けされて
る画像の一覧を取得したかったら
…?
Image f93baeにタグ付けされたユーザ一覧
Query(Table = ImageTags,
Hash = f93bae, Range = *)
43. ユーザのタグ付き画像一覧
ImageTags Table
UserにImageをRangeキーとした
Global Secondary Indexを張る
User
(Hash)
Image
(Range)
Bob aed4c
Bob f93bae
Alice aed4c
Alice f93bae
ByUser Global Secondary Index
Image
(Hash)
User
(Range)
aed4c Alice
aed4c Bob
f93bae Alice
f93bae Bob
Table
Bob
Aliceがタグ付けされた画像一覧
49. ◯☓ゲーム – 今のままだとチートが可能
Bob (1)
Amazon
DynamoDB
Bob (2)
Bob (3)
Update:
Turn : Alice
Top-Left : X
Update:
Turn : Alice
Mid : X
State : STARTED,
Turn : Bob,
Top-Right : O
Update:
Turn : Alice
Low-Right : X
50. ◯☓ゲーム – 今のままだとチートが可能
Bob (1)
Amazon
DynamoDB
Bob (2)
Bob (3)
Update:
Turn : Alice
Top-Left : X
Update:
Turn : Alice
Mid : X
State : STARTED,
Turn : Alice,
Top-Right : O,
Top-Left : X,
Mid: X,
Low-Right: X
Update:
Turn : Alice
Low-Right : X
52. 修正版◯☓ゲーム
Bob (1)
Amazon
DynamoDB
Bob (2)
Bob (3)
Update:
Turn : Alice
Top-Left : X
Expect:
Turn : Bob
Top-Left : null
State : STARTED,
Turn : Bob,
Top-Right : O
Update:
Turn : Alice
Mid : X
Expect:
Turn : Bob
Mid : null
Update:
Turn : Alice
Low-Right : X
Expect:
Turn : Bob
Low-Right : null
53. 修正版◯☓ゲーム
Bob (1)
Amazon
DynamoDB
Bob (2)
Bob (3)
State : STARTED,
Turn : Bob,
Top-Right : O
Update:
Turn : Alice
Top-Left : X
Expect:
Turn : Bob
Top-Left : null
Update:
Turn : Alice
Low-Right : X
Expect:
Turn : Bob
Low-Right : null
Update:
Turn : Alice
Mid : X
Expect:
Turn : Bob
Mid : null
54. 修正版◯☓ゲーム
Bob (1)
Amazon
DynamoDB
Bob (2)
Bob (3)
State : STARTED,
Turn : Alice,
Top-Right : O,
Top-Left : X
Update:
Turn : Alice
Top-Left : X
Expect:
Turn : Bob
Top-Left : null
Update:
Turn : Alice
Mid : X
Expect:
Turn : Bob
Mid : null
Update:
Turn : Alice
Low-Right : X
Expect:
Turn : Bob
Low-Right : null
Typical data we see developers gathering about the player includes session length, telemetry data, in game data like how long to the first purchase etc, basically any information that will tell you where the game is doing well versus where it is not.
This data tends to be unstructured and so developers often deploy a NoSQL solution to store that data. They will later use a batch based sort job to cleanse the data and move it into a relational database of some sort, likely a DataWareHouse, for analysis.
Many Game Developers use AWS’s NoSQL offering DynamoDB which can handle very high volumes of read and writes and is highly durable. And as always you can install and manage your own NoSQL offering like MongoDB, Cassandra, Couchbase, etc too.
The Hash and Range schema unlocks the Query API. Given an exact hash key value, in this case, “Bob”, you can Query for all of Bob’s images.
This is great, but are there really that many use cases for querying for ALL of Bob’s images, sorted by some GUID image id? This Query gets more and more expensive as Bob uploads more images.
This isn’t really the way that users tend to interact with an Image app, but we’ve checked off a first basic requirement.
Here we illustrate a local secondary index on Date.
You can think of a Local Secondary Index as a separate table maintained automatically by DynamoDB.
You can see that Bob’s images are still grouped together, but now sorted by their alternate range key
Here we illustrate a local secondary index on Date.
You can think of a Local Secondary Index as a separate table maintained automatically by DynamoDB.
You can see that Bob’s images are still grouped together, but now sorted by their alternate range key
We have two players in a round of tic-tac toe. The item storing the data for this particular game of tic tac toe is here stored in DynamoDB.
This is a match between Alice and Bob, and we’re going to have Alice go first.
We can see with this move that Bob is not very good at this game. By playing that, Alice can guarantee a win by playing in the lower-left.
Let’s say Bob realizes that he not good at this game, and wants to come up with some other way to win.
Based on the API calls we’ve sketched out, this would work and Bob would win, or crash the game.
Here all of those will be merged together. UpdateItem lets you pick specific attributes in an item to update, leaving all the rest of the attributes alone.
PutItem on the other hand replaces the whole item, so then it would have been last write wins. That opens another can of worms around being able to “undo” moves, but that’s a different issue that we’ll fix in the same way.
Apply the write only if the values in the item are still what the request expected them to be.
9-10 min
But, only one of those writes will arrive first. Writes to each item are serialized by DynamoDB.
Thank you for your participation.
Amazon EC2 is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers.
On-Demand Instances – On-Demand Instances let you pay for compute capacity by the hour with no long-term commitments. This frees you from the costs and complexities of planning, purchasing, and maintaining hardware and transforms what are commonly large fixed costs into much smaller variable costs. On-Demand Instances also remove the need to buy “safety net” capacity to handle periodic traffic spikes.
Reserved Instances – Reserved Instances give you the option to make a low, one-time payment for each instance you want to reserve and in turn receive a significant discount on the hourly charge for that instance.
Spot Instances – Spot Instances allow customers to bid on unused Amazon EC2 capacity and run those instances for as long as their bid exceeds the current Spot Price. The Spot Price changes periodically based on supply and demand, and customers whose bids meet or exceed it gain access to the available Spot Instances. If you have flexibility in when your applications can run, Spot Instances can significantly lower your Amazon EC2 costs.
Amazon Relational Database Service (Amazon RDS) is a web service that makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while managing time-consuming database administration tasks, freeing you up to focus on your applications and business.
Amazon S3 is designed to make web-scale computing easier for developers. Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, secure, fast, inexpensive infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.
Amazon Elastic MapReduce (Amazon EMR) is a web service that enables businesses, researchers, data analysts, and developers to easily and cost-effectively process vast amounts of data. It utilizes a hosted Hadoop framework running on the web-scale infrastructure of Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3).
Amazon Virtual Private Cloud (Amazon VPC) lets you provision a private, isolated section of the Amazon Web Services (AWS) Cloud where you can launch AWS resources in a virtual network that you define. With Amazon VPC, you can define a virtual network topology that closely resembles a traditional network that you might operate in your own datacenter. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways.
Amazon CloudFront is a web service for content delivery. It integrates with other Amazon Web Services to give developers and businesses an easy way to distribute content to end users with low latency, high data transfer speeds, and no commitments.
AWS Direct Connect makes it easy to establish a dedicated network connection from your premise to AWS. Using AWS Direct Connect, you can establish private connectivity between AWS and your datacenter, office, or colocation environment, which in many cases can reduce your network costs, increase bandwidth throughput, and provide a more consistent network experience than Internet-based connections.