amazon-web-servicesamazon-dynamodbdynamoose

What are hash keys and range keys in dynamodb and how to use them


I was currently looking into dynamodb documentation and it is not clear about what hash keys and range keys are and how should they be used.

I just need a basic explanation of what they are and how I am supposed to use them so that I can move forward with using it.


Solution

  • I like to think of it like this:

    For example, if you had an Invoices table, then the primary key would the Invoice Number. If you had a Login table, then the primary key would be User ID + Timestamp because one user could have multiple logins.

    Behind-the-scenes, the Partition Key is also used to distribute data amongst servers. This is how DynamoDB assures its high speed — when there's more data, it is distributed across more servers.

    If a table (such as the Login table) has multiple entries for a given Partition Key (eg User ID), then the addition of the Sort Key ensures the uniqueness of the Primary Key so that the Item can be stored and retrieved quickly.

    Hash Key = Partition Key

    Range Key = Sort Key