Core concepts of Amazon DynamoDB

Agnel Nandapurapu
Tensult Blogs
Published in
5 min readJun 21, 2018

This blog has been moved from medium to blogs.tensult.com. All the latest updates are available there. Subscribe to our newsletter to stay updated.

What is DynamoDB? DynamoDB is a managed NoSQL database provided by AWS, and it is a highly scalable and reliable database. We can scale from 10 to 1000 transactions per second (tps) in couple of seconds. Since it is a managed service, we don’t need to worry about the underlying hardware, servers or operating system. Data stored in the DynamoDB is redundantly copied across multiple Availability Zones so by default it provides protection for data loss due to underlying hardware failures.

DynamoDB organises data as tables and each table contains several items (rows) and each item has Keys and Attributes (columns). The tables in the DynamoDB are non relational and non schema based. This means table joins are not supported at DB level. For most use cases, we don’t need table joins and those fit really well with DynamoDB.

Now let us discuss DynamoDB terminology.

Tables

When we create the table we specify Partition Key and an optional Sort Key, we can’t change these later but rest of the attributes (columns) of item (row) can change. Also each item can have different set of attributes.

Example

Let us say we want to store articles from various authors in DynamoDB:

Article_Table
Table Keys
Partition Key : authorId
Sort Key : publicationDate

Item 1 has mainImage but not mainVideo attribute, where as Item 2 has mainVideo but not mainImage attribute. This is possible in DynamoDB as it is non-schema based.

Item1 and Item3 have same partition key abc123 and with different sort keys, which is possible in dynamoDB.

There is no limit on how many items can be stored in a table.

Item

A DynamoDB item is nothing but a row in the table. We can change any attribute of an item except its keys: partition key or sort key, these keys are an identification for an item; if we have to change these keys, then the only option is to delete an item and create it again.

Data Types

DynamoDB supports different data types for attributes of an item, they can be mainly categorised into the following:

  • Scalar Types : Number, String, Binary, Boolean and Null.
  • Document Types : List and Map
  • Set Types : Number Set, String Set, and Binary Set.

Partition Key

This key is mandatory for the DynamoDB table and item. DynamoDB partitions the items using this key, that’s why this key is also called as the partition key and some times is also referred as a Hash Key.

Sort key

This key can be used in conjunction with the Partition key but it is not mandatory. This is useful while querying the data relating a Partition key. We can use several different filter functions on the sort key such as begins with, between etc. Some times it is also referred to as a Range Key.

Primary Key

Primary key is just a combination of both Partition key and Sort Key.

Batch APIs

BatchGetItem : This can be used to fetch items from different tables using Partition Key and Sort Key. In a single BatchGetItem call, we can fetch up to 16MB data or 100 items.

BatchGetItem can be performed only on tables not on secondary indexes.

BatchWriteItem: This can be used to delete or put items on one or more tables in DynamoDB in one call. We can write up to 16 MB data, which can be 25 put and delete requests.

BatchWriteItem cannot update items, for that use UpdateItem API call

Query

To query table we must pass partition key so selecting proper partition key for the table is important. Query operation will return all items that are matched with partition key of the table. Sort Key is further useful to filter and sort items but it is optional.

Scan

Scan operation does’t require Partition Key or Sort Key to fetch results from the table. As the name suggests, it scans an entire table to filter results based on attribute values passed as filters.

Pagination

DynamoDB Query/Scan results return maximum of 1MB response size so if our request matches an items of size more than 1MB, it gets automatically divided into pages. In such cases DynamoDB returns a special response parameter “LastEvaluatedKey” and this can be used to fetch next page results. Please note we need to pass the value of “LastEvaluatedKey” as “ExclusiveStartKey” in the next request to DynamoDB.

In some cases we might want to fix page size to number such as 10 or 20 results per page. In those cases we can use the “Limit” parameter. Please note if the results matching to the “Limit” is more than 1MB then DynamoDB only returns subset of the results which fits to 1MB limit.

Sorting

When we use Query/Scan operation on a DynamoDB table, then by default the results are sorted based on Sort Key value of the table. Incase we want that results in reverse order then we need to pass “ScanIndexForward” as “false” in query/scan request parameters. If the data type of Sort key is a number, then the results will be in a numeric order, otherwise, results will be in UTF-8 bytes. By default sort order is ascending. To get results in a descending order, pass “ScanIndexForward” as “false”.

How to call DynamoDB APIs programmatically ?

AWS SDK for DynamoDB is supported for several languages, which can be used to interact with DynamoDB API seamlessly. We can also use DynamoDB using AWS CLI.

Pricing

Last but not least… let us see how is DyanamoDB is priced.

Free Tier :

Throughput limit : 200 million requests per month ( 25 read and 25 write capacity units)

Stream limit : 2.5 million read requests per month.

Storage : 25GB of indexed data storage.

Note : Above free tier does not end after 12 months.

For more information about pricing click here

What next?

Now that we have learnt basics of DynamoDB, I will be discussing more concepts related to DynamoDB in my future blogs. So if you want me to particularly focus on any topic of DynamoDB, then please leave a comment on this blog.

--

--