Module 5 - Developing Flexible NoSQL Solutions
Module 5 - Developing Flexible NoSQL Solutions
Contents
Module 5: Developing Flexible NoSQL Solutions 4
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Section 1: Introduction
Module 5: Developing Flexible NoSQL Solutions
Section 1: Introduction.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 6
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Module overview
Sections Lab
1. Introduction Working with Amazon DynamoDB
2. Introducing AWS database options
3. Key concepts for DynamoDB
Activity
4. Partitions and data distribution
Calculating RCUs and WCUs
5. Secondary indexes
6. Read/write throughput
7. Streams and global tables
8. Backup and restore
Knowledge check
9. Basic operations for DynamoDB
tables
4
Finally, you will complete a knowledge check to test your understanding of key
concepts covered in this module.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 7
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
The café has a basic website now. Sofía wants to modify the site so that information can be
updated dynamically and staff members can retrieve information about the product
inventory.
The café has a basic website now. Sofía wants to modify the site so that information
can be updated dynamically and staff members can retrieve information about the
product inventory.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 8
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
The diagram on this slide gives an overview of the application that you will build
through the labs in this course.
The highlighted portions are relevant to this module. As highlighted in the diagram,
you will set up a DynamoDB database to store the product information.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 9
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Database types
Relational
Databases (DBs) are excellent solutions Traditional examples:
for persistent storage.
Microsoft SQL Server
Oracle Database
The two major types of databases are: MySQL
• Relational databases
• Nonrelational databases
Nonrelational
Traditional examples:
MongoDB
Cassandra
Redis
8
This section defines these two types of databases, and covers AWS examples of each
type.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 11
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
A relational database is a persistent store where data has relationships to other data.
Because of the relationships, you must enforce the schema (or the definitions of the
data that’s stored in the table) and you must also enforce how tables relate to one
another. Changing the schema to accommodate new requirements can sometimes be
a challenge.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 12
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
key 3 value 3
10
The design of nonrelational databases means that they can scale by adding more
compute to the infrastructure. That is, servers can be brought online to increase
capacity when customer demand increases. When demand drops, the additional
servers can be shut down to reduce costs.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 13
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Focused on collection of
Querying SQL-based queries
documents
11
This table compares the features of relational (or SQL) databases and nonrelational
(or NoSQL) databases.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 14
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
12
Amazon Relational Database Service (Amazon RDS) has different optimizations that
you can use, such as memory, performance, and I/O. It offers six database engines:
• Amazon Aurora: Database that’s built for the cloud and is compatible with MySQL
and PostgreSQL
• PostgreSQL: Can be used to deploy a scalable PostgreSQL database with resizable
hardware capacity
• MySQL: Can be used to set up a MySQL deployment
• MariaDB: Scalable MariaDB database engine
• Oracle Database: Fully managed commercial database that can be used to set up,
operate, and deploy an Oracle DB in the cloud
• Microsoft SQL Server: Can be used to set up, operate, and scale Microsoft SQL
Server (2012, 2014, 2016, 2017, 2021)
Amazon Aurora is a database that is built for the cloud and is compatible with MySQL
and PostgreSQL. In terms of performance, Aurora can be up to five times faster than
standard MySQL. It also can be up to three times faster than standard PostgreSQL
databases. It is a fully managed service. It provides security, availability, and reliability
at about one tenth of the cost of commercial databases.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
13
Amazon ElastiCache is an in-memory data store that can boost the performance of
databases. ElastiCache can improve the performance of caching, session stores,
gaming, geospatial services, real-time analytics, and queueing.
Amazon Neptune is a graph database engine that’s optimized for storing billions of
relationships. Its queries run with millisecond latency. Developers can use Neptune
for recommendation engines, fraud detection, drug discovery, network security, and
other solutions that need complex relationships to find solutions.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 16
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
14
The following are the key takeaways from this section of the module:
• Relational databases:
• Strict schema
• Doesn’t need extreme performance
• Nonrelational databases:
• Horizontal scaling
• Non-traditional schema
• AWS offers several different services to support both relational and nonrelational
databases.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 17
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
15
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 18
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
16
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 19
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
SensorReadings table
17
Remember that DynamoDB can be used as a key-value store and document store. In
the examples shown, the primary key value (partition key and sort key if present) is
the key. The remaining attributes constitute the value that corresponds to the key.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 20
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
{
"person_id" : 123,
JSON
"last_name" : "Doe",
String Boolean
"first_name" : "John", Number
"next_anniversary" :
Document item {
"year" : 2021, Map
"month" : 5,
"day" : 30
},
"children" :
["Paulo", "Richard", "Jane", "Mary" List
]
}
18
The size of an item is the sum of the lengths of its attribute names and values. An
item can be up to 400 KB in size.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 21
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
19
The following are the key takeaways from this section of the module:
• Components of DynamoDB:
• Table
• Items
• Attributes
• Types of primary keys:
• Simple primary key
• Composite primary key
• An item has attributes.
• Different items in the same table can have a different number of attributes.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 22
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
20
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 23
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Partitions
Partition Partition
key: A key: B
21
If your table has a simple primary key (partition key only), DynamoDB stores and
retrieves each item based on its partition key value. The partition key of an item is
also known as its hash attribute. A table can have a composite primary key (partition
key and sort key). In this case, DynamoDB will store all the items that have the same
partition key value physically close together. It will then order them by sort-key value
in the partition. The sort key of an item is also known as its range attribute.
For information about how partitioning works, see “Partitions and Data Distribution”
at
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWork
s.Partitions.html.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
22
In this example, the Pets table has a simple primary key (partition key only) and spans
multiple partitions. DynamoDB calculates the hash value of the partition key to
determine which partition should contain the item. In this case, the partition is based
on the hash value of the string Dog. Notice that the items are not stored in sorted
order.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 25
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
{ {
"AnimalType": "Turtle", "AnimalType": "Dog",
DynamoDB "Name": "Quincy", "Name": "Rover",
hash <other attributes> <other attributes>
function } }
Partition {
DynamoDB stores the "AnimalType": "Dog",
new item among the "Name": "Spot",
others with the same <other attributes>
partition key in }
ascending order by sort Pets table Different Partition
key.
23
Suppose that the Pets table has a composite primary key that consists of AnimalType
(partition key) and Name (sort key). DynamoDB again calculates the hash value of the
partition key to determine which partition should contain the item. In that partition,
several items have the same partition key value. Therefore, DynamoDB stores the
new item among the others with the same partition key, in ascending order by sort
key. In this example, DynamoDB writes an item with a partition key value of Dog and
a sort key value of Rover in ascending order.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
24
The following are the key takeaways from this section of the module:
• Amazon DynamoDB stores data in partitions based on the partition key.
• With a simple primary key, the key determines which partitions are selected.
• With a composite primary key, the key still determines which partition is selected,
but the order is based on the sort key.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
25
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 28
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Secondary indexes
• Use to query data based on non-primary-key attributes
• Secondary index defines an alternate key
• Contain:
• Alternate-key attributes
• Primary-key attributes
• Optional subset of other attributes from the base table (projected attributes)
• Can be one of two types:
• Global secondary index
• Local secondary index
26
You can use a secondary index to perform queries on attributes that aren’t part of the
table’s primary key. With a secondary index, you can query the data in the table by
using an alternate key, in addition to queries against the primary key.
In addition to the alternate key attributes and primary key attributes (partition key
and sort key), a secondary index contains a subset of the other table attributes. When
you create an index, you specify which attributes will be copied (or projected) from
the base table to the index. At a minimum, DynamoDB projects the key attributes
from the base table into the index.
Each table in DynamoDB can have up to 20 global secondary indexes (default limit)
per table, and up to five local secondary indexes per table.
For more information about secondary indexes, see “Improving Data Access with
Secondary Indexes” at
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryI
ndexes.html.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
The slide shows an example of when you would use a global secondary index.
Suppose that you have a Music table. You can query data items by Artist (partition
key) or by Artist and SongTitle (partition key and sort key). What if you also wanted to
query the data by Genre and AlbumTitle? You cannot do this query with the Music
table. To do this query, you create a global secondary index on Genre and AlbumTitle
from the base Music table. Then, you query the index in much the same way that you
would query the Music table. In the index, Genre is the partition key and AlbumTitle is
the sort key. Notice that the combination of Genre and Album Title might not be
unique. Multiple albums can belong to one genre.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 30
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Suppose that you want to query by Artist and by AlbumTitle. In this case, you would
create a local secondary index (called ArtistAlbumTitle) from the base Music table.
The local secondary index has the same partition key as the base table (Artist) but a
different sort key (AlbumTitle).
For a local secondary index, the partition key is the same as the table’s partition key.
The sort key can be any scalar attribute.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
29
The following are the key takeaways from this section of the module:
• Secondary indexes contains a subset of attributes and an alternate key or keys to
support query operations.
• Global secondary indexes have different partition keys and sort keys from the keys
in the base table.
• Local secondary indexes have the same partition key as the base table, but a
different sort key.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 32
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
30
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 33
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Read consistency
• DynamoDB maintains multiple copies of data for durability.
• All copies of your data are usually consistent within a second after a write
operation.
• Eventually consistent read might return slightly stale data if a read
operation is performed immediately after a write operation.
• Use case: Blog posts.
• Strongly consistent read returns the most up-to-date data.
• Use cases: Scoreboards, booking systems, financial algorithms.
• You can specify the preferred consistency level for reading data.
31
You can specify the desired consistency level for reading data.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 34
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Transactions
• Simplify the developer experience of making coordinated, all-or-
nothing changes (inserts, deletes, or updates) to multiple items both
within and across tables
• Provide atomicity, consistency, isolation, and durability (ACID),
which help you maintain data correctness in your applications
• Use cases
• Processing financial transactions
• Fulfilling and managing orders
• Building multiplayer game engines
• Coordinating actions across distributed components and services
32
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 35
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
33
DynamoDB divides throughput evenly among partitions. The throughput per partition
is the total provisioned throughput divided by the number of partitions.
With provisioned throughput, you specify the throughput capacity in terms of read
capacity units (RCUs) and write capacity units (WCUs):
• A read capacity unit is the number of strongly consistent reads per second of items
that are up to 4 KB in size. If you perform eventually consistent reads, you use half
of the read capacity units that are provisioned. In other words, for eventually
consistent reads, one read capacity unit is two reads per second for items that are
up to 4 KB.
• A write capacity unit is the number of 1 KB writes per second.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 36
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
34
On-demand mode is a good option when any of the following situations are true:
• You create new tables with unknown workloads.
• You have unpredictable application traffic.
• You prefer the ease of paying for only what you use.
For more information about Amazon DynamoDB on-demand and provisioned modes,
see “Read/Write Capacity Mode” at
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWork
s.ReadWriteCapacityMode.html.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 37
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Activity:
Calculating RCUs
and WCUs
35
In this educator-led activity, you will calculate the number of read capacity units and
write capacity units that are needed for a particular scenario.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 38
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
36
Suppose that you must read 20 items that are 11 KB in size every second, with
eventual consistency. How many RCUs do you need?
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 39
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
37
Compute as follows:
• Round 11 KB up to the next multiple of 4: 12
• Divide by 4 KB per RCU: 3
• Multiply by items read per second: 60
• Divide by 2 for eventual consistency: 30
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 40
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
38
Suppose that you must write 120 items that are 7 KB in size every minute. How many
WCUs do you need?
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 41
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
39
Compute as follows:
• WCUs required to write each 7 KB item: 7
• Number of items per second: 2
• Multiply WCU per item by items per second: 14
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 42
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
40
The following are the key takeaways from this section of the module:
• DynamoDB provides two read consistencies:
• Eventual consistency: might be stale
• Strong consistency: up-to-date data
• DynamoDB transactions offers insert, delete, and update operations that are all-or-
nothing and ACID compliant.
• Read and write throughput can be either of the following types:
• Provisioned (set throughput)
• On-demand (changes with demand)
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 43
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
41
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 44
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
DynamoDB Streams
42
Applications can benefit from capturing changes to items that are stored in a
DynamoDB table at the point when these changes occur. For example, consider an
application that makes changes to user preferences, which are stored in a DynamoDB
table. Another application, such as an ad server, must respond to the new
preferences and present different advertisements.
You can enable DynamoDB Streams as a solution to this type of use case. This
optional feature captures a time-ordered sequence of item-level modifications in any
DynamoDB table (called a stream). It then stores this information for up to 24 hours.
Applications can view the data items as they appeared both before and after they
were modified, in near-real time.
A stream consists of stream records. Each stream record represents a single data
modification in the DynamoDB table that the stream belongs to. Each stream record
is assigned a sequence number, which reflects the order that the record was
published to the stream. When an application creates, updates, or deletes items in
the table, DynamoDB Streams writes a stream record. It records the primary key
attributes of the items that were modified. You can configure the stream so that the
stream records capture additional information, such as the before and after images of
modified items.
Stream records are organized into groups, or shards. Each shard acts as a container
for multiple stream records, and it contains information that is required for accessing
and iterating through these records. The stream records in a shard are automatically
removed after 24 hours.
For information about working with streams, see “Change Data Capture for
DynamoDB Streams” at
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.ht
ml.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 45
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Global tables
• Global tables offer a solution
to deploy a multi-Region,
multi-active database without
needing to build and maintain
your own replication solution.
43
DynamoDB automatically spreads the data and traffic for your tables over a sufficient
number of servers to handle your throughput and storage requirements. At the same
time, it maintains consistent and fast performance. All your data is stored on solid-
state disks (SSDs) and is automatically replicated across multiple Availability Zones in
an AWS Region. As a result, it provides built-in high availability and data durability.
You can use global tables to keep DynamoDB tables synchronized across AWS
Regions.
Global tables provide a fully managed solution for deploying a multi-Region, multi-
active database, without needing to build and maintain your own replication solution.
When you create a global table, you specify the AWS Regions where you want the
table to be available. DynamoDB performs all of the necessary tasks to create
identical tables in these Regions, and to propagate ongoing data changes to all of
them.
The following scenario illustrates one use case for a global table. Suppose that you
have a large customer base that’s spread across three geographic areas: North
America, Europe, and Asia. Customers must update their profile information while
they are using your application. To address these requirements, you could create
three identical DynamoDB tables that are named CustomerProfiles in three different
AWS Regions. These three tables would be entirely separate from each other.
Changes to the data in one table would not be reflected in the other tables. Without
a managed replication solution, you could write code to replicate data changes
among these tables. However, this could be a time-consuming and labor-intensive
effort.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 46
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Instead of writing your own code, you could create a global table that consists of your three Region-
specific CustomerProfiles tables. DynamoDB would then automatically replicate data changes among
those tables. Thus, changes to CustomerProfiles data in one Region would be propagated to the
other Regions. In addition, if one of the AWS Regions becomes temporarily unavailable, your
customers could still access the same CustomerProfiles data in the other Regions.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 47
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Replica: eu-central-1
Replica: us-east-1
Replica: us-west-2
44
A global table is a collection of one or more DynamoDB tables, which are called
replica tables. A replica table (or replica) is a single DynamoDB table that functions as
a part of a global table. Each replica stores the same set of data items. Any given
global table can only have one replica table per Region. Each replica has the same
table name and the same primary key schema.
Global tables use DynamoDB Streams to propagate changes between replicas. Any
changes that are made to any item in any replica table are recorded in a stream.
These changes are propagated to all the other replicas in the same global table. In a
global table, a newly written item is usually propagated to all replica tables within
seconds. With a global table, each replica table stores the same set of data items.
DynamoDB does not support partial replication of only some of the items.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 48
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
45
Conflicts can arise if applications update the same item in different Regions at about
the same time. To achieve eventual consistency, DynamoDB global tables use a last
writer wins reconciliation between concurrent updates. DynamoDB makes a best
effort to determine the last writer. With this conflict resolution mechanism, all the
replicas will agree on the latest update. They then converge toward a state where
they all have identical data.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 49
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
46
An application can read and write data to any replica table. If your application only
uses eventually consistent reads, and only issues reads against one AWS Region, then
it will work without any modification. However, if your application requires strongly
consistent reads, then it must perform all of its strongly consistent reads and writes in
the same Region. DynamoDB does not support strongly consistent reads across AWS
Regions. Therefore, if you write to one Region and read from another Region, the
read response might include stale data. This data doesn't reflect the results of
recently completed writes in the other Region. Strongly consistent reads require the
use of a replica in the same Region where the client is running.
Transactions are enabled for all single-Region DynamoDB tables, and they are
disabled on global tables by default. You can choose to enable transactions on global
tables by request, but replication across Regions is asynchronous and eventually
consistent. You might observe partially completed transactions during replication to
other Regions.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 50
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
47
The following are the key takeaways from this section of the module:
• DynamoDB streams are observed changes in data.
• Streams are composed of shards of data.
• Streams can be consumed by other AWS services, like AWS Lambda.
• Global tables replicate DynamoDB tables across AWS Regions.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 51
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
48
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 52
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
On-demand backups
Amazon DynamoDB provides on-demand backup and restore capabilities. You can use
DynamoDB to create full backups of your tables so that you can meet the
requirements for long-term data retention and archiving for any data regulations that
you must follow. You can back up and restore your DynamoDB table data anytime in
the AWS Management Console or through an API call. Backup and restore actions run
with little impact on table performance or availability.
When you create an on-demand backup, a time marker of the request is cataloged.
The backup is created asynchronously by applying all changes until the time of the
request to the last full table snapshot. Backup requests are processed near
instantaneously, and become available for restore within minutes.
Each time you create an on-demand backup, the entire table data is backed up. No
limit exists to the number of on-demand backups that you can take. All backups in
DynamoDB work without consuming any provisioned throughput on the table.
Point-in-time recovery
Point-in-time recovery helps protect your Amazon DynamoDB tables from accidental
write operations or delete operations. With point-in-time recovery, you don't have to
worry about creating, maintaining, or scheduling on-demand backups. For example,
suppose that a test script writes accidentally to a production DynamoDB table. With
point-in-time recovery, you can restore that table to any point in time during the last
35 days. DynamoDB maintains incremental backups of your table.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 53
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
50
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 54
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
You can use the Amazon DynamoDB API to invoke the following types of operations
from an application:
• Control operations: Use to create and manage DynamoDB tables. These operations
also let you work with indexes, streams, and other objects that depend on tables.
• Data operations: Use to perform create, read, update, and delete actions on data
in a table. These actions are the major functions that are implemented in
relational database applications. Some data operations also allow you to read data
from a secondary index.
• Batch operations: Allows developers to run multiple operations against
DynamoDB. So developers can run a query, compute some value from the result of
the query, and then write back to DynamoDB.
• Transaction operations: Make coordinated, all-or-nothing changes to multiple
items both within and across tables.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 55
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Creating a table
table = dynamodb.create_table(
TableName is the name TableName = 'users', Create_table:
of the table to create. KeySchema = [
{ Creates a new
'AttributeName': 'username',
'KeyType': 'HASH'
table
KeySchema specifies },
the attributes that {
'AttributeName': 'last_name',
make up the primary 'KeyType': 'RANGE' users Table
key for the table. }
], username last_name
AttributeDefinitions = [ (Partition key) (Sort key)
AttributeDefinitions is {
'AttributeName': 'username',
an array of attributes 'AttributeType': 'S'
that describes the key },
schema for the table. {
'AttributeName': 'last_name',
'AttributeType': 'S' 5 5
}
ProvisionedThroughput ],
represents the ProvisionedThroughput = {
provisioned throughput 'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
settings for the table. }
)
52
This example shows how to create a table by using the AWS SDK for Python (Boto3).
The invocation of the create_table operation creates a table that is named users. It
has a partition-and-sort primary key that’s composed of attributes named username
and last_name (which have key types of hash and range, respectively). The
provisioned throughput for the table is five RCUs and five WCUs.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 56
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Creating an item
users Table: Before
Put_item: Creates a username last_name
new item or (Partition key) (Sort key)
replaces (overwrites)
table.put_item(
an existing item Item = {
'username': 'janedoe',
Item is a map of attribute name-value 'first_name': 'Jane',
pairs, one for each attribute. Only the 'last_name': 'Doe',
primary key attributes are required. 'age': 25,
You can optionally provide other 'account_type': 'standard_user'
attribute name-value pairs for the }
item. )
53
The PutItem operation creates a new item or replaces an earlier item with a new
item. If an existing item in the specified table has the same primary key as the new
item, the new item completely replaces the existing item. You can perform a
conditional PUT operation (add a new item if an item with the specified primary key
doesn't exist). Or you can replace an existing item if it has certain attribute values.
You can return the item's attribute values in the same operation by using the
ReturnValues parameter.
To continue with the Python example: After the users table is created and assigned to
the table variable, the put_item operation is invoked to add a new item to the table.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 57
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Updating an item
username last_name first_name Age account_type
Update_Item: users Table: (Partition key) (Sort key)
Edits an existing Before
janedoe Doe Jane 25 standard_user
item's attributes, or
adds a new item to table.update_item(
Key is the primary key
the table if it of the item to be
Key = {
'username': 'janedoe',
doesn’t already updated. 'last_name': 'Doe'
exist },
UpdateExpression = 'SET age = :val1',
UpdateExpression defines one or
more attributes to be updated. ExpressionAttributeValues = {
':val1': 26
ExpressionAttributeValues is }
one or more values that can be )
substituted in an expression.
username last_name first_name Age account_type
users Table: (Partition Key) (Sort key)
After
janedoe Doe Jane 26 standard_user
54
Use the UpdateItem operation to update an existing item's attributes, or add a new
item to the table if it doesn’t already exist. You add, set, or remove attribute values.
You can also perform a conditional update on an existing item. For example, you can
replace an existing name-value pair if it has certain expected attribute values.
Additionally, you can return the item's attribute values—from either before or after
the update—by using the ReturnValues parameter.
In this Python example, the update_item operation is used to modify the age
attribute of an item in the users table.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 58
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Deleting an item
Delete_Item:
users Table: username last_name first_name Age account_type
Deletes a single Before (Partition key) (Sort key)
item in a table by janedoe Doe Jane 26 standard_user
primary key
table.delete_item(
Key = {
Key represents the 'username': 'janedoe',
primary key of the 'last_name': 'Doe'
item to delete. }
)
55
Use the DeleteItem operation to delete an item in a table by using its primary key.
You can perform a conditional delete operation that deletes the item if it has an
expected attribute value. In addition to deleting an item, you can also return the
item's attribute values by using the ReturnValues parameter.
In this Python example, the delete_item operation is used to delete an item from the
users table.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 59
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Condition expressions
AccountStatus Table
userId lastFailedLoginTime accountLocked
(primary key)
1 2021-06-06T19:20+01:00 Y
UpdateItem Operation
56
For the PutItem, UpdateItem, and DeleteItem operations, you can specify a condition
expression to determine which items should be modified. If the condition expression
evaluates to true, the operation succeeds. Otherwise, the operation fails.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 60
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Reading an item
{u'username': u'janedoe',
u'first_name': u'Jane',
Expected u'last_name': u'Doe',
output u'account_type': u'standard_user',
u'age': Decimal('25')
}
57
Use the GetItem operation to retrieve a specific item from a DynamoDB table. You
must specify the table name and full primary key (partition key and sort key, if any) to
retrieve a single item from a table. You can optionally specify a projection expression
to retrieve only certain attributes instead of retrieving the entire item. By default, all
attributes of the item are returned. You can also request that the operation use a
strongly consistent read instead of the default eventually consistent read.
In this Python example, the get_item operation is used to retrieve an item in the
users table, which is then printed to standard output. The expected result is a JSON
object that contains all the attributes of the item.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 61
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Querying a table
KeyConditionExpression FilterExpression
(optional)
Collection of items
Table or index Optional:
• ProjectionExpression
• ConsistentRead
response = table.query (
KeyConditionExpression = Key('username').eq('johndoe')
)
58
Use the Query operation to read only the items that match the primary key from a
table or secondary index. The primary key is specified in the key condition expression.
If a filter expression is specified, then the Query operation further refines the result
set based on the filter. You must explicitly specify the name of the table or secondary
index that you want to query. The Query operation returns a result set with the items
that match the conditions that were specified. If none of the items satisfy the given
criteria, then the Query operation returns an empty result set.
This Python example uses the Query operation to retrieve all the items in the users
table with a primary key value that’s equal to johndoe.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 62
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Scanning a table
A Scan operation reads ALL
items from the table or index.
FilterExpression
(optional) A Query operation is more
Table or Index Collection of items
efficient than a scan operation.
response = table.scan (
FilterExpression = Attr('age').lt(27)
)
59
The Scan operation is similar to a Query operation, but the Scan operation reads all
items from the table or index. The result set can be refined by using a filter
expression.
In this Python example, the Scan operation is used to retrieve all the users whose age
is less than 27.
Because the Scan operation reads all items from the table or index, it’s an expensive
operation. It’s more efficient to perform a Query operation that has the appropriate
key condition expression to return only the data that your application needs. If you
must do a Scan operation, perform a parallel scan when possible.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 63
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
• The 1-MB pagination limit reduces • The Limit parameter specifies the
the amount of data that Query or maximum number of items that a
Scan operations return. Query or Scan operation returns.
ExclusiveStartKey
LastEvaluatedKey
60
To improve performance and optimize cost, you can limit the amount of data that
Query and Scan operations return. Two factors affect the number of items that a
Query or Scan operation returns:
• Pagination limit: By default, DynamoDB divides the results of Query and Scan
operations into pages of data that are 1 MB in size (or less). An application can
process the first page of results, then the second page, and so on. A single Query
or Scan will only return a result set that fits within the 1-MB size limit.
• Limit parameter value: You can also limit the number of items that Query and Scan
operations return in the result. To achieve this result, set the Limit parameter to
the maximum number of items that you want.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 64
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Batch operations
• BatchGetItem: Read up to 16 MB of data that consists of up to 100
items from multiple tables.
• If one request in a batch fails, the entire operation does not fail.
• Retry with failed keys and data that is returned by the operation.
61
You can use batch operations to achieve higher throughput by writing, deleting, or
replacing multiple items in a single request. You can also use batch operations to take
advantage of parallelism without needing to manage multiple threads on your own.
You can achieve lower average latencies, compared to using single PutItem or
DeleteItem operations, when multiple items are written, deleted, or replaced.
You can use the following operations to retrieve or write data to DynamoDB in
batches:
• BatchGetItem: Returns the attributes of one or more items from one or more
tables. You identify requested items by primary key. A single operation can retrieve
up to 16 MB of data, which can contain as many as 100 items.
• BatchWriteItem: Puts or deletes multiple items in one or more tables. A single call
to BatchWriteItem can write up to 16 MB of data, which can comprise as many as
25 PUT or DELETE requests. Individual items to be written can be as large as 400
KB.
With batch operations, if one request in a batch fails, the entire operation does not
fail.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 65
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Transactional operations
• TransactWriteItems
• Contains a write set
• Includes one or more PutItem, UpdateItem, and DeleteItem operations across multiple tables
• TransactGetItems
• Contains a read set
• Includes one or more GetItem operations across multiple tables
62
You can use the DynamoDB transactional read and write APIs to manage complex
business workflows. These workflows might require adding, updating, or deleting
multiple items as a single, all-or-nothing operation. For example, a video game
developer can ensure that players’ profiles are updated correctly when they exchange
items in a game or make in-game purchases.
With transactional operations, if one operation fails, the entire transaction fails.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 66
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
63
The following are the key takeaways from this section of the module:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 67
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Lab 5.1:
Working with
Amazon
DynamoDB
64
You will now complete Lab 5.1: Working with Amazon DynamoDB.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 68
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Lab: Tasks
1. Preparing the lab
2. Creating a DynamoDB table by using the SDK for Python
3. Working with DynamoDB data – Understanding DynamoDB
condition expressions
4. Adding and modifying a single item by using the SDK
5. Adding multiple items by using the SDK and batch processing
6. Querying the table by using the SDK
7. Adding a global secondary index to the table
65
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 69
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
~ 90 minutes
66
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 70
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Lab debrief:
Key takeaways
67
Your educator might choose to lead a conversation about the key takeaways from this
lab after you have completed it.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 71
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Module wrap-up
Module 5: Developing Flexible NoSQL Solutions
68
It’s now time to review the module and wrap up with a knowledge check and
discussion of a practice certification exam question.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 72
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Module summary
In summary, in this module, you learned how to do the following:
• Recognize different database options on AWS
• Identify the features of DynamoDB
• Describe the components of DynamoDB
• Explain how DynamoDB uses partitions
• Indicate how indexes are used with DynamoDB
• Describe how DynamoDB keeps data consistent
• Recognize when streaming and global tables are used
• Explain the backup and restore process
• Develop with DynamoDB by using the AWS SDKs
69
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 73
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
70
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 74
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
How could the developer modify the table to create this solution?
• Amazon DynamoDB
71
It is important to fully understand the scenario and question being asked before even
reading the answer choices. Find the keywords in this scenario and question that will
help you find the correct answer.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 75
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
How could the developer modify the table to create this solution?
Choice Response
A Create a global secondary index to speed up queries so the developer’s code can get faster results.
B Create a local secondary index to speed up queries so the developer’s code can get faster results.
72
Now that we have bolded the keywords in this scenario, let us look at the answers.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 76
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Choice Response
A Create a global secondary index to speed up queries so the developer’s code can get faster results.
B Create a local secondary index to speed up queries so the developer’s code can get faster results.
73
Look at the answer choices and rule them out based on the keywords that were
previously highlighted.
When DynamoDB Streams is enabled, the table will emit one of the four options to
the stream:
1. Keys only: Only the key attributes of the modified item
2. New image: The entire item, as it appears after it was modified
3. Old image: The entire item, as it appeared before it was modified
4. New and old images: Both the new and the old images of the item
The developer could select the New Image option, which writes update information
to the stream. This Stream could then be associated with the target Lambda function.
The Lambda function would receive update information that it could use to
determine whether the Operations team must be notified.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 77
AWS Training and Certification Module 5: Developing Flexible NoSQL Solutions
Thank you
74
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 78