Solid arrows: calls. Dashed: replies. Red: errors. Amber: conflicts. Dashed blue boxes: database transactions (each IdempotencyReserver method opens its own); amber boxes: alternative branches.
What to look for
- The
findByKey check is not what protects the payment: both requests pass it. The database UNIQUE constraint does. - The loser catches
DataIntegrityViolationException, reads the winner's row and answers from it (409 while PROCESSING). - If B arrives after A's reserve has committed, B finds the row directly and gets the same 409 without trying the INSERT.
PaymentController is left out to make room for both clients; it only passes the call through.