Skip to main content

DynamoDB

Key Concepts

  1. Table: Table in DynamoDB is very similar to table in relational databases. Just how you group a set of related data records together that have the same structure
  2. Item: This is what identifies as a single data record in the table. Every item in the table is uniquely identified by the primary key of the table. Similar to a row in a relational databases
  3. Attributes: Data that are attached to a single item. A simple attribute could be the age of a user in a User table. This is similar to a column in the relational database. However, DynamoDB does not require attribute on items except the attributes that are part of the primary key

Primary Key

Every item is identified by a primary key. This must be defined at the creation of the table and must be provided when inserting a new item.

There are two types of primary key used for DynamoDB

1. Simple primary key

Simple primary jet is just similar to standard key-value stores. You have one key mapped to the value.

Simple primary key is also referred to as a partition key, the partition key you specified will be used as part of a hash calculation, which determines which partition the actual record will be stored.

2. Composite primary key

A little bit more complex, composite primary key you would specify both a partition key and a sort key. The sort key is used to sort the items within the same partition.

The partition key works the same as the simple primary key, it will be used as part of the hash calculation in order to determine which partition the record will be end up at. Then items that are part of the same partition, i.e. have the partition key value, will be ordered by the sorting key.

So it is possible a value to have same partition key, however, they cannot have the same sorting key.

Secondary indexes

The primary key uniquely identifies an item in the table. However, sometimes you have additional access patterns that requires querying with attributes that are not part of the primary key. It would be easier to query with something else.

Secondary indexes give you this ability of query with things other than the primary key.

Again two type of secondary indexes:

1. Local secondary index

Local secondary index uses the same partition key as the table but you can specify a different sorting key. This allows you to basically query but with a different sorting key

2. Global secondary index

Global secondary index on the other hand allows you to define an entirely different primary for the table, you can pick a different partition key and the sorting key.

If you make a secondary index you can pick which attributes will be copied or projected to the new index that you have created, or basically which field it will show up when you query the secondary index. At minimum if you don't specify anything then DynamoDB will output the primary key that are used in the base table and on the secondary index.

More about attributes

Since DynamoDB is a NoSQL database, most attributes are not required for inserting for every item. If you don't specify it then DynamoDB will just not put anything for it, it is more flexible compared to SQL databases.

The only exception is that the primary key must have attribute(s) (depending on whether or not it is a simple or composite primary key).

Attribute types

Attribute types ranges from strings, numbers, lists, maps, and sets.

The way that you specify types in AWS CLI is using short notation of types such as "S" for strings and "N" for numbers.