AWS Game Tech - Guide to Scalable Data Storage
AWS Game Tech - Guide to Scalable Data Storage
Quick jump
Supporting a global audience
of millions of online players 1.0
MySQL...................................................................................4
reach millions per second Amazon Aurora...................................................................6
Caching...............................................................................13
We hope this guide gives you the information you need
to become more knowledgeable about different types of
2.0
purpose-built data storage for games that AWS has to
Binary game content with Amazon S3
offer—plus how to optimize performance to give your
players the best experience possible. We even threw in Content delivery and Amazon CloudFront.................17
a few architectural diagrams to help you get started.
Uploading content to Amazon S3................................18
1.0
access, distributed storage, and eventual consistency.
These NoSQL databases can be especially beneficial
for games, where data structures tend to be lists
and sets—like friends, levels, and items—as opposed
to complex relational data.
Relational
single large relational database. relational and NoSQL flavors, but the ones used most
frequently for games on AWS are Amazon Aurora,
Amazon ElastiCache for Redis, Amazon DynamoDB,
Amazon RDS for MySQL, and Amazon DocumentDB
vs. NoSQL
(with MongoDB capability).
databases
as MySQL and Redis, or MySQL and DynamoDB,
are especially successful on AWS. All database
alternatives described in this section support atomic
operations, such as increment and decrement, which
are crucial for gaming.
4
1.1 MYSQL
TRANSACTIONS
MySQL provides support for grouping multiple These advantages continue to make MySQL attractive,
changes into a single atomic transaction especially for aspects of gaming like account records,
that must be committed or rolled back. in-app purchases, and similar functionality where
NoSQL stores typically lack multi-step transactions and data consistency are paramount.
transactional functionality. Even gaming companies using NoSQL offerings, such
as Redis and DynamoDB, frequently put transactional
ADVANCED QUERYING data like accounts and purchases in MySQL.
MySQL
MySQL speaks SQL, which provides the
flexibility to perform complex queries that If you are using MySQL on AWS, we recommend
evolve over time. NoSQL databases typically that you use Amazon Relational Database Service
only support key-value access or access by a (RDS) to host MySQL. This can save you valuable
single secondary index, meaning you must deployment and support cycles. Amazon RDS for
make careful data design decisions up front. MySQL automates the time-consuming aspects of
database management, like launching Amazon Elastic
MySQL is the most widely adopted SINGLE SOURCE OF TRUTH Compute Cloud (EC2) instances, configuring MySQL,
MySQL guarantees internal data consistency. attaching Amazon Elastic Block Store (EBS) volumes,
open- source relational database. With Part of what makes many NoSQL solutions setting up replication, running nightly backups, and so
more than 20 years of community-backed faster is distributed storage and eventual on. In addition, Amazon RDS offers advanced features,
consistency. (Eventual consistency means you including synchronous Multi-AZ replication for high
development and support, MySQL is a
could write a key on one node, fetch that key availability, automated master-slave failover, and read
reliable, stable, and secure SQL-based on another node, and have it not appear replicas for increased performance.
database management system. there immediately.)
The following are some configuration options that we recommend you As your game grows and your write
implement when you create your RDS MySQL DB instances: load increases, resize your RDS DB
instances to scale up.
There are several key features that Amazon Aurora brings to a The following are some recommendations for using
gaming workload: Amazon Aurora in your gaming workload:
Amazon
latency between read replicas. Availability Zone to provide for failover and read
operation offloading.
DATA DURABILITY
Aurora
In Amazon Aurora, each 10 GB chunk of your database volume is Schedule Amazon RDS backup snapshots and
replicated six ways across three Availability Zones, allowing for the upgrades during low player count times. If possible,
loss of two copies of data without affecting database write availability, avoid running jobs or reports against the database
and three copies without affecting read availability. Backups are done during this window to prevent backlogging.
automatically and continuously to Amazon Simple Store Service
(S3), which is designed for 99.999999999 percent durability with a If your game grows beyond the bounds of a
retention period of up to 35 days. You can restore your database to traditional relational database, like MySQL or
Amazon Aurora is a MySQL- any second (up to the last five minutes) during the retention period. Amazon Aurora, we recommend that you perform a
performance evaluation, including tuning parameters
compatible relational database SCALABILITY and sharding. In addition, you should look at using
engine that combines the speed Amazon Aurora is capable of automatically scaling its storage a NoSQL offering, such as Redis or DynamoDB,
subsystem out to 64 TB of storage. This storage is automatically to offload some workloads from MySQL. In the
and availability of high-end
provisioned for you, so you do not have to provision storage ahead of following sections, we will cover a few popular
commercial databases with the time. As an added benefit, this means you pay only for what you use, NoSQL offerings.
simplicity and cost-effectiveness reducing the costs of scaling. Amazon Aurora also can deploy up to 15
read replicas in any combination of Availability Zones, including cross-
of open-source databases. region where Amazon Aurora is available. This allows for seamless
failover in case of an instance failure.
Redis provides foundational data types, including MySQL or DynamoDB, Redis can provide a highly
counters, lists, sets, and hashes, which are accessed scalable solution for game data. Redis plus MySQL
using a high-speed text-based protocol. For more is a popular solution for gaming.
details, see the Redis data types documentation and
introduction to Redis data types and abstractions. Redis uses minimal CPU but a lot of memory. As a
These unique data types make Redis an ideal choice result, it is best suited to high-memory instances,
for leaderboards, game lists, player counts, stats, such as the Amazon EC2 memory optimized instance
inventories, and similar data. Redis keeps its entire family (that is, R3). AWS offers a fully managed Redis
data set in memory, so access is extremely fast. For service, Amazon ElastiCache for Redis. ElastiCache
comparisons with Memcached, check out the for Redis can handle clustering, master-slave
Redis benchmarks. replication, backups, and many other common Redis
maintenance tasks.
There are a few caveats concerning Redis that you
Redis
should be aware of. First, you need a large amount For a deep dive on how to get the most out of
of physical memory because the entire dataset is ElastiCache, see the Performance at Scale with
memory resident (that is, there is no virtual memory Amazon ElastiCache whitepaper.
support). Replication support is also simplistic, and
debugging tools for Redis are limited. Redis is not
suitable as your only data store. However, when used
in conjunction with a disk-backed database, such as
Best described as an atomic data
structure server, Redis has unique
features not found in other databases.
Photo of developer
Benefits of Redis
± Extensibility
8
1.4 MONGODB
MongoDB uses a binary variant of JSON called BSON for Benefits of MongoDB
communication, which makes programming against it
a matter of storing and retrieving JSON structures. This ± Data flexibility
has made MongoDB a popular choice for games and web
applications, since server APIs are usually JSON too. ± High performance
± High availability
MongoDB also offers a number of interesting hybrid features,
including SQL-like syntax that enables you to query data by ± Continuous scale
range and composite conditions. MongoDB supports atomic
operations, such as increment/decrement and add/remove
from list. This is similar to Redis support for these operations.
For examples of atomic operations that MongoDB supports,
see the MongoDB findAndModify documentation.
MongoDB
MongoDB is widely used as a primary data store for games
and is frequently used in conjunction with Redis because
the two complement each other well. Transient game data,
sessions, leaderboards, and counters are kept in Redis. Then
progress is saved to MongoDB at logical points (for example,
at the end of a level or when a new achievement is unlocked).
MongoDB is a document-oriented database, Redis yields high-speed access for latency-sensitive game
which means data is stored in a nested data data, and MongoDB provides simplified persistence.
structure similar to a structure you would use in MongoDB supports native replication and sharding as
a typical programming language. well, although you do have to configure and monitor these
features yourself. For an in-depth look at deploying MongoDB
on AWS, see the MongoDB on AWS whitepaper.
Amazon
DynamoDB Games frequently use DynamoDB features in the
following ways:
A production-ready game
backend running on AWS
using DynamoDB
Table structure and queries
Elastic Load
Balancing
DynamoDB, like MongoDB, is a loosely structured
Stateful TCP socket HTTP/S HTTP/S TCP
NoSQL data store that allows you to save different sets
of attributes on a per-record basis. You only need to
CloudFront CDN predefine the primary key strategy you are going to use:
PARTITION KEY
The partition key is a single attribute that
DynamoDB uses as input to an internal hash
function. This could be a player name, game ID,
UUID, or similar unique key. Amazon DynamoDB
builds an unordered hash index on this key.
Stateful Game Servers HTTP/JSON Servers HTTP/JSON Servers Stateful Game Servers
Security Group Auto Scaling Group Auto Scaling Group Security Group PARTITION KEY AND SORT KEY
Referred to as a composite primary key, this
type of key is composed of two attributes: the
Queue Mobile push Broadcast partition key and the sort key. DynamoDB uses
Game data async notifications message for
the partition key value as input to an internal
job game
Reads Reads hash function, and all items with the same
partition key are stored together in sorted order
S3 for binary
SQS for job game assets
by sort key value. For example, you could store
CACHE CACHE Writes queues game history as a duplet of [user_id, last_login].
SNS for Push Amazon DynamoDB builds an unordered hash
Messages index on the partition key attribute, and a
Push message
sorted range index on the sort key attribute.
ElastiCache for Redis
Security Group Only the combination of both keys is unique
Job results in this scenario.
Writes Run job For best querying performance, you should maintain
each DynamoDB table at a manageable size. For
example, if you have multiple game modes, it is
DynamoDB
better to have a separate leaderboard table for each
Job Workers Auto
game mode, rather than a single giant table. This also
Scaling Group
gives you the flexibility to scale your leaderboards
separately in the event that one game mode is more
Availability Zone A Availability Zone B
popular than the others.
DynamoDB shards your data behind the scenes to To get the best performance from DynamoDB, make DAX allows you to provision a fully managed, in-
give you the throughput you requested. DynamoDB sure your reads and writes are spread as evenly as memory cache for DynamoDB that speeds up the
uses the concept of read and write units. One read possible across your keys. Using a hexadecimal string, responsiveness of your DynamoDB tables from
capacity unit represents one strongly consistent such as a hash key or checksum, is one easy strategy millisecond-scale latency to microseconds. This
read per second, or two eventually consistent reads to inject randomness. acceleration comes without requiring any major
per second, for an item up to 4 KB in size. One write changes in your game code, which simplifies
capacity unit represents one write per second for deployment into your architecture. All you have
For more details on optimizing DynamoDB
an item up to 1 KB in size. The defaults are five to do is re-initialize your DynamoDB client with
performance, see Best Practices for Designing
read and five write units, which means 20 KB of a new endpoint that points to DAX, and the
and Architecting with DynamoDB in the Amazon
strongly consistent reads per second and 5 KB of rest of the code can remain untouched. DAX
DynamoDB Developer Guide.
writes per second. You can increase your read and/ handles cache invalidation and data population
or write capacity at any time by any amount up without your intervention. This cache can help
to your account limits. You can also decrease the speed responsiveness when running events that
read and/or write capacity by any amount, but this might cause a spike in players, such as a seasonal
cannot exceed more than four decreases in one day. downloadable content (DLC) offering or a new
Scaling can be done using the AWS Management patch release.
Console or Command Line Interface (CLI) by selecting
the table and modifying it appropriately. You can !
also take advantage of DynamoDB Auto Scaling by 3
using the Auto Scaling service to dynamically adjust
Amazon SNS
provisioned throughput capacity on your behalf Amazon
in response to actual traffic patterns. DynamoDB CloudWatch
Auto Scaling works in conjunction with Amazon 2
CloudWatch alarms that monitor the capacity units. 1
4
Auto Scaling scales according to your defined rules.
You can use any of these for gaming, and there are
examples of gaming companies using them on AWS
with success. As with choosing a server programming
language, there is no perfect database—you need to
weigh the pros and cons of each one.
Other RIAK
NoSQL
model for web scale profile and session
management, real-time big data, data
cataloging, content management, customer
360 data management, digital messaging,
options
and more.
COUCHBASE
CASSANDRA
Caching
install Memcached or Redis on EC2 instances yourself, population or cache aside. This means the cache is
or you can use Amazon ElastiCache for Redis, the checked, and if the value is not in cache (a cache
AWS managed caching service. Like Amazon RDS miss), the record is retrieved, stored in cache,
and DynamoDB, ElastiCache completely automates and returned.
the installation, configuration, and management of
Memcached and Redis on AWS. LAZY POPULATION
Lazy population is the most prevalent caching
For gaming, adding a caching layer in For more details on setting up ElastiCache, see strategy because it only populates the cache
when a client requests the data. This way, it avoids
front of your database for frequently What is Amazon ElastiCache for Redis? in the
extraneous writes to the cache in the case of
Amazon ElastiCache User Guide.
used data can alleviate a significant records that are infrequently (or never) accessed
or that change before being read. This pattern is
number of scalability problems.
so ubiquitous that most major web development
frameworks, such as Ruby on Rails, Django, and
Grails, include plugins that wrap this strategy. The
downside to this strategy is that when data changes,
the next client that requests it incurs a cache miss,
resulting in a slower response time because the new
record needs to be queried from the database and
populated into cache.
Your database is responsible for While you could theoretically store this type of
data in a database, using Amazon S3 has a number
storing user data, including accounts, of advantages, including:
stats, items, purchases, and more.
But for game-related binary data, ± Storing binary data in a database is memory and
Amazon S3 is a better fit. disk intensive, consuming valuable
query resources.
2.0
± Clients can directly download the content from
Amazon S3 provides a simple HTTP-based API to Amazon S3 using a simple GET operation.
PUT (upload) and GET (download) files. With Amazon
± Amazon S3 is designed for 99.999999999 percent
S3, you pay only for the amount of data you store
durability and 99.99 percent availability of objects
and transfer. Using Amazon S3 consists of creating
over a given year.
a bucket to store your data in and making HTTP
requests to and from that bucket. For a walkthrough ± Amazon S3 natively supports features such as
of the process, see Creating a bucket in the Amazon ETag, authentication, and signed URLs.
S3 Getting Started Guide. ± Amazon S3 plugs into the Amazon CloudFront
CDN for distributing content quickly to large
numbers of clients.
Amazon S3 is ideally suited for a variety of gaming
use cases, including the following: With these factors in mind, let’s look at the aspects of
Amazon S3 that are most relevant for gaming.
Binary game
± Content downloads: Game assets, maps,
patches, and betas
content with
user-created levels, and device backups
Amazon S3
between devices (AWS AppSync would also
be a good choice)
17
2.1 CONTENT DELIVERY AND AMAZON CLOUDFRONT
Users expect an ongoing stream of new characters, Easy versioning with ETag
levels, and challenges for months—if not years—after
a game’s release. The ability to deliver this content As mentioned earlier, Amazon S3 supports HTTP
quickly and cost-effectively has a big impact on the ETag and the If-None-Match HTTP header, which
profitability of a DLC strategy. are well known to web developers but frequently
overlooked by game developers. These headers
Although the game client itself is typically distributed enable you to send a request for a piece of Amazon
Content
through a given platform’s app store, pushing a new S3 content and include the MD5 checksum of the
version of the game just to make a new level available version you already have. If you already have the
can be onerous and time consuming. Promotional latest version, Amazon S3 responds with an HTTP
or time-limited content, such as Halloween-themed 304 Not Modified status code (or an HTTP 200 status
delivery
assets or a long weekend tournament, are usually code along with the file data) if you need it. For an
easier to manage yourself in a workflow that mirrors overview of this call flow, read about typical usage
the rest of your server infrastructure. of HTTP ETag.
and Amazon
If you are distributing content to a large number
of clients (for example, a game patch, expansion, Using ETag in this manner makes any future use of
or beta), we recommend using CloudFront in front CloudFront more powerful because CloudFront also
CloudFront
of Amazon S3. CloudFront has points of presence supports the Amazon S3 ETag. For more information,
(POP) located throughout the world, which improves see Request and Response Behavior for Amazon S3
download performance. In addition, you can configure Origins in the Amazon CloudFront Developer Guide.
which Region’s CloudFront serves to optimize your
costs. For more information, see How does CloudFront You can also geo target or restrict access to your
lower my costs? in the CloudFront FAQs. content through CloudFront’s Geo Targeting
feature. CloudFront detects the country where your
DLC is a huge aspect of modern games from an Finally, if you anticipate significant CloudFront usage, customers are located and will forward the country
contact our sales team. Amazon offers reduced code to your origin servers, allowing your origin
engagement perspective, and it is becoming a
pricing that is even lower than our on-demand pricing server to determine the type of personalized content
primary revenue stream. for high-usage customers. that will be returned to the customer based on their
geographic location. This content could be anything
from a localized dialog file for an RPG to localized
asset packs for your game.
18
2.2 UPLOADING CONTENT TO AMAZON S3
Uploading
if possible, since this offloads work from your
REST API tier.
content to
Using Amazon S3 POST Using Amazon S3 POST
and can even be accomplished directly from a web
browser. For more information, see Browser-based
uploads using POST (AWS signature version 2) in
Amazon S3
the Amazon S3 Developer Guide. You can even create Amazon S3 Amazon S3
S3 revolve around uploading data from not corrupted during upload. For more information,
File transfer
see PutObject in the Amazon S3 API Reference. Web request
The following figure shows an example call flow that This simple call flow handles the case where
you can use to upload UGC to Amazon S3. the asset data is stored verbatim in Amazon S3,
which is usually true of user-generated levels or
In this example, you PUT the binary game asset characters. This same pattern works for game
(for example, the avatar or level) to Amazon S3, saves as well—store the game save data in Amazon
which creates a new object in Amazon S3. After you S3, and index it in your database by user_id, date,
receive a success response from Amazon S3, you and any other important metadata. If you need to
make a POST request to our REST API layer with the do additional processing of an Amazon S3 upload
metadata for that asset. The REST API needs to have (for example, generating preview thumbnails),
a service that accepts the Amazon S3 key name make sure to read about asynchronous jobs in the
plus any metadata you want to keep, and then Introduction to Scalable Game Development
it stores the key name and the metadata in the Patterns on AWS ebook. There, you will learn
database. The game’s other REST services can then about adding Amazon SQS to queue jobs to
query the database to find new content, popular handle these types of tasks.
downloads, and so on.
HTTP/JSON Data
Analytics and A/B testing After you identify the data, follow these steps to track it:
Collecting data about your game is one of 1. Collect metrics in a local data file on the user’s 3. For each file you upload, put a record somewhere For both analytics and A/B testing, the data
the most important and easiest things you device (for example, mobile, console, or PC). To indicating there is a new file to process. Amazon flow tends to be unidirectional—that is,
can do. Perhaps the trickiest part is deciding make things easier later, we recommend using a S3 event notifications provide an excellent way metrics flow in from users, are processed, and
what to collect. Because Amazon S3 storage CSV format and a unique file name. For example, to support this. To enable notifications, you must then a human makes decisions that impact
is cheap, you should consider keeping track of a given user might have their data tracked in 241- first add a notification configuration identifying future content releases or game features. In
any reasonable metrics you can think of for a game_name-user_idYYYYMMDDHHMMSS.csv or the events you want Amazon S3 to publish, such A/B testing, for example, when you present
user (for example, total hours played, favorite something similar. as a file upload and the destinations where you users with different items, screens, and so
characters or items, and current and highest want Amazon S3 to send the event notifications. forth, you can make a record of the choice
level) if you are not sure what to measure or 2. Periodically persist the data by having the client We recommend Amazon SQS, as you can have they were given along with their subsequent
have a client that is not updated easily. upload the metrics file directly to Amazon S3. a background worker listen to Amazon SQS for actions (such as purchase or cancel). Then,
Alternatively, you can integrate with Amazon new files and process them as they arrive. For periodically upload this data to Amazon S3,
However, if you are able to formulate Kinesis and adopt a loosely coupled architecture, more details, see the Amazon SQS section of the and use Amazon EMR to create reports. In the
questions that you want answered as discussed in the Introduction to Scalable Game Introduction to Scalable Game Development simplest use case, you can generate cleaned
beforehand or if client updates are easy, you Development Patterns on AWS ebook. When you Patterns on AWS ebook. up data from Amazon EMR in CSV format in
can focus on gathering the data that helps go to upload a given data file to Amazon S3, open another Amazon S3 bucket and then load it
you answer those specific questions. a new local file with a new file name. This keeps 4. As part of a background job, process the data into a spreadsheet program.
the upload loop simple. using a framework like Amazon EMR or another
framework that you choose to run on Amazon A proper treatment of analytics and Amazon
EC2. This background process can look at new data EMR is beyond the scope of this ebook.
files that have been uploaded since the last run
and perform aggregation or other operations on For more information, see Data Lakes and
Upload Processing the data. (Note: If you are using Amazon EMR, you Analytics on AWS and the Best Practices
might not need to follow step 3 because Amazon for Amazon EMR whitepaper. To contact us,
EMR has built-in support for streaming new files.)
2. PUT file to S3 please fill out the form at the AWS Game
5a. EMR cluster Tech website.
3. HTTP 200 OK from S3 5. Optionally, feed the data into Amazon Redshift
1. Write local
metrics file
S3 Bucket EMR for additional data warehousing and analytics
OR flexibility. Amazon Redshift is an ANSI SQL-
compliant, columnar data warehouse that you pay
for by the hour. This enables you to perform queries
5b. Non-EMR workflow across large volumes of data, such as sums and
min/max using familiar SQL-compliant tools.
DynamoDB EC2 Workers
OR
Repeat these steps in a loop, uploading and
processing data asynchronously.
4. PUT new S3 key is ready
SQS The figure on this page shows how this pattern works.
To achieve this scale and get the best performance Let’s assume your bucket is called mygame-ugc and that you store files
out of Amazon S3, there are a few guidelines you based on a sequential database ID:
need to follow. First, as with DynamoDB, make
sure that your Amazon S3 key names are evenly
distributed, as Amazon S3 determines how to
partition data internally based on the first few
characters in the key name.
Amazon S3 In this case, all of these files would likely live in the same internal partition
performance
within Amazon S3 because the keys all start with 107. This limits your
scalability because it results in writes that are sequentially clustered
together. A simple solution is to use a hash function to generate the first
part of the object name in order to randomize the distribution of names.
considerations
One easy strategy is to use an MD5 or SHA1 filename and prefix the Amazon
S3 key with that, as shown in the following code example:
#!/usr/bin/env python
import hashlib
sha1 = hashlib.sha1(filename).hexdigest()[0:3]
path = sha1 + “-” + filename 39