An introduction to DynamoDB

Amazon DynamoDb is a NoSQL database provided as a service by the cloud giant Amazon through its Amazon Web Services platform. DynamoDb is basically the talk of the town due its features such as high availability, scalability and throughput it provides. We can store huge amounts data without any worries on the hardware requirements and configurations to be taken care of as that part would be automatically handled and maintained by Amazon. This provides greater convenience and also proves to be a great time saver in the development efforts and hence has become one of the leading cloud-based databases.

Basic building blocks

Now that we have seen a basic introduction on what DynamoDB is, let us have a look into basic building blocks of DynamoDb to understand it. There are three basic building blocks for this , they are tables, items and attributes.

1. Table

Tables are a group of items. To put it into perspective, this is analogous to the databases in the SQL world. The data is stored in multiple tables in DynamoDB. Now inside a table, there will be multiple items and attributes.

2. Items

Items are a group of attributes. Items are analogous to the rows in a table in the SQL databases. An item in a table would be unique. There would be a unique identifiable attribute or a set of attributes whose combination would be unique to identify an item.

3. Attributes

Attributes are specific information in an item. This is similar to the “columns” in the SQL world. The figure below shows a more graphical representation of the tables, items and attributes:

Amazon DynamoDB Attributes

Data handling in Amazon DynamoDB

Regarding the data handling in the DynamoDb, there are two types of primary keys that can be defined in a table, named partition keys and sort keys.

1. Partition Key

Partition key or hash attribute is a uniquely identifiable attribute in an item in a DynamoDB. This essentially means that, in a table where there is only a partition key, there will be no two items that will have the same partition key value.

There is an internal function for DynamoDb which accepts the partition key value and determines on which physical partition the item gets stored. Thing to be noted regarding the partition key is that the data types are restricted to be string, number of binary.

2. Composite Keys (partition key and sort key)

As indicated by the name, this is a key composed of two attributes, one being a partition key and the other one being a sort key. Sort key is also called the range key. This key’s main purpose is to make the storage of the items in the partition in a specific order. That is, depending on the partition key DynamoDb determines the storage location and during the time of storage, the sort key will determine in which order the items are to be stored. This helps greatly in saving search speed. In such a scenario, there can be multiple items with the same partition key, but different sort keys.

A simple Table creation in Amazon DynamoDB

Let us see some a simple CRUD operation using DynamoDb using the AWS console.

The pre-requisites for proceeding further is to have an AWS account so that we can log-in to the AWS console.
After logging in to the console, in the search bar type in “Dynamo DB” and hit search. Click on the resulting Dynamo DB icon and we will be directed to the DynamoDB console, which shows the list of tables we have (if any) as below:

 Table creation in Amazon DynamoDB

As shown in the above figure, you can see the most basics of the details such as table name, the status of the tables (whether it is being active or not), the type of keys existing in the table etc.

Now let us create a table using the console. All we have to do is to click on the “create table” button (can see the same in the above picture) on the console screen. The resulting screen would look like below:

 Table creation in Amazon DynamoDB

In the above screen, as you can see there is provision to enter the name of the table (Table name), the primary key’s field name and the sort key’s key name. Here we are taking the employee data for sample and then gave the table name as “employee”, the partition key as “eid” which denotes the employee id and the sort key as “ename”, which denotes the employee name. As you can see the “eid” and “ename” fields have the option for setting data type (in the drop downs provided adjacent to them). After entering the required fields, hit the “create” button at the right bottom of the page.
After hitting the create button, the table would be created and we will be directed to the table listing page, where we can see the newly created table in the list as shown below:

 Table creation in Amazon DynamoDB

Now select the corresponding table by clicking on the required table (here you can see I have selected the “employee” table). Now click on the “create item” button towards the bottom right of the listing table. A new window will pop-up which has the options to enter the items to the table as shown below:

aws DynamoDB

As you can see from the above table we can enter the new attributes to an item by clicking the + button in the above screen. By clicking the + button, it will ask for the data type, then for the field name and then we can enter value for the key which we have entered. Hitting on the “save” button would save the item to the table.

In such a manner, I have added 3 items to the employee table for demonstration purposes.

Search in Amazon DynamoDB

One of the most essential parts of a database is the way we can search it. DynamoDb provides two ways for searching the database:

1. Query method

Query method, search is restricted to the primary key attribute values. It also supports a few comparison operators on attribute values to aid in the search process. In the below example, I have used the query method (shown with the red circled 1) to search for the record with eid =1 and ename = “nikesh varma” :

 DynamoDB Query method

2. Scan method

Scan operation, unlike the query operation, will scan the entire table. Here filters can be specified to applied on the results to make search results accurate. In the figure below,  I have scanned the table for the year 2018 on the field “join_year” and the results are the records with the value 2018 in the field “join_year”:

DynamoDB Scan method

When to use Amazon DynamoDB?

Unlike the yesteryears, the data generation and consumption of web-based applications is huge. Also, there are chances that an application might be a huge success overnight, hugely contradicting the expectations of it architecting team. Such applications and surprise cases can be handled by using a database service like Amazon DynamoDb, where scaling to any extent without any downtime is automatically taken care of.

Conclusion

In this blog, we had a brief overview of what is Amazon DynamoDb, what are its basic components and how simple table creation can be done in it. In the coming blogs, we will see some advanced operations on how to insert data to Amazon DynamoDb programmatically rather than from the AWS console. Also, there are some other interesting DBs in a picture, like AWS Aurora Db, which is the SQL solution, I will also introduce you to such interesting cloud databases in the future blogs.

 

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>