DynamoDB
adjacency list
primary key
database design
NoSQL

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.

Practice system design

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

  1. Partition Key: Represents the node (entity).
  2. 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

UserIDConnectedUserIDConnectionTypeTimestamp
12friend1685702400
13friend1685702500
23follower1685702600

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 ConnectionType and Timestamp for 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:

python
1import boto3
2
3# Initialize DynamoDB resource
4dynamodb = boto3.resource('dynamodb')
5
6# Reference the UserConnections table
7table = dynamodb.Table('UserConnections')
8
9# Query to retrieve friends
10response = table.query(
11    KeyConditionExpression=Key('UserID').eq('1') & Key('ConnectionType').eq('friend')
12)
13
14items = response['Items']
15print(f"Friends of user 1: {items}")

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 ComponentDescription
Partition KeyRepresents the primary node or entity.
Sort KeyRepresents a connection or related entity.
Data RetrievalEfficient retrieval of a node's connections through partition key.
FlexibilityEasily accommodates additional connection attributes.
ScalabilityHandles large datasets with effective partitioning.
ConsiderationsCareful 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
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.