Amazon DynamoDB is a managed NoSQL database service provided by Amazon Web Services (AWS). Data in DynamoDB is organized with tables, and each table is required to have a primary key. This primary key uniquely identifies each item in the table, which Amazon DynamoDB uses to distribute the data across different partitions for efficient querying.

Table of Contents

Primary Keys in DynamoDB

DynamoDB supports two types of primary keys:

  • Simple Primary Key: A simple primary key is known as “Partition Key” in DynamoDB. For a DynamoDB table with a simple primary key, the key value must be unique within the table.
  • Composite Primary Key: This type of key has two components: a partition key and a sort key. In a table with a composite primary key, both the partition key and the sort key must be provided to fully identify a classic item. The partition key must be a unique value, but the sort key does not need to be.

Indexes in DynamoDB

Indexes are vital in DynamoDB to provide more querying flexibility to the database. DynamoDB supports two kinds of indexes:

  • Global Secondary Indexes (GSI): A global secondary index features a key schema that can be different from the original table. You can think of global secondary indexes as “views” on your table, which allow you to specify alternate keys to retrieve your data.
  • Local Secondary Indexes (LSI): A local secondary index shares the same partition key as the base table, but has a different sort key, allowing more complex querying on a single partition.

Here’s a comparison of these two indexes:

Type Partition Key Sort key Provisioned throughput
GSI Can be different from the table Can be different from the table Separate from the table
LSI Same as the table Can be different from the table Shared with the table

Use Cases and Examples

Consider a table named “Students” with “StudentID” as the partition key and “CourseID” as the sort key. You can directly access any item in the table by providing the “StudentID” and “CourseID”.

If you want to fetch the data based on the “CourseID”, you would have to scan the entire table which is not efficient. In this case, you can create a GSI where “CourseID” is the partition key.

Here’s an example of creating a GSI:

table = dynamodb.create_table(
TableName='Students',
...
GlobalSecondaryIndexes=[
{
'IndexName': 'CourseIndex',
'KeySchema': [
{
'AttributeName': 'CourseID',
'KeyType': 'HASH' #Partition key
},
],
'Projection': {
'ProjectionType': 'ALL',
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1,
}
},
],
...
)

In this example, a GSI named “CourseIndex” is created using “CourseID” as the partition key.

To summarise, understanding keys and indexing is crucial to effectively manage data in Amazon DynamoDB. Primary keys uniquely identify each item in a table and distribute the data across different partitions. Indexes, whether global or local, provide more flexibility in the way data is queried. Remember to wisely choose your keys and indexing depending on your application’s specific needs to ensure optimal performance.

Practice Test

Multiple Choice Question: What are the two types of keys in Amazon DynamoDB?

  • A. Primary and Secondary
  • B. Partition and Sort
  • C. Local and Global
  • D. Range and Hash

Answer: B. Partition and Sort

Explanation: Amazon DynamoDB mainly uses two types of keys for data access purposes: Partition key and Sort key.

True/False: The primary key attribute(s) must all be unique in Amazon DynamoDB.

Answer: True.

Explanation: The primary key uniquely identifies each item in a table, thus it must be unique for every item.

Multiple Select Questions: Which of the following statements are true about Partition Key and Sort Key in DynamoDB?

  • A. A partition key is used to distribute data across partitions for scalability
  • B. A sort key allows us to order the data within each partition
  • C. A partition key should be evenly distributed to optimize performance
  • D. A sort key is also known as hash key

Answer: A, B, C

Explanation: All statements are true except D. A partition key is also known as the hash key.

True/False: Secondary indexes in DynamoDB are used to query data with attributes other than the primary key.

Answer: True

Explanation: Secondary indexes can be understood as ‘sub-tables’ within your main table, which you can create to query your data based on non-primary key attributes.

Multiple Choice Question: How many types of secondary indexes does DynamoDB support?

  • A. 1
  • B. 2
  • C. 3
  • D. 4

Answer: B. 2

Explanation: DynamoDB supports two types of secondary indexes: Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI).

True/False: A secondary index automatically has the same throughput settings as its main table in Amazon DynamoDB.

