Fix a Double-Spend Race in Concurrent Gift Card Redemptions

Last updated: November 29, 2026

Quick Overview

Two checkout requests redeem the same Amazon gift card at the same moment and both succeed, overdrawing it. Explain the race and write a redemption ledger that makes the balance check and debit atomic.

Amazon
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Software Engineering Fundamentals
Easy

0

0

0 solved


Two checkout requests redeem the same Amazon gift card at the same moment and both succeed, overdrawing it. Explain the race and write a redemption ledger that makes the balance check and debit atomic.

Gift card and promotional balance services see bursts of parallel checkouts, especially from retrying clients. This screens whether you can name a check-then-act race, pick the right lock granularity, and avoid a global lock that would serialize every customer.

What the Interviewer Expects
  • Identifies the read, compare, write sequence as a check-then-act race
  • Holds one lock across both the check and the debit
  • Uses per-card locks instead of one global lock
  • Protects creation of the per-card lock itself
  • Mentions that a database would use a conditional update instead
Key Topics to Cover
race conditions
check-then-act
lock granularity
atomicity
mutex
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you do this across many service hosts instead of threads in one process?
  • What changes if a redemption can be refunded later?
  • How would you write a test that reliably reproduces the original race?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • redeem(card, cents) must fail with InsufficientBalance if the balance is too low.
  • Concurrent redemptions of the same card must never drive the balance negative.
  • Redemptions of different card...
High-Level Design

The buggy code reads the balance, checks it, then writes balance - cents. Two threads can both read 5000, both pass the check and both write. The fix is to make check plus write a single critical se...


Submit Your Answer
Markdown supported