DynamoDB is a part of Amazon Web Services. It is a NoSQL database, which supports key-value and document data structures.
In this blog, we will be discussing Core components of DynamoDb.
Features of DynamoDb:
- It is a fully managed NoSQL database.
- It can store & retrieve any amount of data, and can serve any amount of traffic.
- To maintain fast performance, it distributes data and traffic over servers.
- It provides a provisioned-throughput model through which read and write units can be changed any time according to the requirement of application.
- Using Amazon’s IAM (Identity and access management) service, we can apply security and access control over the database.
Core Components of DynamoDB:
The core components of DynamoDb are Tables, Attributes, and Items.
Table:
- In DynamoDb, Data is stored in tables in the form of items.
Items:
Every Item in DynamoDb is like a row, tuple or record of any other Database system.
- Item is a collection of attributes.
- Each item can have its own distinct attributes.
- A table can have 0 or more items.
- We can store as many items as we want, there is no limit.
Attributes:
Attributes are the fundamental element of item that requires no further decomposition. It stores information in the form of key-value pairs.
- Attributes are like Columns of other Database systems.
- We don’t need to define the attributes or their data types beforehand.
- We only need to define the Primary key.
- DynamoDb supports nested attributes up to 32 Levels deep.
DynamoDB supports the following data types:
- Scalar – Number, String, Binary, Boolean, and Null.
- Multi-valued – String Set, Number Set, and Binary Set.
- Document – List and Map.
Example 1:
A table named Minion with sample items and attributes.
Things to notice in Minion Table:
- Each item has a unique identifier (Primary key).
- In the Minion table, the primary key consists of one attribute (id).
- Other than primary keys, the Minion table is Schemaless.
- Nested attribute (traits).
Example 2:
Table Movie with sample items and attributes.
Things to notice in the Movie table:
- The primary key for Movie consists of two attributes (Director and MovieName).
- Each item in the table must have these two attributes.
- The combination of Director and MovieName distinguishes each item in the table from all of the others.
Primary Key
Primary key is used to uniquely identify an item in a table.
DynamoDB supports two different types of primary keys:
- Partition Key
- Partition-Sort key (Composite primary key).
Partition Key:
In this case, a primary key consists of a single attribute known as the partition key. It is also known as Hash attribute of an item.
DynamoDB uses an internal hash function to determine the partition (physical storage internal to DynamoDB) where the item is stored. The input to this hash function is the partition key’s value.
In a table or a Secondary Index, that has only one partition key, no two items can have the same partition key value. There is no limit on the number of distinct values of partition key. But there is a limit on its length i.e., minimum length of the value of partition key could be 1 byte and maximum is 2048 bytes.
Partition-Sort Key:
In this case, a primary key consists of two attributes i.e. Partition key and Sort key.
The values of two partition keys could be same but the values of sort key must be different.
All items with the same partition key values are sorted by sort key value and are stored together in the same partition.
By using both the values together (Patition key and Sort key) of an item, we can access any item in the table.
e.g. In table Movie, if we provide only the value of Director, DynamoDB retrieves all the movies by that director.
Points to Remember:
- The sort key is also known as Range attribute of an item. The term range attribute derives from the way DynamoDB stores items with the same partition key physically close together, in sorted order by the sort key value.
- Each primary key attribute must be a scalar (meaning that it can hold only a single value).
- The only data types allowed for primary key attributes are the string, number, or binary.
- If the sort key is well defined, it can be very helpful, One can use operators like – starts-with, between, >, <, etc.
Hope you liked this blog.
References:
Amazon DynamoDb developer guide.