KKeySync
← Back to quests
Quest · quest 12Johto · Advanced

Trading with Rival (Transactions)

You want to trade your Scyther for your rival's Onix. This involves two database updates: 1) Set Scyther's owner to your rival, 2) Set Onix's owner to you. If step 1 succeeds but step 2 fails, you've lost your Pokémon! DynamoDB Transactions prevent this.

TransactWriteItemsACID

Key takeaways

  • Transactions (TransactWriteItems) guarantee ACID properties across multiple items.
  • Either all operations succeed, or they all fail together.
  • Transactions are more expensive than standard writes, so use them only when necessary.
  • Condition each write on the expected owner to prevent a stale trade from overwriting newer state.

Trading Pokémon Safely (ACID)

By wrapping our updates in a TransactWriteItems call, we ensure that the trade is atomic. If the Onix update fails for any reason (maybe it was already traded!), the Scyther update is automatically rolled back.

TransactWriteItemsjavascript

Interactive Challenge

Trading Scyther for Onix is two writes that must both succeed or both fail. Complete the TransactWriteCommand by adding the second Update that hands Onix to you.

Goal: Add the second Update, and give both updates a condition that checks the expected current owner before swapping them.

Quick Quiz

What DynamoDB operation guarantees that multiple item updates either all succeed or all fail?

Make your choice to test your understanding. Submit to check, and if it's not quite right you can adjust and try again.