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

    Single-Table Design and Item Collections

    Single-table design stores several entity types in one table, using generic key attributes (PK, SK) whose values carry the entity type.

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

    Introduction

    Single-table design stores several entity types in one table, using generic key attributes (PK, SK) whose values carry the entity type. Items that share a partition key form an item collection, and one Query can return the whole collection — the DynamoDB equivalent of a join, done at write time.

    This lesson implements the core of ShopTable.

    Purpose of this lesson

    You will learn when single-table design pays off, how to lay out entity keys, and how to fetch related entities in one request.

    Understanding the topic

    ShopTable layout:

    • Every item has an entityType attribute so code (and Streams consumers) can tell items apart.
    • Sort keys that start with a type prefix let begins_with select one entity type from a collection.
    • ISO-8601 timestamps in sort keys sort chronologically as strings.
    • Duplicating small, rarely changing attributes (an order summary under the customer) trades storage for single-request reads.
    EntityPKSK
    CustomerCUSTOMER#c1PROFILE
    Order summary (under customer)CUSTOMER#c1ORDER#2026-11-19T10:00:00Z#o1
    Order headerORDER#o1HEADER
    Order lineORDER#o1LINE#001
    ProductPRODUCT#p9PRODUCT

    Informative example

    Write an order and read it back with its lines in one Query:

    ts
    import { BatchWriteCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    await ddb.send(new BatchWriteCommand({
    RequestItems: {
    [TABLE]: [
    { PutRequest: { Item: { PK: "ORDER#o1", SK: "HEADER", entityType: "Order", customerId: "c1", status: "PLACED", total: 59 } } },
    { PutRequest: { Item: { PK: "ORDER#o1", SK: "LINE#001", entityType: "OrderLine", sku: "BOOK-1", qty: 1, price: 39 } } },
    { PutRequest: { Item: { PK: "ORDER#o1", SK: "LINE#002", entityType: "OrderLine", sku: "PEN-4", qty: 2, price: 10 } } },
    ],
    },
    }));
    const { Items = [] } = await ddb.send(new QueryCommand({
    TableName: TABLE,
    KeyConditionExpression: "PK = :pk",
    ExpressionAttributeValues: { ":pk": "ORDER#o1" },
    }));
    for (const item of Items) console.log(item.SK, item.entityType);

    Output

    HEADER Order
    LINE#001 OrderLine
    LINE#002 OrderLine

    Items come back sorted by sort key (UTF-8 byte order), so HEADER sorts before LINE#…. To read only lines, add AND begins_with(SK, :line) to the key condition.

    Trade-offs

    • Fewer round trips and lower latency vs. a schema that is harder to read without documentation.
    • One table to operate vs. shared throughput and backup scope across entities.
    • Pre-joined data vs. duplicated attributes that must be kept in sync (often with Streams).

    Decision framework

    • Use single-table design when several entities are read together on hot paths and the access patterns are stable.
    • Use separate tables when entities have different lifecycles, retention, security boundaries, or teams — or when the workload is simple CRUD per entity.
    • Do not force unrelated data into one table just to follow the pattern.

    Best practices

    • Document the key layout (like the table above) in the repository.
    • Add an entityType attribute to every item.
    • Keep item collections small enough to page through quickly; large collections belong under their own partition key.

    Common mistakes

    • Putting unbounded child lists (all events for a popular product) under one partition key.
    • Overloading keys so much that new engineers cannot tell what an item is.
    • Duplicating attributes that change often, creating expensive fan-out updates.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    2 questions
    1BeginnerQuestionWhat is an item collection?+

    Answer

    All items in a table (and its local secondary indexes) that share the same partition key value. A single Query on that partition key returns the collection, sorted by sort key.
    2IntermediateQuestionWhen would you avoid single-table design?+

    Answer

    When entities are not read together, when access patterns are still changing, when different data needs different security, retention, or backup policies, or when the team would be slowed down by an opaque overloaded schema.

    Hands-on exercise

    Hands-on: add a CUSTOMER#c1 / PROFILE item and two order summaries under the customer with sort keys ORDER#<ISO timestamp>#<orderId>. Write listCustomerOrders(customerId) that returns summaries newest first using ScanIndexForward: false and begins_with(SK, "ORDER#").

    Summary

    Single-table design groups related entities under shared partition keys so one Query returns what a relational join would. It works best for stable, hot access patterns and needs clear documentation.

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