Prevent Lost Updates with DynamoDB Conditional Writes
Intermediate · 30 minutes · 5 tasks, checked automatically · 100 points
In high-concurrency systems, two processes can read the same item, make independent changes, and then both write back — causing one update to silently overwrite the other. This is the classic 'lost update' problem. In this lab you will reproduce the problem with plain PutItem calls, then fix it using DynamoDB conditional expressions and optimistic locking with a version attribute. You will create a DynamoDB table, seed it with inventory data, simulate a lost update, and finally protect writes with `ConditionExpression` so that stale writers are rejected instead of silently clobbering fresh data.
What you’ll learn
- Create a DynamoDB table suitable for an inventory tracking use-case
- Reproduce the lost-update race condition using unconditional PutItem writes
- Implement optimistic locking with a version counter and ConditionExpression to prevent lost updates
- Verify that a stale writer is rejected with a ConditionalCheckFailedException
- Use UpdateItem with atomic counters to safely increment numeric attributes without optimistic locking
How this lab works
- Start the lab: One click starts an emulated cloud on your computer, with the cloud’s own CLI ready: no account and no bill.
- Work through 5 tasks: Each task says what to build or fix. You do it yourself, in a real terminal, with hints when you get stuck.
- Check your work: Check my work reads the emulated cloud’s resources and says, task by task, what passed and what to fix.
- Finish and keep going: 100 points, your progress saved, and the next lab to try.
What it runs on
- Runs: An emulated cloud on your computer: no cloud account, no bill
- Cloud: AWS
- Services: dynamodb
What you need
- A free LearnXops account
- The LearnXops desktop app for macOS, Windows or Linux
- Docker Desktop (or Podman / Rancher Desktop) installed and running
- A AWS sandbox created in the Local Cloud Playground
Topics: dynamodb, conditional-writes, optimistic-locking, concurrency, databases, lost-update, put-item, update-item, condition-expression
Create a free account to start this lab.
Keep going
- Add a Global Secondary Index to a DynamoDB Table and Query It
- Datastore Ancestor Keys and Composite Index Queries
- Provision Azure Database for PostgreSQL, Create a Schema and Run Parameterised Queries
- Cache Query Results in Azure Cache for Redis and Measure the Hit Rate
- Paginate Large DynamoDB Scans and Handle Throttling with boto3
- RDS Snapshot and Restore: Backup and Recovery Fundamentals