AWS DynamoDB — Primary Key and Secondary Index


Our team has mostly been using S3 as a storage for the data. In the new project, I had a chance to explore a AWS’s NoSQL service, DynamoDB. With defined use cases in the project, I was responsible for finalizing a schema.

In the next couple posts, I would like to share my design process that includes some interesting implementation of DynamoDB. But first, this is a short summary of two important concepts in DynamoDB — Primary Key and Secondary Indexes. This document contains the full details and some examples.

Primary Key

  • Primary key uniquely defines each item in the table
  • DynamoDB supports two different kinds of primary keys: partition key and composite key
  • Partition key is a simple primary key composed of a single attribute. DynamoDB uses the partition key’s value as input to an internal hash function. The output of the function determines the partition in which the item will be sorted
  • Composite key is composed of partition key and sort key. The item with the same partition key are stored together in sorted order by sort key value. Composite primary key gives flexibility when querying data.

Secondary Index

  • Secondary index lets you query the data in the table using an alternative key, in addition to queries against the primary key
  • DynamoDB supports two kinds of indexes: Global secondary index (GSI) and Local secondary index (LSI)
  • Global secondary index (GSI) is an index with a partition key and sort key that are different from the those on the base table
  • Local secondary index (LSI) is an index with the same partition key as the base table, but a different sort key



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store