Bird
Raised Fist0
DynamoDBquery~15 mins

Return values on write in DynamoDB - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Return values on write
What is it?
In DynamoDB, 'Return values on write' controls what data the database sends back after you add, update, or delete an item. Instead of just confirming the action, DynamoDB can return the old item, the new item, or nothing at all. This helps you see what changed without making a separate read request.
Why it matters
Without return values, you would need extra steps to check what data was changed, which slows down your app and uses more resources. Return values make your app faster and simpler by giving immediate feedback on your write actions. This is especially useful when you want to confirm updates or keep track of changes.
Where it fits
Before learning this, you should understand basic DynamoDB operations like PutItem, UpdateItem, and DeleteItem. After this, you can explore advanced features like conditional writes and transactions that also use return values to manage data safely.
Mental Model
Core Idea
Return values on write let you see what changed in your database immediately after a write operation, saving extra steps.
Think of it like...
It's like when you send a letter and get a receipt that shows what you sent or what was there before, so you know exactly what happened without guessing.
┌───────────────┐
│ Write Action  │
└──────┬────────┘
       │
       ▼
┌─────────────────────────────┐
│ DynamoDB processes the write │
└──────┬───────────────┬──────┘
       │               │
       ▼               ▼
┌───────────────┐ ┌───────────────┐
│ Return Values │ │ No Return     │
│ (Old/New Item)│ │ (Just Success)│
└───────────────┘ └───────────────┘
Build-Up - 7 Steps
1
FoundationBasic write operations in DynamoDB
🤔
Concept: Learn what happens when you add, update, or delete items in DynamoDB.
DynamoDB lets you add new items with PutItem, change existing items with UpdateItem, and remove items with DeleteItem. By default, these operations just confirm success without showing what changed.
Result
You can store and modify data, but you don't see the before or after state of the item automatically.
Understanding the default behavior helps you see why return values are useful—they add visibility to your data changes.
2
FoundationWhat are return values on write?
🤔
Concept: Return values specify what data DynamoDB sends back after a write operation.
When you perform a write, you can ask DynamoDB to return the item's old state, new state, both, or nothing. This is controlled by the ReturnValues parameter in your request.
Result
You get immediate feedback about the item you changed, which can be the previous data, the updated data, or no data.
Knowing this parameter exists lets you control how much information you get back, making your app more efficient.
3
IntermediateReturnValues options explained
🤔Before reading on: do you think ReturnValues can return both old and new item states at the same time? Commit to yes or no.
Concept: Learn the different ReturnValues settings and what each returns.
ReturnValues can be: - NONE: returns nothing (default) - ALL_OLD: returns the item as it was before the write - UPDATED_OLD: returns only the updated attributes before the write - ALL_NEW: returns the item as it is after the write - UPDATED_NEW: returns only the updated attributes after the write Note: Not all options are supported by every write operation.
Result
You can choose exactly what data you want back to suit your needs.
Understanding these options helps you avoid unnecessary data transfer and tailor responses to your app's logic.
4
IntermediateUsing ReturnValues with UpdateItem
🤔Before reading on: do you think UpdateItem returns the entire item by default or only the changed parts? Commit to your answer.
Concept: UpdateItem supports ReturnValues to show changes made during the update.
When you update an item, you can set ReturnValues to ALL_NEW to get the full updated item back, or UPDATED_OLD to see what changed before the update. This helps confirm your update worked as expected.
Result
You receive the updated data immediately, avoiding extra reads.
Knowing how to use ReturnValues with UpdateItem improves efficiency and reduces round trips to the database.
5
IntermediateReturnValues with PutItem and DeleteItem
🤔
Concept: PutItem and DeleteItem also support ReturnValues but with some differences.
PutItem can return ALL_OLD to show the item replaced by the new one, if any. DeleteItem can return ALL_OLD to show the deleted item. This helps you track what was overwritten or removed.
Result
You get confirmation of what data was replaced or deleted.
Using ReturnValues here helps maintain data integrity and audit trails without extra queries.
6
AdvancedReturnValues in conditional writes and transactions
🤔Before reading on: do you think ReturnValues behave the same in transactions as in single writes? Commit to yes or no.
Concept: ReturnValues can be used in conditional writes and transactions to get data only if conditions pass.
In conditional writes, ReturnValues returns data only if the condition is met and the write happens. In transactions, you can get return values for each operation, helping you see all changes atomically.
Result
You get precise feedback on complex operations, improving reliability.
Understanding this helps you build safe, consistent applications that react to data changes immediately.
7
ExpertPerformance and cost implications of ReturnValues
🤔Before reading on: do you think using ReturnValues always improves performance? Commit to yes or no.
Concept: ReturnValues affect network data size and processing time, impacting cost and speed.
Returning large items or many attributes increases response size and latency. This can raise costs if your app transfers lots of data frequently. Choosing minimal return values balances visibility and efficiency.
Result
You optimize your app's performance and cost by selecting appropriate ReturnValues.
Knowing the tradeoffs prevents overuse of return data, keeping your app fast and affordable.
Under the Hood
When a write request with ReturnValues is received, DynamoDB performs the write operation internally. It then retrieves the requested item state (old, new, or updated attributes) from its storage engine before sending the response. This retrieval is done atomically with the write to ensure consistency.
Why designed this way?
ReturnValues were designed to reduce the need for extra read requests after writes, saving network calls and improving app responsiveness. The options reflect common use cases like auditing changes or confirming updates without extra complexity.
┌───────────────┐
│ Client sends  │
│ write request │
│ with ReturnValues │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ DynamoDB      │
│ performs write│
└──────┬────────┘
       │
       ▼
