The Real Fullstack

The Duplicate Payment

The payment API returned an error. So why was the customer charged twice?

エンジニア10 分フローシミュレーターコード課題
spring-bootpostgresqlkafkahttpjavatypescript

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Break the flow on purpose to reproduce the double charge, then make the payment effect happen exactly once — under every combination of double clicks, timeouts, slow databases and message redelivery.背景 · 学習目標 ▾

Larkspur Tickets, a fictional event-ticketing company, takes card payments through an API gateway and a payment service that calls an external payment provider. On busy evenings a few customers are charged twice and receive two receipts. Each component "worked": the gateway retried a timeout, the service charged the card, the e-mail consumer sent a receipt.

ミッション: Break the flow on purpose to reproduce the double charge, then make the payment effect happen exactly once — under every combination of double clicks, timeouts, slow databases and message redelivery.

学習目標

  • Explain why networks force at-least-once delivery and why retries are still necessary.
  • Implement idempotency with a key and a uniqueness constraint in the same transaction.
  • Recognise that every consumer of an at-least-once stream needs its own deduplication.
シミュレーションシミュレーション値
CheckoutAPI gatewayPayment servicePayment providerPayments DBpayment-confirmedReceipt mailer
シミュレーションを実行
@PostMapping("/payments")public PaymentResult pay(@RequestBody PaymentRequest req) {  Charge charge = psp.charge(req.cardToken(), req.amount());   // every call charges  return PaymentResult.from(payments.save(Payment.of(req, charge)));}
@KafkaListener(topics = "payment-confirmed")public void onConfirmed(PaymentConfirmed e) {  mailer.sendReceipt(e.customerId(), e.paymentId());  // redelivery → second e-mail}

簡略化

Timing is reduced to four failure switches. Real systems also need key expiry, storing the original response, and handling a key reused with a different payload (409/422). 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

先に取り組むと役立つ課題

関連する課題

参考資料