DynamoDB Transactions: Why You Can't Touch the Same Item Twice
A ValidationException that only showed up on one code path, the rule behind it, and the two habits that keep a transaction's items from colliding again.
We mentioned this one in passing in our first post, and it turned out to deserve a post of its own: a feature that looked completely correct in code review, passed a quick manual test, and then failed outright the first time someone exercised one specific path through it — not with a wrong answer, but with DynamoDB refusing to run the write at all.
What actually went wrong
Our auction room lets an owner pause a lot mid-bid — the clock freezes, the standing bid stays put. From a paused lot, we added a second action: sell the lot right now, at whatever the standing bid is, instead of resuming it. That write already existed as a transaction, built by the same code that closes an ordinary lot — it updates the winning team's remaining points, marks the item sold, and appends to the activity log, all in one TransactWriteItems call.
The new "sell while paused" code added one more safety check to that same call: a condition confirming the auction was still actually paused before letting the sale go through. Reasonable, cheap, and — on its own — correct. It just happened to check a condition against the same row the existing closure code was already writing to.
// The existing lot-closure builder already writes this item:
transactionItems.push({
Update: {
Key: lotKey,
UpdateExpression: "SET #status = :sold, sold_price = :price",
},
});
// A later feature adds a guard on the SAME item, thinking it's independent:
transactionItems.push({
ConditionCheck: {
Key: lotKey,
ConditionExpression: "auction_state = :stopped",
},
});
await dynamoDb.transactWrite({ TransactItems: transactionItems });
// ValidationException: Transaction request cannot include multiple
// operations on one itemDynamoDB's rule is absolute and has nothing to do with what the two operations actually say: a single TransactWriteItems call may reference each (table, primary key) pair once. A Put, an Update, a Delete and a bare ConditionCheck all count as touching the item — there is no "read-only" exception. Two harmless-looking operations on the same row fail the whole transaction, every time, with the same generic ValidationException.
That's what made it slip past review: nothing about either diff looked wrong. The bug was only visible at the point where two pieces of code, written weeks apart, happened to reach for the same row in the same call.
The fix: fold the guard in, don't bolt it on
The honest fix wasn't a clever workaround — it was admitting that the new rule belonged on the existing write, not beside it. Instead of appending a second action for the same key, its condition became part of the ConditionExpression the closure builder was already sending:
transactionItems.push({
Update: {
Key: lotKey,
UpdateExpression: "SET #status = :sold, sold_price = :price",
ConditionExpression:
"attribute_exists(pk) AND auction_state = :stopped",
ExpressionAttributeValues: { ":stopped": "STOPPED", ... },
},
});
await dynamoDb.transactWrite({ TransactItems: transactionItems });
// Succeeds — and still refuses to sell a lot that isn't actually paused.Nothing about the guarantee got weaker. The condition still has to hold or the whole transaction rolls back — it just has to ride on an action that was already there, rather than arrive as a second one. The rule we took away: whoever builds a TransactWriteItems call for a given item should be the only place a new rule about that item is allowed to live.
Two more from the same family
You can't ADD and DELETE the same attribute either
A related DynamoDB limit bit a different feature: an UpdateExpression that tried to both ADD an id to one string set and DELETE an id from the same set, in a single update, to move a player from one group's pool to another's in one call. DynamoDB refuses that combination outright, independent of transactions entirely — it's a rule about a single UpdateItem call. The fix was the same shape as above: stop trying to do two logical things to one attribute in one expression, and either split it into two update calls or restructure the data so "remove from A" and "add to B" aren't the same attribute at all.
Sometimes the fix is fewer items per transaction, not more
Not every collision should be solved by merging harder. When our fill-remaining-players feature needed to withdraw a batch of unsold players from the pool as a side effect of closing out an auction, we deliberately kept that as its own transaction rather than folding it into the write that marks the auction finished. Cramming every related change into one call is exactly how you end up with two unrelated features reaching for the same row. A second, smaller transaction — each idempotent on its own, typically with an attribute_not_exists(...) condition so a retried call is a safe no-op instead of a double-write — is often the simpler design, not the compromise.
What we changed about how we build transactions
Once we understood the rule, we stopped relying on remembering it. The function that builds a lot's closure transaction is the only place allowed to decide what happens to that lot's row — a new rule is a new clause on an existing action, never a new action. And because we test against a local DynamoDB fake rather than live tables, we taught that fake the same constraint: it now rejects a TransactWriteItems call with a duplicate (table, key) pair exactly as the real service does. The mistake that used to surface as a ValidationException in front of a room full of bidders now fails a unit test in under a second, the next time someone writes it.
DynamoDB documents the transaction limits plainly — up to 100 action requests and 4 MB total per call — but "don't touch the same item twice" is easy to read once and forget, because most of the time you're not touching the same item twice on purpose. The useful habit isn't memorising the rule; it's treating "who is allowed to write this row" as a question with exactly one answer, and keeping that answer true in code, not just in your head.
Watch the lot clock in a real auction room
Run a free practice auction in your browser — no sign-up required to watch, no real money anywhere in the bidding.
🎯 No real-money auctions · Pure strategy · Free to download