Amazon DynamoDB Tutorial 0/30 lessons ~6 min read Lesson 11

    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.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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#p9 lists the product's categories.
    • A GSI with SK as its partition key and PK as its sort key (the inverted index) lets you Query CATEGORY#books to 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):

    bash
    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:

    ts
    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 (INCLUDE projection).
    • 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.

    2 questions
    1IntermediateQuestionWhat is the adjacency list pattern?+

    Answer

    Storing each relationship (edge) as an item whose partition key is one node and sort key is the other node. Querying a partition key lists its neighbours; an inverted GSI lists neighbours in the opposite direction.
    2AdvancedQuestionWhat is the main scaling risk of an inverted index?+

    Answer

    Popular target nodes concentrate many edges under one GSI partition key, creating hot partitions and GSI write throttling that can back-pressure base-table writes. Shard popular keys or project fewer attributes.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.