┌─────────────────────────────┐
│ Retrieve requested item data │
│ (old/new/updated attributes) │
└──────┬───────────────┬──────┘
       │               │
       ▼               ▼
┌───────────────┐ ┌───────────────┐
│ Send response │ │ Send response │
│ with data     │ │ without data  │
└───────────────┘ └───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does ReturnValues return both old and new item states together? Commit to yes or no.
Common Belief:ReturnValues can return both the old and new item states at the same time.
Tap to reveal reality
Reality:ReturnValues options return either old or new states, or updated attributes, but never both full old and new items together.
Why it matters:Expecting both can lead to confusion and incorrect code that tries to handle impossible responses.
Quick: Does using ReturnValues always improve performance? Commit to yes or no.
Common Belief:Using ReturnValues always makes your application faster because you get immediate feedback.
Tap to reveal reality
Reality:ReturnValues can increase response size and latency, potentially slowing down your app and increasing costs if overused.
Why it matters:Misusing ReturnValues can degrade performance and raise bills, especially with large items.
Quick: Does PutItem with ReturnValues always return the old item? Commit to yes or no.
Common Belief:PutItem always returns the old item when ReturnValues is set.
Tap to reveal reality
Reality:PutItem returns the old item only if it overwrites an existing item; if the item is new, nothing is returned.
Why it matters:Assuming an old item is always returned can cause errors in logic that expects data that isn't there.
Quick: Can ReturnValues be used with all DynamoDB operations? Commit to yes or no.
Common Belief:ReturnValues can be used with any DynamoDB operation to get data back.
Tap to reveal reality
Reality:ReturnValues is only supported on write operations like PutItem, UpdateItem, and DeleteItem, not on reads or queries.
Why it matters:Trying to use ReturnValues on unsupported operations leads to errors and wasted development time.
Expert Zone
1
ReturnValues with UPDATED_OLD and UPDATED_NEW return only the attributes that changed, which can be much smaller than the full item, saving bandwidth.
2
In transactional writes, ReturnValues are returned per operation, allowing fine-grained tracking of changes within a single atomic transaction.
3
Using ReturnValues with conditional writes can help detect why a write failed by comparing expected and actual item states.
When NOT to use
Avoid using ReturnValues when your application does not need immediate feedback on the item state after writes, especially for large items or high-throughput workloads. Instead, use separate read operations selectively or design your app logic to minimize data transfer.
Production Patterns
In production, ReturnValues is often used to implement optimistic concurrency control by returning old item versions before updates. It's also used in audit logging to capture changes without extra reads, and in event-driven architectures to trigger actions based on updated data.
Connections
Optimistic Concurrency Control
ReturnValues provides the old item state needed to check for conflicts before applying updates.
Knowing how ReturnValues works helps implement safe concurrent updates by comparing old and new data.
Event Sourcing
ReturnValues can supply the changed data needed to create events representing state changes.
Understanding ReturnValues enables building event-driven systems that react immediately to data mutations.
HTTP Status Codes
ReturnValues is like HTTP responses that include body content to confirm what happened, not just status codes.
Seeing this connection helps appreciate why returning data after writes improves client-server communication clarity.
Common Pitfalls
#1Expecting ReturnValues to return both old and new item states simultaneously.
Wrong approach:UpdateItem with ReturnValues set to 'ALL_OLD_AND_NEW' (invalid value).
Correct approach:UpdateItem with ReturnValues set to 'ALL_OLD' or 'ALL_NEW' separately.
Root cause:Misunderstanding the allowed ReturnValues options and expecting combined old and new data in one response.
#2Using ReturnValues unnecessarily on large items causing slow responses.
Wrong approach:UpdateItem with ReturnValues='ALL_NEW' on very large items when the app does not need the updated data.
Correct approach:UpdateItem with ReturnValues='NONE' to avoid extra data transfer when feedback is not needed.
Root cause:Not considering the performance impact of returning large amounts of data after writes.
#3Trying to use ReturnValues with Query or GetItem operations.
Wrong approach:Query with ReturnValues='ALL_NEW'.
Correct approach:Query without ReturnValues parameter, as it is unsupported.
Root cause:Confusing write operation parameters with read operation parameters.
Key Takeaways
ReturnValues on write operations let you get immediate feedback about data changes without extra reads.
Choosing the right ReturnValues option balances visibility and performance, avoiding unnecessary data transfer.
ReturnValues supports different write operations with slightly different behaviors, so know which options apply where.
Using ReturnValues effectively helps build faster, safer, and more responsive applications with DynamoDB.
Misunderstanding ReturnValues options or overusing them can cause bugs, slowdowns, and higher costs.

