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

    Time-Series Data and Item Versioning

    Two patterns appear in almost every production table: time-ordered data (events, metrics, audit logs) and versioned items where you need the current state and the history.

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

    Introduction

    Two patterns appear in almost every production table: time-ordered data (events, metrics, audit logs) and versioned items where you need the current state and the history. Both rely on sort keys that sort chronologically.

    Purpose of this lesson

    You will query time ranges with BETWEEN, keep a fast “latest” item next to historical versions, and expire old data with TTL.

    Understanding the topic

    Patterns covered:

    • Time-range queries: sort keys like READING#2026-11-19T10:15:00Z support BETWEEN and >= key conditions.
    • Bucketing: add a time bucket to the partition key (DEVICE#d1#2026-11) so a busy device does not grow one partition forever.
    • Latest + history: store version items V#0001, V#0002 … plus a V#0000 item that always holds the current version.
    • TTL deletes expired items automatically at no write cost (Module 4 covers TTL events).

    Informative example

    Query one hour of readings:

    ts
    import { QueryCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    const { Items = [] } = await ddb.send(new QueryCommand({
    TableName: TABLE,
    KeyConditionExpression: "PK = :pk AND SK BETWEEN :from AND :to",
    ExpressionAttributeValues: {
    ":pk": "DEVICE#d1#2026-11",
    ":from": "READING#2026-11-19T10:00:00Z",
    ":to": "READING#2026-11-19T10:59:59Z",
    },
    }));
    console.log(`${Items.length} readings`);

    Keep timestamps in one fixed format (UTC, ISO-8601, same precision) — mixed formats break string ordering.

    Useful templates

    Versioned item with a current pointer (transaction)

    Writes the new history version and updates the V#0000 'current' item atomically; the condition prevents two writers from creating the same version.

    ts
    import { TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    export async function saveVersion(docId: string, expectedVersion: number, body: string) {
    const next = expectedVersion + 1;
    const sk = `V#${String(next).padStart(4, "0")}`;
    await ddb.send(new TransactWriteCommand({
    TransactItems: [
    { Put: {
    TableName: TABLE,
    Item: { PK: `DOC#${docId}`, SK: sk, version: next, body },
    ConditionExpression: "attribute_not_exists(PK)",
    } },
    { Update: {
    TableName: TABLE,
    Key: { PK: `DOC#${docId}`, SK: "V#0000" },
    UpdateExpression: "SET version = :next, body = :body",
    ConditionExpression: "attribute_not_exists(version) OR version = :expected",
    ExpressionAttributeValues: { ":next": next, ":body": body, ":expected": expectedVersion },
    } },
    ],
    }));
    }

    Best practices

    • Bucket high-volume series by day or month in the partition key.
    • Store timestamps in UTC ISO-8601 (or zero-padded epoch numbers) for correct ordering.
    • Set TTL on raw data and keep pre-aggregated rollups for long-term trends.

    Common mistakes

    • Using only the device id as the partition key for years of high-frequency data.
    • Mixing local times and UTC in sort keys.
    • Reading history to find the latest version instead of keeping a current item.

    Advanced interview questions

    Interview Prep

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

    2 questions
    1BeginnerQuestionHow do you get the latest item in a time-ordered collection cheaply?+

    Answer

    Query the partition with ScanIndexForward set to false and Limit 1, or maintain a dedicated 'latest' item updated on every write.
    2IntermediateQuestionWhy bucket time-series partition keys?+

    Answer

    A single partition key for unbounded time-series data concentrates writes on one key and grows its item collection forever. Bucketing by time spreads writes and keeps collections bounded, at the cost of querying several buckets for long ranges.

    Hands-on exercise

    Hands-on: store hourly temperature readings for one device for two days in monthly buckets. Query one day with BETWEEN, then fetch only the most recent reading with ScanIndexForward: false and Limit: 1.

    Summary

    Chronological sort keys power time-range queries and version history. Bucket busy series, keep a current item for fast reads, and expire raw data with TTL.

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