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

    Batch Operations, Retries, and Bulk Loads

    BatchWriteItem and BatchGetItem group many independent requests into one network call.

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

    Introduction

    BatchWriteItem and BatchGetItem group many independent requests into one network call. They are not transactions: each item succeeds or fails on its own, and DynamoDB may return some items as unprocessed when the table is throttled. Correct retry handling is the whole skill.

    Purpose of this lesson

    You will write a batch writer that chunks input, retries unprocessed items with exponential backoff, and know when to use managed import instead.

    Understanding the topic

    Limits and behaviour:

    • BatchWriteItem: up to 25 put or delete requests and 16 MB per call; no updates and no condition expressions.
    • BatchGetItem: up to 100 keys and 16 MB per call; eventually consistent by default (ConsistentRead per table).
    • Throttled items come back in UnprocessedItems / UnprocessedKeys — the call itself still succeeds.
    • Batch calls consume the same capacity as the individual operations; they save round trips, not capacity.
    • For loading large datasets into a new table, Import from S3 does not consume write capacity.

    Informative example

    Chunked writer with exponential backoff and jitter:

    ts
    import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
    export async function batchPut(items: Record<string, unknown>[]) {
    for (let i = 0; i < items.length; i += 25) {
    let requests = items.slice(i, i + 25).map((Item) => ({ PutRequest: { Item } }));
    for (let attempt = 0; requests.length > 0; attempt++) {
    if (attempt > 8) throw new Error(`${requests.length} items still unprocessed`);
    if (attempt > 0) await sleep(Math.random() * Math.min(5000, 50 * 2 ** attempt));
    const res = await ddb.send(new BatchWriteCommand({ RequestItems: { [TABLE]: requests } }));
    requests = (res.UnprocessedItems?.[TABLE] ?? []) as typeof requests;
    }
    }
    }

    The SDK already retries throttling errors on the whole call; this loop handles the partial-success case, which the SDK does not retry for you.

    Decision framework

    • Need all-or-nothing across items → TransactWriteItems.
    • Many independent writes from an application → BatchWriteItem with retry of unprocessed items.
    • Millions of items into a new table → Import from S3.
    • Backfilling a new attribute on existing items → parallel Scan + UpdateItem with rate limiting.

    Best practices

    • Always loop on UnprocessedItems with backoff and jitter.
    • De-duplicate keys within one batch (a batch cannot contain two requests for the same item).
    • Rate-limit backfills so they do not starve production traffic.

    Common mistakes

    • Ignoring UnprocessedItems and silently losing writes.
    • Expecting conditions or updates in BatchWriteItem.
    • Using batch writes where atomicity is required.

    Advanced interview questions

    Interview Prep

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

    2 questions
    1BeginnerQuestionIs BatchWriteItem atomic?+

    Answer

    No. Each put or delete is independent; some may succeed while others are returned in UnprocessedItems. Use TransactWriteItems for atomicity.
    2AdvancedQuestionHow do you load 50 million items into DynamoDB?+

    Answer

    For a new table, use Import from S3 (CSV, DynamoDB JSON, or Ion), which does not consume write capacity. For an existing table, run parallel batch writers with rate limiting, backoff on unprocessed items, and on-demand or temporarily raised capacity.

    Hands-on exercise

    Hands-on: load 1,000 product items with batchPut. Then implement batchGet(ids) that requests 100 keys at a time and retries UnprocessedKeys. Verify all 1,000 items are returned.

    Summary

    Batch operations save round trips but are not atomic. Chunk to the limits, retry unprocessed items with backoff, and use Import from S3 for large initial loads.

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