Answer: False

Explanation: Throughput settings for secondary indexes in DynamoDB are managed separately from the main table.

Multiple Select Questions: Which of the following statements are true about Global Secondary Indexes (GSIs)?

  • A. GSIs must have the same partition key and sort key as the base table
  • B. GSIs can have separate partition key and sort key as the base table
  • C. GSIs can have different throughput limits than the base table
  • D. GSIs cannot be queried directly

Answer: B, C

Explanation: Global Secondary Indexes can have different partition, sort keys and different read/write capacity settings than the base table. Also, it can be queried directly.

True/False: Amazon DynamoDB supports consistent reads on the table data and secondary index data.

Answer: True

Explanation: DynamoDB supports both eventual consistent reads (default) and strongly consistent reads on the table and secondary index data.

Multiple Choice Question: Partition key’s value in DynamoDB is also called __

  • A. Hash attribute
  • B. Key attribute
  • C. Value attribute
  • D. Sort attribute

Answer: A. Hash attribute

Explanation: The partition key of DynamoDB is also referred to as a hash attribute.

True/False: You cannot delete a global secondary index from a table after it is created.

Answer: False

Explanation: You can delete a Global Secondary Index even after the table is created.

Multiple Choice Question: What are DynamoDB Streams?

  • A. A series of sorted logs for each database operation
  • B. A way to replicate DynamoDB data across AWS regions
  • C. A global secondary index
  • D. A method of data backup

Answer: A. A series of sorted logs for each database operation

Explanation: DynamoDB Streams captures table activity, and each stream record appears exactly once in the stream.

True/False: DynamoDB automatically manages indexes so that they don’t exceed their provisioned throughput

Answer: True

Explanation: DynamoDB offers automatic scaling of throughput capacity, ensuring that indexes do not exceed their provisioned throughput.

Multiple Choice Question: Maximum length of the partition key value in DynamoDB is:

  • A. 2KB
  • B. 256 KB
  • C. 1MB
  • D. 512 KB

Answer: A. 2KB

Explanation: The maximum length of a partition key value is 2 KB.

True/False: Local Secondary Index (LSI) provides an alternate sort key for a given partition key value.

Answer: True

Explanation: LSI provides an alternate sort key for a given partition key value along with different subset of attributes.

Multiple Choice Question: What is the maximum limit of GSIs that can be created per table in DynamoDB?

  • A. 5
  • B. 10
  • C. 20
  • D. 25

Answer: C. 20

Explanation: Amazon DynamoDB allows up to 20 GSIs per table by default.

Interview Questions

1. What are the two types of primary keys used in Amazon DynamoDB?

Partition key and composite key.

2. How does DynamoDB distribute data for a table with a composite primary key?

DynamoDB distributes data by the partition key value, and the sort key value is used to order items with the same partition key.

3. How are local secondary indexes different from global secondary indexes in DynamoDB?

Local secondary indexes have the same partition key as the table but a different sort key, while global secondary indexes have different partition and sort keys from the table.

4. What is the primary purpose of secondary indexes in DynamoDB?

Secondary indexes allow querying a table based on attributes other than the primary key to improve query performance.

5. Can you add a secondary index to an existing DynamoDB table?

Yes, you can add secondary indexes to existing tables in DynamoDB.

6. How does DynamoDB handle the indexing of items with the same partition key?

Items with the same partition key are stored in sort key order, which allows for efficient querying using the sort key.

7. What is the purpose of a sparse index in DynamoDB?

Sparse indexes allow for efficient querying when a subset of items in a table have a particular attribute.

8. How does DynamoDB handle indexing of attributes with different data types?

Attributes with different data types are indexed separately by DynamoDB, allowing for efficient querying based on different data types.

9. Can you query DynamoDB using secondary indexes even if the primary key of a table is not known?

Yes, secondary indexes enable querying a table based on attributes other than the primary key, providing flexibility in query options.

10. What is the main benefit of using secondary indexes in DynamoDB?

Secondary indexes improve query performance by allowing efficient access to data based on attributes other than the primary key.

Leave a Reply

Your email address will not be published. Required fields are marked *