DynamoDB Batch Operations

A batch operation groups many reads or writes into one request. Your application sends a single call, and DynamoDB processes every item inside it. Fewer network round trips mean less waiting and simpler code.

The Grocery Trip Idea

Imagine you need 25 items from a market. You can walk to the market 25 times and buy one item per trip. A smarter shopper writes a list and buys everything in one trip. Batch operations bring the same shopping list approach to your database calls.

Two Batch APIs

APIPurposeLimit per request
BatchGetItemReads many items by primary key100 items, 16 MB of data
BatchWriteItemPuts or deletes many items25 requests, 16 MB of data

Each individual item still obeys the 400 KB item size limit.

BatchGetItem

BatchGetItem fetches items from one or more tables in a single call. You list the primary keys, and DynamoDB returns the matching items in no guaranteed order.

BatchGetItem
  Table: Students
    Keys: { "StudentID": "S001" }
          { "StudentID": "S002" }
          { "StudentID": "S003" }
  Table: Courses
    Keys: { "CourseID": "C10" }

Reads are eventually consistent by default. You can request strongly consistent reads for each table in the request. A missing key does not cause an error. DynamoDB simply leaves that item out of the response.

BatchWriteItem

BatchWriteItem accepts two kinds of actions: PutRequest to create or replace an item, and DeleteRequest to remove an item.

BatchWriteItem
  Table: Students
    PutRequest:    { "StudentID": "S010", "Name": "Meera" }
    PutRequest:    { "StudentID": "S011", "Name": "Arjun" }
    DeleteRequest: { "StudentID": "S003" }

Several rules apply to BatchWriteItem:

  • The operation cannot update part of an item. Use UpdateItem for that.
  • The operation cannot use condition expressions.
  • One request cannot contain two actions that target the same primary key.

Handling Unprocessed Items

DynamoDB sometimes processes only part of a batch. Throttling or size limits cause this behavior. The response lists the leftovers in a field named UnprocessedKeys for reads and UnprocessedItems for writes. Your code must send those leftovers again.

items_left = full_batch
attempt = 0
WHILE items_left is not empty AND attempt < 5:
    response = BatchWriteItem(items_left)
    items_left = response.UnprocessedItems
    IF items_left is not empty:
        wait random time up to (2 ^ attempt) * 50 ms
        attempt = attempt + 1

The waiting pattern is called exponential backoff with jitter. The delay grows after each failure, and the random part keeps many clients from retrying at the same instant. Most AWS SDKs retry throttled calls automatically. The application stays responsible for unprocessed items in the response.

Diagram: Partial Success in a Batch

Batch of 25 writes
   │
   ▼
DynamoDB processes 20 ✓
DynamoDB returns 5 as UnprocessedItems
   │
   ▼
Application waits briefly and resends the 5
   │
   ▼
All 25 complete ✓

Batch Is Not a Transaction

A batch does not guarantee all-or-nothing behavior. Each action succeeds or fails on its own. A batch of 25 writes can finish with 22 saved items and 3 failures. Use DynamoDB Transactions when your data needs all-or-nothing safety.

FeatureBatchTransaction
All-or-nothingNoYes
Condition checksNoYes
Capacity costStandardDouble
Best forBulk loading, bulk readingLinked changes that must agree

Capacity Cost

A batch consumes the same capacity as the equivalent single calls. Reading 100 items with BatchGetItem costs the same read units as 100 GetItem calls. The saving comes from reduced network time and fewer requests to manage, not from lower capacity charges.

When to Use Batches

  • Load a starting dataset into a new table.
  • Fetch the profiles of 50 users shown on one screen.
  • Delete many expired records in one sweep.
  • Copy items between tables.

Summary

  • BatchGetItem reads up to 100 items, and BatchWriteItem writes up to 25 items per call.
  • Batches support puts and deletes only. Updates and conditions are unavailable.
  • Applications must retry unprocessed items with exponential backoff.
  • Batches give no all-or-nothing guarantee.
  • Capacity cost matches single operations, while network time drops.