Upgrade to Pro — share decks privately, control downloads, hide ads and more …

コード決済における冪等性と整合性 / #merpay_techtalk

susho
December 18, 2019

コード決済における冪等性と整合性 / #merpay_techtalk

susho

December 18, 2019
Tweet

More Decks by susho

Other Decks in Technology

Transcript

  1. About me • susho (@susho0220) • Backend Engineer • Code

    Payment Team at Merpay • Go歴4年くらい • 2018年までYahooでオブジェクトストレージ Dragonの開発に従事
  2. メルペイのコード決済 • CPM(Consumer-Presented Mode) ◦ 一般社団法人キャッシュレス推進協議会が定める統一 QRコード・バーコード「JPQR」に準拠し たコード決済スキーム「 Smart Code(TM)」を採用。

    • 静的MPM(Merchant-Presented Mode) ◦ 国際ブランド6社からなる技術団体 EMVCoによって策定された共通規格である EMVを採用。 ◦ QRコードのEncode/Decodeには、自社で開発した OSSライブラリを利用。 ▪ mercari/go-emv-code ※ 「Smart Code」は、JCBの商標です。 ※QRコードは(株)デンソーウェーブの登録商標です。 ※EMV® is a registered trademark in the U.S. and other countries and an unregistered trademark elsewhere. The EMV trademark is owned by EMVCo, LLC. CPM MPM
  3. モノリシックアーキテクチャにおけるコード決済処理 Monolithic Service 1.request 3.begin tx 4.authorize & capture payment

    5. commit tx 2.validate QR Code 6.reply 3-5のどれかでエラーになった としても、データベースの Atomic性の保証によって整合 性は保たれる。
  4. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment
  5. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Pending 3.begin tx 4.mark state as pending 5.commit tx
  6. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Pending Pending 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment
  7. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Pending Authorized 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment
  8. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Pending Authorized 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment 8.reply
  9. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Authorized Authorized 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment 8.reply 9.begin tx 10.mark state as authorized 11.commit tx
  10. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Authorized Authorized 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment 8.reply 9.begin tx 10.mark state as authorized 11.commit tx 12.capture payment
  11. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Authorized Captured 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment 8.reply 9.begin tx 10.mark state as authorized 11.commit tx 12.capture payment 13. capture payment
  12. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 2.validate QR Code •

    Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Authorized Captured 3.begin tx 4.mark state as pending 5.commit tx 6.authorize payment 7. authorize payment 8.reply 9.begin tx 10.mark state as authorized 11.commit tx 12.capture payment 13. capture payment 14.reply
  13. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 6.authorize payment 7. authorize payment 8.reply 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 Code Payment Internal Payment Captured Captured
  14. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 7. authorize payment 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 • Failed: 決済失敗 Code Payment Internal Payment 3-5でエラーになったとしても、 データベースのAtomic性の保 証によって整合性は保たれ る。 6.authorize payment 8.reply
  15. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 7. authorize payment 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 • Failed: 決済失敗 Code Payment Internal Payment Pending Pending? Authorized? Failed? Internal Paymentの状態が不 定なので不整合が発生してし まう。 6.authorize payment 8.reply
  16. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 7. authorize payment 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 • Failed: 決済失敗 Code Payment Internal Payment Pending Authorized Code Paymentの状態と Internal Paymentの状態に不 整合が発生してしまう。 6.authorize payment 8.reply
  17. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 7. authorize payment 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 • Failed: 決済失敗 Code Payment Internal Payment Authorized Authorized? Captured? 6.authorize payment 8.reply Internal Paymentの状態が不 定なので不整合が発生してし まう。
  18. マイクロサービスアーキテクチャにおけるコード決済処理 Code Payment Internal Payment 1.request 3.begin tx 4.mark state

    as pending 5.commit tx 2.validate QR Code 7. authorize payment 9.begin tx 10.mark state as authorized 11.commit tx 18.reply 12.capture payment 13. capture payment 14.reply 15.begin tx 16.mark state as captured 17.commit tx • Pending: 初期状態 • Authorized: 残高の仮押さえ • Captured: 決済確定 • Failed: 決済失敗 Code Payment Internal Payment Authorized Captured Code Paymentの状態と Internal Paymentの状態に不 整合が発生してしまう。 6.authorize payment 8.reply
  19. バッチによる定期的な不整合修復 Code Payment Internal Payment 1.select pending state transactions Code

    Payment Internal Payment Pending Pending? Authorized? Failed? • Pending: 初期状態 • Authorized: 残高の仮押さえ • Failed: 決済失敗
  20. バッチによる定期的な不整合修復 Code Payment Internal Payment 1.select pending state transactions Code

    Payment Internal Payment Pending Pending? Authorized? Failed? • Pending: 初期状態 • Authorized: 残高の仮押さえ • Failed: 決済失敗 2.get payment 3.reply
  21. バッチによる定期的な不整合修復 Code Payment Internal Payment 1.select pending state transactions Code

    Payment Internal Payment Pending Pending? Authorized? Failed? • Pending: 初期状態 • Authorized: 残高の仮押さえ • Failed: 決済失敗 2.get payment 3.reply 4.check payment state
  22. バッチによる定期的な不整合修復: Internal Payment State is Pending Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Pending Pending • Pending: 初期状態 • Authorized: 残高の仮押さえ • Failed: 決済失敗 2.get payment 3.reply Internal Payment側のリトライ によってAuthorized or Failed に遷移するまで待つ。 4.check payment state
  23. バッチによる定期的な不整合修復: Internal Payment State is Authorized Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Pending Authorized • Pending: 初期状態 • Authorized: 残高の仮押さえ 2.get payment 3.reply 4.check payment state
  24. バッチによる定期的な不整合修復: Internal Payment State is Authorized Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Pending Voided • Pending: 初期状態 • Authorized: 残高の仮押さえ • Voided: 仮押さえの取消 2.get payment 3.reply 5.void payment 6. void payment 7.reply 4.check payment state
  25. バッチによる定期的な不整合修復: Internal Payment State is Authorized Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Voided Voided • Pending: 初期状態 • Authorized: 残高の仮押さえ • Voided: 仮押さえの取消 2.get payment 3.reply 5.void payment 6. void payment 7.reply 8.begin tx 9.mark state as voided 10.commit tx 4.check payment state
  26. バッチによる定期的な不整合修復: Internal Payment State is Failed Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Pending Failed • Pending: 初期状態 • Failed: 決済失敗 2.get payment 3.reply 4.check payment state
  27. バッチによる定期的な不整合修復: Internal Payment State is Failed Code Payment Internal Payment

    1.select pending state transactions Code Payment Internal Payment Failed Failed • Pending: 初期状態 • Failed: 決済失敗 2.get payment 3.reply 8.begin tx 9.mark state as failed 10.commit tx 4.check payment state
  28. メルペイのコード決済アーキテクチャ: 同期処理 Code Payment Transaction Internal Payment Pub/Sub 1.request 4.begin

    tx 5.mark state as pending 6.commit tx 2.validate QR Code 3.payment request 7.authorize payment 8. authorize payment 9.publish authorized event 10.reply 11.begin tx 12.mark state as authorized 13.commit tx 14.reply 15.reply
  29. メルペイのコード決済アーキテクチャ: 非同期処理 Transaction Internal Payment Pub/Sub 3.check state whether authorized

    or not 4.capture payment 5. capture payment 6.reply 7.begin tx 8.mark state as captured 9.commit tx 1.subscribe authorized event 2.reply
  30. メルペイのコード決済アーキテクチャ: バッチ処理 Transaction Internal Payment 1.select pending state transactions 2.get

    payment 3.reply 5.void payment 6. void payment 7.reply 8.begin tx 9.mark state as voided 10.commit tx 4.check payment state