Concepts

Primary Keys

In DynamoDB, each item (row) in a table is uniquely identified by a primary key. The primary key is fundamental in determining the way data is partitioned and retrieved. There are two types of primary keys:

  • Partition key: A simple primary key, composed of one attribute known as the partition key. DynamoDB uses the partition key’s value as input to an internal hash function, which determines the partition or physical location on which the data is stored.
  • Composite primary key: Also known as a partition key and sort key, it is composed of two attributes. The first attribute is the partition key, and the second one is the sort key. This allows for a range of items to be grouped together in one partition because they share the same partition key, but can be queried and sorted efficiently using the sort key.

Example:

Partition Key (UserID) Sort Key (Timestamp) Data
User1 2021-05-01T12:00:00Z {“Message”: “Hello”}
User1 2021-05-01T13:00:00Z {“Message”: “World”}
User2 2021-05-01T12:00:00Z {“Message”: “FooBar”}

In this example, we can efficiently query all messages from User1 and further refine this query to return messages within a certain timestamp range.

DynamoDB Indexing

Indexes in DynamoDB are special data structures that allow you to perform query operations on attributes other than the main primary key columns. There are two main types of indexes in DynamoDB:

Global Secondary Index (GSI)

  • GSIs allow you to have a completely different partition key and sort key from the main table.
  • You can create and delete GSIs at any time.
  • GSIs support eventually consistent and strongly consistent reads.

Local Secondary Index (LSI)

  • An LSI must have the same partition key as the main table, but a different sort key.
  • LSIs must be defined when the table is created and cannot be added later.
  • LSIs support only eventually consistent reads.

Example:

Suppose we have a table tracking customer orders, and we want to query these orders not only by the customer ID, but also by the order date. We define the customer ID as the partition key and order ID as the sort key. To query by order date, we can use a GSI that uses the order date as a partition key and the order ID as a sort key.

Table: Orders

CustomerID (PK) OrderID (SK) OrderDate Details
C123 O456 2021-04-12 {“Amount”: “$50”}
C789 O123 2021-04-11 {“Amount”: “$100”}
C123 O789 2021-04-13 {“Amount”: “$75”}

GSI: OrdersByDate

OrderDate (PK) OrderID (SK) CustomerID Details
2021-04-11 O123 C789 {“Amount”: “$100”}
2021-04-12 O456 C123 {“Amount”: “$50”}
2021-04-13 O789 C123 {“Amount”: “$75”}

With the GSI, you can easily query the Orders table based on the order date regardless of which customer made the order.

Understanding the Impact of Keys and Indexes

Keys and indexes are the main building blocks for creating efficient access patterns in DynamoDB. They allow developers to run queries and scans efficiently to avoid full table scans, which can be resource-intensive and costly.

AWS Certified Developers are expected to understand these concepts deeply to optimize DynamoDB usage – not only from a performance standpoint but also in terms of cost since DynamoDB pricing can be significantly impacted by read/write throughput and indexed data.

Answer the Questions in Comment Section

True or False: In Amazon DynamoDB, a primary key can consist of only a partition key.

  • A) True
  • B) False

Answer: A) True

Explanation: A primary key in Amazon DynamoDB can be a simple primary key, which is only a partition key, or a composite primary key, which includes both a partition key and a sort key.

True or False: The maximum size for a partition key value in DynamoDB is 256 bytes.

  • A) True
  • B) False

Answer: A) True

Explanation: The maximum size for a DynamoDB partition key value is 256 bytes. The sort key can also be up to 256 bytes.

How many secondary indexes are allowed per table in DynamoDB?

  • A) 5
  • B) 20
  • C) 10
  • D) No limit

Answer: B) 20

Explanation: DynamoDB allows up to 20 global secondary indexes and 5 local secondary indexes per table.

What type of index in DynamoDB maintains the same partition key as the base table but a different sort key?

  • A) Global Secondary Index
  • B) Local Secondary Index
  • C) Both A and B
  • D) Neither A nor B

Answer: B) Local Secondary Index

Explanation: A Local Secondary Index maintains the same partition key as the base table but has a different sort key.

True or False: You can only query data in DynamoDB using the primary key.

  • A) True
  • B) False

Answer: B) False

Explanation: While the primary key often provides the most efficient way to query data, you can also query data using secondary indexes.

True or False: It is possible to add a Local Secondary Index to an existing DynamoDB table after the table has been created.

  • A) True
  • B) False

Answer: B) False

Explanation: Local Secondary Indexes must be defined at table creation time. Once a table is created, you cannot add a Local Secondary Index to it.

In DynamoDB, what do you need to specify in order to perform a query action?

  • A) A partition key value
  • B) A sort key value
  • C) Both A and B
  • D) Neither A nor B

Answer: A) A partition key value

Explanation: To perform a query action, you need to specify a partition key value. You can also specify a sort key value if needed.

True or False: When using a Global Secondary Index in DynamoDB, the index can have a completely different key schema than the base table.

  • A) True
  • B) False

Answer: A) True

Explanation: A Global Secondary Index can have a completely different key schema from the base table.

Which consistency model does DynamoDB use when reading data from a Global Secondary Index?

  • A) Strongly consistent
  • B) Eventually consistent
  • C) Both A and B
  • D) Neither A nor B

Answer: C) Both A and B

Explanation: DynamoDB supports both strongly consistent and eventually consistent reads on Global Secondary Indexes.

True or False: In DynamoDB, the range key allows you to create hierarchical (one-to-many) relationships.

  • A) True
  • B) False

Answer: A) True

Explanation: The sort key, also known as the range key, is used to create hierarchical (one-to-many) relationships in DynamoDB.

What is the term used for the automatic partitioning and distribution of tables across many servers in DynamoDB?

  • A) Vertical scaling
  • B) Horizontal scaling
  • C) Sharding
  • D) Partitioning

Answer: C) Sharding

Explanation: DynamoDB automatically partitions and distributes the tables across multiple servers for scalability, a process known as sharding.

True or False: The partition key and sort key of an item in DynamoDB are collectively known as the item’s index.

  • A) True
  • B) False

Answer: B) False

Explanation: The partition key and sort key are collectively known as the item’s primary key. Indexes are separate constructs used to allow more efficient access patterns.

0 0 votes
Article Rating
Subscribe
Notify of
guest
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lidija Ognjanović
7 months ago

Great blog post! I finally understand the concept of partition and sort keys in DynamoDB.

Diana Clarke
7 months ago

Could someone explain how DynamoDB’s local secondary indexes (LSIs) differ from global secondary indexes (GSIs)?

Frederick Byrd
6 months ago

Appreciate the concise explanation on GSIs. They can be tricky to understand sometimes.

Christina Rose
7 months ago

Thank you for clarifying secondary indexes, exactly what I needed before my exam.

Andreia Spijksma
7 months ago

In a high-read-demand scenario, which type of key or index would be more efficient for querying large datasets?

Onni Annala
7 months ago

Great breakdown of partition and sort keys. This will definitely help me in optimizing my table designs.

Melike Dağdaş
7 months ago

One question: How does DynamoDB handle indexing when it comes to eventual consistency?

Nick Jones
7 months ago

Thanks for the detailed info. This post is a sanity saver!

21
0
Would love your thoughts, please comment.x
()
x