Many-to-Many Relationships with Adjacency Lists
Many-to-many relationships — students and courses, products and categories, users and groups — need to be read from both sides.
Introduction
Many-to-many relationships — students and courses, products and categories, users and groups — need to be read from both sides. The adjacency list pattern stores each relationship as an edge item and uses an inverted index (a GSI that swaps PK and SK) to traverse it in the other direction.
Purpose of this lesson
You will model product–category membership so you can list the categories of a product and the products in a category, each with one Query.
Understanding the topic
Edge items and the inverted index:
- Edge item:
PK = PRODUCT#p9,SK = CATEGORY#books, plus attributes you want to display on either side. - Base-table Query on
PRODUCT#p9lists the product's categories. - A GSI with
SKas its partition key andPKas its sort key (the inverted index) lets you QueryCATEGORY#booksto list products. - Duplicate a few display attributes (product name, price) on the edge so the category listing needs no extra reads.
Syntax reference
Create the inverted index (on an existing table):
aws dynamodb update-table \--table-name ShopTable \--attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S \--global-secondary-index-updates '[{"Create": {"IndexName": "InvertedIndex","KeySchema": [{"AttributeName": "SK", "KeyType": "HASH"},{"AttributeName": "PK", "KeyType": "RANGE"}],"Projection": {"ProjectionType": "ALL"}}}]'
Informative example
Query both directions:
import { QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";// Edges: (PRODUCT#p9, CATEGORY#books), (PRODUCT#p9, CATEGORY#gifts), (PRODUCT#p3, CATEGORY#books)const categories = await ddb.send(new QueryCommand({TableName: TABLE,KeyConditionExpression: "PK = :p AND begins_with(SK, :c)",ExpressionAttributeValues: { ":p": "PRODUCT#p9", ":c": "CATEGORY#" },}));console.log(categories.Items?.map((i) => i.SK));const products = await ddb.send(new QueryCommand({TableName: TABLE,IndexName: "InvertedIndex",KeyConditionExpression: "SK = :c AND begins_with(PK, :p)",ExpressionAttributeValues: { ":c": "CATEGORY#books", ":p": "PRODUCT#" },}));console.log(products.Items?.map((i) => i.PK));
Output
[ 'CATEGORY#books', 'CATEGORY#gifts' ] [ 'PRODUCT#p3', 'PRODUCT#p9' ]
Best practices
- Project only what listing screens need if edge counts are large (
INCLUDEprojection). - Keep duplicated edge attributes small and update them from a Stream when the source changes.
- Use a separate category-meta item (
CATEGORY#books / META) for category details.
Common mistakes
- Very popular nodes (a category with millions of products) become hot partitions in the inverted index — shard the category key if needed.
- Forgetting that GSI reads are eventually consistent.
- Storing large duplicated attributes on every edge.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhat is the adjacency list pattern?+
Answer
2AdvancedQuestionWhat is the main scaling risk of an inverted index?+
Answer
Hands-on exercise
Hands-on: model customers and wish-list products as a many-to-many relationship. Implement wishlistFor(customerId) and whoWishes(productId) using the base table and the inverted index.
Summary
Adjacency lists store relationships as edge items; an inverted GSI makes the relationship queryable from both sides with one Query each.