The diff only improves the error message, and now a coupon works twice

The pull request is titled "Clearer errors when redeeming a coupon". Support has complained that people only ever see "could not redeem", and the change splits that into "no such code" and "already used". The tests are green and the new messages are better. The interviewer asks whether you would approve it. The goal is fine. What the diff moved to reach it is the problem.

The diff

Here is an original example. A single-use code gives ten pounds of credit. The handler adds the credit when redeem succeeds. Before:

def redeem(db, code, user_id):
    cur = db.execute(
        "UPDATE coupons SET redeemed_by = ? WHERE code = ? AND redeemed_by IS NULL",
        (user_id, code))
    return cur.rowcount == 1

After:

def redeem(db, code, user_id):
    row = db.execute(
        "SELECT redeemed_by FROM coupons WHERE code = ?", (code,)).fetchone()
    if row is None:
        raise CouponNotFound(code)
    if row[0] is not None:
        raise CouponAlreadyUsed(code)
    db.execute("UPDATE coupons SET redeemed_by = ? WHERE code = ?", (user_id, code))

The tests redeem a code, redeem it again, and check that the second call raises CouponAlreadyUsed. It does.

What goes wrong, and the input that exposes it

The old version asked and acted in one statement. The condition redeemed_by IS NULL sat inside the UPDATE, so the database checked it at the moment it wrote. The new version asks first and acts later, in two statements. Between them, another request can do the same.

The smallest input is not a value. It is an order of events, with one unused code and two requests for it:

  1. Request A reads the row: unused.
  2. Request B reads the row: unused.
  3. A writes its update, and the handler adds the credit.
  4. B writes its update, and the handler adds the credit again.

Neither request raises. A double click is enough to produce this, and so is a mobile client that retries after a slow response.

The old version survives the same order. In PostgreSQL at its default isolation level, read committed, B's UPDATE waits for A's row lock. When A commits, B checks its condition again against the new row, finds the code used, and changes nothing. rowcount is 0 and redeem returns False.

The tests missed it because they run one request after the other. A sequential test can never put two reads before two writes.

If your round is in Java, the same shape is if (!map.containsKey(k)) map.put(k, v) on a map that several threads share. On a ConcurrentHashMap, putIfAbsent does both in one step.

How to argue the severity

"That's a race, it'll almost never happen" is the reply to expect. Answer it with questions that have factual answers:

Then offer a fix that keeps what the author wanted. Write first, with the condition inside the UPDATE. Only when it changed 0 rows, read the row to choose the message: missing code or used code. The read explains a failure. It no longer decides one.

What to say out loud

"The new messages are a good change, but this moves the check out of the write. Before, the database checked 'not yet used' at the moment it updated. Now we read, decide, and update separately, so two requests can both read 'unused' and both succeed, and the handler adds credit twice. The tests can't see it because they run sequentially. I can show it with two sessions: both selects, then both updates. I'd block it, put the condition back in the UPDATE, and read the row only when 0 rows changed, to pick the right error."

That answer credits the goal, names the gap, gives the order of events that breaks it, says why the tests passed, and offers a fix that keeps the better messages.

The exercise

  1. Find code you wrote or reviewed that reads a value and then writes based on it: a balance, a stock count, a "not taken yet" flag, a unique name.
  2. For each one, write down the two statements and what changes between them if another request runs at the same time.
  3. Write the order of events, as numbered steps, in which two requests both pass the check.
  4. Rewrite it so the check happens in the write itself: a condition in the UPDATE, a unique constraint, or an atomic method such as putIfAbsent.
  5. Say the out-loud answer above for your own example in under forty-five seconds, ending with how you would reproduce it.

Where this fits in Gadfly

Gadfly has four rooms: algorithms, system design, behavioral and code review. The code review room takes a class you wrote yourself, a connected GitHub repository, or a project from its catalog, and reviews it together with the transcript of how the code was produced. It does not execute code, so an interleaving like the one above is something you lay out step by step and explain aloud rather than run in the room. Code review is part of a session pack. You code in Java or Python, and the tutor speaks English.

Published 3 December 2026 · Code review: find it, then defend it · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].