Practice

(1/5)
1. What does the ReturnValues parameter do in a DynamoDB write operation?
easy
A. It specifies what data should be returned after the write operation.
B. It controls the write capacity units consumed by the operation.
C. It sets the timeout duration for the write request.
D. It defines the table where data will be written.

Solution

  1. Step 1: Understand the role of ReturnValues

    The ReturnValues parameter tells DynamoDB what data to send back after a write, such as old or new item attributes.
  2. Step 2: Differentiate from other parameters

    ReturnValues does not affect capacity, timeout, or table selection; it only controls returned data.
  3. Final Answer:

    It specifies what data should be returned after the write operation. -> Option A
  4. Quick Check:

    ReturnValues controls returned data [OK]
Hint: ReturnValues returns data after write, not capacity or timeout [OK]
Common Mistakes:
  • Confusing ReturnValues with capacity settings
  • Thinking it sets request timeout
  • Assuming it selects the table
2. Which of the following is the correct syntax to request the old item attributes after an update in DynamoDB using ReturnValues?
easy
A. "UPDATED_NEW"
B. "NONE"
C. "ALL_NEW"
D. "ALL_OLD"

Solution

  1. Step 1: Recall ReturnValues options

    ReturnValues can be NONE, ALL_OLD, UPDATED_OLD, ALL_NEW, UPDATED_NEW.
  2. Step 2: Identify option for old attributes after update

    "ALL_OLD" returns the entire item as it was before the update.
  3. Final Answer:

    "ALL_OLD" -> Option D
  4. Quick Check:

    Old attributes = ALL_OLD [OK]
Hint: Old data after update is ALL_OLD [OK]
Common Mistakes:
  • Using ALL_NEW to get old data
  • Confusing UPDATED_NEW with old data
  • Setting NONE expecting data back
3. Given this DynamoDB update command snippet:
{
  TableName: "Users",
  Key: { "UserId": "123" },
  UpdateExpression: "SET Age = :newAge",
  ExpressionAttributeValues: { ":newAge": 30 },
  ReturnValues: "UPDATED_NEW"
}

What will the response contain?
medium
A. The entire item before the update.
B. Only the new Age attribute after the update.
C. No attributes, just success confirmation.
D. The entire item after the update.

Solution

  1. Step 1: Understand ReturnValues "UPDATED_NEW" meaning

    This option returns only the attributes that were updated with their new values.
  2. Step 2: Apply to given update

    The update changes Age to 30, so response includes only Age with value 30.
  3. Final Answer:

    Only the new Age attribute after the update. -> Option B
  4. Quick Check:

    UPDATED_NEW returns updated attributes [OK]
Hint: UPDATED_NEW returns only changed attributes with new values [OK]
Common Mistakes:
  • Expecting full item back with UPDATED_NEW
  • Confusing UPDATED_NEW with ALL_NEW
  • Assuming no data returned with UPDATED_NEW
4. You wrote this DynamoDB PutItem request:
{
  TableName: "Products",
  Item: { "Id": "001", "Name": "Pen" },
  ReturnValues: "UPDATED_OLD"
}

But you get an error. What is wrong?
medium
A. ReturnValues must be set to "ALL_NEW" for PutItem.
B. The Item attribute is missing required keys.
C. "UPDATED_OLD" is invalid for PutItem operations.
D. TableName is incorrect.

Solution

  1. Step 1: Check ReturnValues compatibility with PutItem

    PutItem supports only NONE or ALL_OLD for ReturnValues; UPDATED_OLD is invalid.
  2. Step 2: Confirm other parameters

    Item and TableName are correct; error is due to invalid ReturnValues.
  3. Final Answer:

    "UPDATED_OLD" is invalid for PutItem operations. -> Option C
  4. Quick Check:

    PutItem supports NONE or ALL_OLD only [OK]
Hint: PutItem allows NONE or ALL_OLD only for ReturnValues [OK]
Common Mistakes:
  • Using UPDATED_OLD with PutItem
  • Assuming ALL_NEW required for PutItem
  • Mistaking missing keys as cause
5. You want to update a user's email in DynamoDB and get back the entire updated item in one request. Which ReturnValues option should you use in your UpdateItem call?
hard
A. "ALL_NEW"
B. "UPDATED_OLD"
C. "NONE"
D. "ALL_OLD"

Solution

  1. Step 1: Understand the goal

    You want the full updated item returned after the update.
  2. Step 2: Match ReturnValues option

    "ALL_NEW" returns the entire item as it looks after the update.
  3. Final Answer:

    "ALL_NEW" -> Option A
  4. Quick Check:

    Full updated item = ALL_NEW [OK]
Hint: Use ALL_NEW to get full updated item after update [OK]
Common Mistakes:
  • Choosing UPDATED_OLD which returns old updated attributes
  • Using NONE expecting data back
  • Selecting ALL_OLD which returns old full item