Introduction:
DynamoDB, an AWS NoSQL service, provides efficient and scalable data storage. When creating a DynamoDB table, it is crucial to understand primary keys and partitions. This article will delve into the two types of primary keys, simple and composite, and explain how partitions work in DynamoDB.
1. Simple Key:
A simple key consists of a partition key, which is a unique identifier used for grouping items within a table. DynamoDB organizes data based on this partition key, allowing quick retrieval of related items. In the below table, the partition key is defined as notes id.
If you attempt to create another primary key with the same value in a simple key structure, it will result in an error since the partition key must be unique.
2. Composite Key:
A composite key combines a partition key and a sort key. This key type overcomes the limitations of a simple key by allowing multiple attributes with the same partition key, as long as the sort key is unique. If a combination of partition key and sort key matches an existing entry in the table, an error will occur.
In the below image Release year has been defined as Primary Key and Movie name as Sort key. As you can see we have many items with the same Release year (Primary Key) and different Movie Name (Sort Key)
Understanding Partitions:
Partitions play a vital role in DynamoDB's data storage and retrieval process. Each partition key is hashed by a request router, which determines the partition in whichthe data will be stored. Items with the same partition key share the same hash function and are stored in the same partition. If a sort key is present, items are stored within the partition in ascending order based on the sort key's value.
Partition Limits and Provisioning:
Each partition in DynamoDB can store data up to 10 GB. Partition management is handled by AWS, and users can provision partitions with a maximum of 3000 Read Capacity Units (RCUs) and 1000 Write Capacity Units (WCUs).
To determine the minimum number of partitions required, the following formula can be used:
(Number of RCU/3000) + (Number of WCU/1000)
For example, if you have 1500 RCU and 500 WCU:
(1500/3000) + (500/1000) = 0.5 + 0.5 = 1 partition
If you have 3000 RCU and 500 WCU:
(3000/3000) + (500/1000) = 1 + 0.5 = 1.5, rounded to 2 partitions.
DynamoDB scales and manage partitions automatically based on the workload and storage requirements.
It's important to note that RCU and WCU are equally distributed among the available partitions. If the request rate to a partition exceeds its provisioned capacity, throttling may occur. Adaptive provisioning is a technique used to share RCU and WCU units among partitions to avoid such issues.
Choosing Partition Keys:
Selecting a suitable partition key is crucial for efficient data distribution and preventing hot partitions. It is recommended to choose a partition key with high cardinality values to evenly distribute the request rate across partitions.
Conclusion:
Understanding primary keys and partitions in DynamoDB is essential for designing scalable and performant database tables. By selecting appropriate primary key types and carefully choosing partition keys, you can optimize data retrieval and avoid bottlenecks in your DynamoDB applications.
Top comments (0)