Skip to content
Quest 14Advanced18 min

Healing Party (Batch Operations)

You arrive at the Pokémon Center to load your party. Instead of making separate reads, use BatchGetItem—while handling the partial responses a production system can return.
Start the lesson

What you'll learn

  • Batch operations process multiple items in a single network request.
  • BatchGetItem accepts up to 100 keys and returns at most 16 MB per request; partial results can also occur above 1 MB per partition.
  • Responses are unordered and may contain UnprocessedKeys that must be retried.

Section 01

Healing the Party (BatchGetItem)

By passing an array of keys to BatchGetItem, we reduce network round trips. A successful HTTP response can still be partial, so merge responses by key and retry UnprocessedKeys with exponential backoff and jitter.

BatchGetItemJavaScript
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, BatchGetCommand } from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({ region: "ap-south-1" });
const docClient = DynamoDBDocumentClient.from(client);

// Re-send any UnprocessedKeys with backoff (see the lab below).
const { Responses, UnprocessedKeys } = await docClient.send(new BatchGetCommand({
  RequestItems: {
    "JohtoPokemon": {
      Keys: [
        { Name: "Chikorita" },
        { Name: "Cyndaquil" },
        { Name: "Totodile" }
      ]
    }
  }
}));
Your appAWS SDK v3DynamoDBtableP0P1P2P3CONSUMED0

Ready · 7 steps · Sprig

This animation plays when you scroll to it. Press Play to start it now.

All steps as text
  1. Your app sends BatchGetItem to the JohtoPokemon table with Name = "Chikorita, Cyndaquil, Totodile".
  2. DynamoDB hashes "Chikorita" to df9e8fa1, which routes the request to partition P1.
  3. DynamoDB hashes "Cyndaquil" to 6203cc87, which routes the request to partition P3.
  4. DynamoDB hashes "Totodile" to e49e153a, which routes the request to partition P2.
  5. P1 returns Chikorita.
  6. P3 returns Cyndaquil.
  7. DynamoDB responds with success: 2 of 3 returned. Totodile sat in UnprocessedKeys and was never retried.

Practice

Your turn

Load the party efficiently. Finish the Keys array and keep retrying UnprocessedKeys, because batch responses can be partial and their item order is not guaranteed.
Add Cyndaquil and Totodile. The starter loop already re-sends response.UnprocessedKeys with capped exponential backoff and jitter, so keep it.
Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Simulated — no AWS calls

Check

Check your understanding

Quick Quiz · one question, as many tries as you need.

What is the maximum number of items you can retrieve in a single BatchGetItem request?

Choose one answer.