DynamoDB adjacency list primary key
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon DynamoDB is a fully managed NoSQL database service provided by AWS that offers fast and predictable performance with seamless scalability. One common use case for DynamoDB is to model many-to-many relationships in a graph structure, for example, social media friends, organizational hierarchies, and product recommendations. An adjacency list is a popular method to represent such graphs, and designing its primary key is crucial for efficient read and write operations in DynamoDB.
Understanding the Adjacency List Model
In the adjacency list model, each record (or item) in the database represents a node in the graph along with a list of nodes it is directly connected to, known as its "neighbors". The key challenge with this model in the context of Amazon DynamoDB lies in the design of the primary key. DynamoDB's primary key uniquely identifies each item and affects how data is partitioned and accessed.
Primary Key Design
DynamoDB's primary key can be of two types: a simple primary key or a composite primary key. For the adjacency list model, we often use composite primary keys, which consist of a partition key and a sort key, to efficiently group and query connected nodes.
Composite Primary Key Structure
- Partition Key: Represents the node (entity).
- Sort Key: Represents the connection or relationship of the node.
By using a composite primary key, queries can efficiently retrieve all neighbors of a particular node without scanning the entire dataset.
Example Structure
Let's consider a hypothetical social networking platform where each user has a unique identifier (UserID). Here's how the tables might be structured:
- Table:
UserConnections - Partition Key:
UserID - Sort Key:
ConnectedUserID - Additional Attributes:
ConnectionType,Timestamp
In this schema, each item in the table represents an individual connection between a user and one of their connections.
Example Data
| UserID | ConnectedUserID | ConnectionType | Timestamp |
| 1 | 2 | friend | 1685702400 |
| 1 | 3 | friend | 1685702500 |
| 2 | 3 | follower | 1685702600 |
To find all friends of user 1, you perform a query with the partition key set to 1, and you'll receive user IDs 2 and 3.
Query Patterns
In an adjacency list model in DynamoDB, common query patterns include:
- Find all nodes connected to a specific node: Using the partition key to fetch all items associated with a specific node efficiently.
- Check connection between two nodes: Query using the partition key and sort key to check if a specific connected node exists.
- Find all connections with specific characteristics: Use additional attributes like
ConnectionTypeandTimestampfor conditional filtering.
Query Example
Suppose we want to retrieve all friends of user 1. Here's how the query might look using AWS SDKs or queries in DynamoDB:
Advantages and Considerations
Advantages
- Scalability: Efficient reads using primary key.
- Flexibility: Easy to add more attributes or relationships.
Considerations
- Item Size Limit: DynamoDB imposes a 400 KB size limit per item, so large graphs might require additional modeling strategies.
- Consistency: Consider eventual vs. strong consistency based on application needs.
- Partition Key Design: Ensures balanced workload distribution by evenly spreading data across partitions.
Summary Table
| Key Component | Description |
| Partition Key | Represents the primary node or entity. |
| Sort Key | Represents a connection or related entity. |
| Data Retrieval | Efficient retrieval of a node's connections through partition key. |
| Flexibility | Easily accommodates additional connection attributes. |
| Scalability | Handles large datasets with effective partitioning. |
| Considerations | Careful schema design to meet item size and consistency needs. |
Conclusion
Designing an adjacency list in DynamoDB requires careful consideration of how you structure your primary keys to ensure optimal query performance and scalability. Using composite primary keys allows DynamoDB to efficiently manage graph data, making it suitable for applications involving complex relational data.
Related reading
- DynamoDB and User Login table
- DynamoDB API How can I build an add JSON attribute if not present update request?
- DynamoDb Batch write update
- DynamoDB BatchGet Get results in same order as provided Keys
- DynamoDB concurrent write
- DynamoDB condition ttl
- DynamoDB Is adding an item using list_append atomic?
- DynamoDB M-M Adjacency List Design Pattern

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.