Finding a last-seat race condition with AWS Emulator
A small Python demo shows how a local DynamoDB endpoint can help reveal an application bug before deployment. It also explains how a coding agent can use a failing test to investigate and improve the code.
Imagine a concert has one seat left. Two customers try to reserve it at almost the same time. The application should confirm one reservation and tell the other customer the seat is sold out. If it confirms both, it has oversold the seat.
I built a small Python demo to explore how AWS Emulator can help test this kind of application behavior locally, before using real AWS resources.
What does each part do?
The demo has three parts:
- The application contains the reservation logic.
- The test creates a fake concert with one remaining seat, starts two reservation attempts together, and checks the outcome.
- AWS Emulator provides a local DynamoDB API for the application and test to call.
The test defines the expected business behavior. The emulator provides the local AWS service. It does not create the test or decide whether the application is correct.
The test’s key expectation is that exactly one reservation succeeds:
assert sum(result["reserved"] for result in results) == 1The test also checks that the remaining seat count is zero.
What did the test uncover?
In the intentionally buggy starting version, both requests could succeed. The test failed because the application’s update did not protect the inventory from two requests arriving together.
The fix was to make the DynamoDB update conditional: reduce the seat count only if at least one seat remains.
ConditionExpression="remaining >= :one"DynamoDB supports conditional updates: the update succeeds only when the condition is true. In this case, the first request claims the last seat. The second request’s condition fails, and the application can report “sold out.” DynamoDB conditional updates
Where does an AI coding agent fit?
The test and emulator don’t fix the code by themselves. A coding agent can help with that part:
- The agent runs the test against the local emulator.
- The test reports that two reservations succeeded instead of one.
- The agent reads the failure and investigates the application code.
- The agent changes the code and reruns the test.
For the agent to use the emulator, the emulator must be running and the application’s AWS SDK must be configured to send requests to its local endpoint. Saying “test this with the emulator” gives the agent its task; the endpoint configuration connects the test to the emulator. AWS’s Docker setup guide and SDK configuration guide explain those steps.
In my first run, I ran the test in PowerShell and examined the failure; I did not have an AI agent autonomously diagnose and fix it. The repository includes a prompt you can give a coding agent to demonstrate that next: AWS Emulator seat reservation demo on GitHub . The repository’s README explains how to start the emulator and run the test, and
AGENT_DEMO_PROMPT.md contains a ready-to-use agent prompt.What this demo does—and doesn’t—show
This is a focused test of the application’s use of DynamoDB for a concurrent reservation. It does not test VPC connectivity, cross-account access, or production traffic.
AWS Emulator’s behavior can vary by service and operation. AWS publishes information about its fidelity tests and scores , so check the operations your application depends on and validate important behavior in AWS before relying on it in production.
For me, the main value became clearer through this example: the emulator gives the app and its tests a local AWS endpoint, while a test exposes whether the application’s own rules hold. An AI coding agent can use that test feedback to help investigate and improve the code.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article