Prevent Double Ticket Assignment with DynamoDB Conditional Writes
Two agents can assign the same ticket at once. DynamoDB conditional writes reject stale updates server-side so only one assignment succeeds.
17 Aug 2025, 15:14 UTC

The problem: two agents, one ticket, two assignments
Imagine a support queue where two agents open the same unassigned ticket at nearly the same time. Both agents click "assign to me." The service reads the ticket, sees status = open, and writes assignee = agent-alice. A moment later, the second request reads the ticket—possibly before the first write is visible to it—and also sees status = open. It writes assignee = agent-bob. Now two agents believe they own the same ticket, and the first assignment is silently overwritten.
This is a lost update caused by a read-modify-write race. The useful takeaway is that DynamoDB can reject the second write automatically if the ticket no longer matches the state the application expected. The check happens on the server inside the write request itself.
What a conditional write actually does
A ConditionExpression is a predicate attached to PutItem, UpdateItem, or DeleteItem. DynamoDB evaluates it against the current item before applying any change. If the predicate is false, DynamoDB returns ConditionalCheckFailedException and leaves the item untouched. No partial update is applied, so there is nothing to roll back on the item itself.
For optimistic concurrency, you still read the item once to observe its current version. The difference is that the conditional write bundles that expectation and the mutation into one request. Another writer cannot change the item between your application's check and its write, because the check and the write are atomic on the server.
Use ExpressionAttributeNames for reserved words and ExpressionAttributeValues for dynamic values. DynamoDB expressions do not support raw string concatenation.
Worked example: assign a ticket with optimistic concurrency
Suppose a support_tickets table uses pk as the partition key and sk as the sort key. A ticket item looks like pk = TICKET#42, sk = META, with attributes status, assignee, and version.
To assign the ticket safely, issue an UpdateItem that succeeds only when the ticket is still open and the version matches what your application observed:
aws dynamodb update-item \
--table-name support_tickets \
--key '{"pk": {"S": "TICKET#42"}, "sk": {"S": "META"}}' \
--condition-expression "status = :open AND version = :expected" \
--update-expression "SET assignee = :agent, status = :assigned, version = version + :inc" \
--expression-attribute-values '{
":open": {"S": "open"},
":expected": {"N": "2"},
":agent": {"S": "agent-alice"},
":assigned": {"S": "assigned"},
":inc": {"N": "1"}
}' \
--return-values ALL_NEW
Run this in a terminal where the AWS CLI is configured with credentials allowing dynamodb:UpdateItem on the support_tickets table. Replace TICKET#42, 2, and agent-alice with your own key, observed version, and agent identity.
If the stored item has status = open and version = 2, DynamoDB assigns the ticket, increments version to 3, and returns the updated attributes because of ALL_NEW. If another request already assigned the ticket, the status or version no longer matches, DynamoDB returns ConditionalCheckFailedException, and no changes are applied. Your application can then reload the ticket, check whether it was assigned to someone else, and surface a conflict to the user.
Risk: do not hardcode :expected. Read the current version first, then pass it in the condition. Under heavy contention on the same partition key, repeated failures can contribute to throttling; use exponential backoff and consider spreading tickets across more partition keys.
Make ticket creation idempotent
For creating new tickets, combine PutItem with attribute_not_exists(pk). This rejects the request if the item already exists, so a duplicate creation attempt cannot overwrite an existing ticket.
aws dynamodb put-item \
--table-name support_tickets \
--item '{"pk": {"S": "TICKET#42"}, "sk": {"S": "META"}, "status": {"S": "open"}, "version": {"N": "1"}}' \
--condition-expression "attribute_not_exists(pk)"
If a duplicate request arrives, DynamoDB rejects it instead of replacing the item. The caller still receives an exception, so pair this with a client-generated token if you need retries to return success without re-reading.
Trade-offs and limits
- Conditions apply to one item only. You cannot reference attributes in another item or enforce cross-item uniqueness with a condition expression.
- Expression size limits apply. Long conditions or many attribute names can exceed the limit.
- A conditional write is not a distributed lock. If your workflow spans multiple tables or services, use DynamoDB transactions or an orchestration pattern instead.
- DynamoDB Local may differ from real DynamoDB for capacity consumption and some error details. Validate production behavior against the live service.
Verify the result without a rollback
After a successful conditional write, inspect the ReturnValues output to confirm the new assignee and incremented version. After a ConditionalCheckFailedException, issue a GetItem to read the current status and version before deciding whether to retry. Because the failed write applies no changes, the item itself needs no rollback step.
Closing
Start with attribute_not_exists(pk) for new items and add a version condition for updates. Treat ConditionalCheckFailedException as a normal control-flow signal, retry with backoff, and monitor how often it occurs. If contention stays high, rethink your partition key design before adding more retry logic.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.