Build an In-Memory Fake for Conditional Writes in Unit Tests
Last updated: November 29, 2026
Quick Overview
Your service claims Lightning Deal slots using version-checked conditional writes to a key-value table. Build an in-memory fake of the table that supports conditional puts and fault injection, and use it to test the retry path when a concurrent writer wins.
Amazon
November 29, 20260
0
0 solved
Your service claims Lightning Deal slots using version-checked conditional writes to a key-value table. Build an in-memory fake of the table that supports conditional puts and fault injection, and use it to test the retry path when a concurrent writer wins.
Amazon teams lean on DynamoDB conditional expressions for optimistic concurrency, and the hardest code to test is the conflict and retry path. The interviewer checks that you know the difference between fakes and mocks, keep the fake faithful to the real semantics, and can force race conditions deterministically.
What the Interviewer Expects
- Builds a fake with real behavior rather than a mock that only records calls
- Implements version checks and must-not-exist conditions faithfully
- Deep-copies on read and write so tests cannot share mutable state with storage
- Injects faults such as throttling deterministically
- Writes a test that forces a lost race and asserts the retry outcome
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How do you keep the fake from drifting from the real service's behavior?
- When would you rather use DynamoDB Local or a test account?
- How would you test that the retry loop gives up under heavy contention?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Fake supports
getandputwithexpected_versionandmust_not_exist. - Mismatched conditions raise
ConditionalCheckFailed, like the real client. - Test must deterministically simulate anoth...
High-Level Design
The fake stores items in a dict, bumps version on every successful put, and checks conditions under a lock. A fail_next hook injects one exception, for throttling or timeouts. To force the race, t...