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

    Event-Driven Patterns: Outbox, Projections, and Fan-Out

    Streams turn DynamoDB into the source of truth for an event-driven system.

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

    Introduction

    Streams turn DynamoDB into the source of truth for an event-driven system. Three patterns cover most needs: the transactional outbox (publish events reliably with the write that caused them), read-model projections (search indexes, aggregates), and fan-out to many consumers through EventBridge.

    Purpose of this lesson

    You will design an outbox item written in the same transaction as an order, publish it from the stream, and choose a fan-out mechanism for multiple consumers.

    Understanding the topic

    Patterns:

    • Outbox: write the business item and an OUTBOX#… event item in one TransactWriteItems. A stream consumer publishes outbox items, so an event is never lost or sent for a write that failed.
    • Change events from the item itself: often simpler — publish from the item's MODIFY record when the event can be derived from old and new images.
    • Projections: stream consumers maintain read models (OpenSearch documents, counters, denormalised copies).
    • Fan-out: EventBridge Pipes (stream → filter → enrich → target) or one Lambda publishing to an EventBridge bus lets many consumers subscribe without reading the stream directly.
    • Search: DynamoDB also offers a managed zero-ETL integration with Amazon OpenSearch Service that replaces hand-written sync code.

    Informative example

    Order + outbox event in one transaction:

    ts
    import { TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
    import { randomUUID } from "node:crypto";
    import { ddb, TABLE } from "./ddb";
    export async function placeOrder(orderId: string, customerId: string, total: number) {
    const now = new Date().toISOString();
    await ddb.send(new TransactWriteCommand({
    TransactItems: [
    { Put: {
    TableName: TABLE,
    Item: { PK: `ORDER#${orderId}`, SK: "HEADER", entityType: "Order", customerId, total, status: "PLACED" },
    ConditionExpression: "attribute_not_exists(PK)",
    } },
    { Put: {
    TableName: TABLE,
    Item: {
    PK: `OUTBOX#${orderId}`, SK: `EVENT#${now}#${randomUUID()}`,
    entityType: "OutboxEvent", type: "OrderPlaced",
    payload: { orderId, customerId, total },
    expiresAt: Math.floor(Date.now() / 1000) + 3 * 24 * 3600,
    },
    } },
    ],
    }));
    }

    A stream consumer filters INSERT records whose entityType is OutboxEvent, publishes them, and lets TTL clean the outbox up.

    Trade-offs

    • Outbox items give explicit, versioned event contracts at the cost of extra writes.
    • Deriving events from item diffs is cheaper but couples consumers to the table schema.
    • EventBridge fan-out decouples consumers but adds latency and per-event cost.

    Best practices

    • Version event payloads and keep them independent of table layout.
    • Expire outbox items with TTL once published.
    • Use EventBridge or Pipes for more than two consumers instead of attaching more readers to the stream.

    Common mistakes

    • Publishing events from the API handler after the write — a crash between the two loses or duplicates events.
    • Assuming downstream consumers see events in a global order.
    • Projecting into a search index without handling deletes (REMOVE events).

    Advanced interview questions

    Interview Prep

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

    2 questions
    1AdvancedQuestionWhat problem does the transactional outbox solve?+

    Answer

    The dual-write problem: updating the database and publishing a message are two separate operations, so a crash between them causes lost or phantom events. Writing the event into the same database transaction and publishing it from the change stream makes the two consistent.
    2IntermediateQuestionHow would you keep an OpenSearch index in sync with DynamoDB?+

    Answer

    Use the managed zero-ETL integration, or a stream consumer that upserts documents on INSERT/MODIFY and deletes them on REMOVE, with idempotent writes keyed by item id and version.

    Hands-on exercise

    Hands-on: implement placeOrder with an outbox item and a stream handler that publishes only outbox inserts to EventBridge. Simulate a publish failure and confirm the record is retried and reported through batchItemFailures.

    Summary

    Streams enable the outbox pattern, read-model projections, and fan-out. Write events transactionally, publish from the stream, and fan out through EventBridge.

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