DynamoDB
Partition Keys
AWS Database
Querying Data
NoSQL

DynamoDB List all partition keys

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Overview of Amazon DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service offered by Amazon Web Services (AWS) that provides fast and predictable performance with seamless scalability. DynamoDB allows developers to offload the administrative burdens of operating and scaling a distributed database, so they don’t have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.

DynamoDB is particularly well-suited for applications that require consistent, single-digit millisecond latency at any scale. It is commonly used for mobile, web, gaming, IoT, and other applications.

Key Features of DynamoDB

  • Fully Managed: Eliminates the need for complex database management tasks.
  • High Performance: Offers low-latency response times.
  • Scalability: Automatically scales throughput capacity as per the demand.
  • Multi-Region, Multi-Master: Supports active-active partitions across regions.
  • Serverless: The on-demand mode enables automatic scaling without provisioning.

Partition Keys in DynamoDB

DynamoDB uses primary keys to uniquely identify each item in a table, and there are three primary key types:

  1. Simple Primary Key
  2. Composite Primary Key

Simple Primary Key

In the simple primary key, each item in a table is uniquely identified by a single attribute known as the partition key (or hash key). This key is fed into an internal hash function to determine the partition (physical storage internal to Amazon DynamoDB) in which the item will be stored.

Technical Explanation

The choice of a partition key is crucial for the performance of your application. The partition key should be a unique attribute if possible, to avoid creating "hot partitions".

Example: If you are designing a user table, a potential partition key might be UserID, assuming this is uniquely identifiable per user.

json
1{
2    "UserID": "12345",
3    "UserName": "john_doe",
4    "Email": "[email protected]"
5}

Composite Primary Key

In contrast to the simple primary key, a composite primary key consists of two attributes: the partition key and a sort key (also known as a range key). The partition key determines the partition, similar to the simple primary key, while the sort key allows for multiple items with the same partition key, differing only in the sort key attribute.

Technical Explanation

This type of key is beneficial for scenarios where items need to be clustered logically. It enables more complex queries and operations by leveraging both partition and sort.

Example: For an order table, you may choose CustomerID as the partition key and OrderDate as the sort key.

json
1{
2    "CustomerID": "54321",
3    "OrderDate": "2023-10-01",
4    "OrderTotal": 250.00
5}

Considerations in Choosing Partition Keys

When choosing partition keys, consider the following:

  • Uniform Distribution: Aim to distribute traffic evenly, avoiding hot spots.
  • Access Patterns: Determine the query patterns and design the keys to efficiently service those queries.
  • Cardinality: High-cardinality attributes make good partition keys.

Summary Table of Partition Keys

Key TypeDescriptionUse Case
Simple Primary KeyConsists of a single partition keyUniquely identifiable items like UserID
Composite Primary KeyCombines a partition key and a sort keyOrdered data such as CustomerID and OrderDate

Conclusion

DynamoDB offers robust flexibility in terms of scalability and performance, making it a popular choice for a wide range of applications. Understanding how to effectively use partition keys and adapt them to your access patterns is crucial for leveraging the full potential of DynamoDB.

With this insight into partition keys, you're better equipped to design a DynamoDB schema that optimally serves your application's needs, ensuring reliability and efficiency.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.