Skip to content

Demo 13: saga under a slow payment - #16

Merged
MAOFILHO merged 1 commit into
mainfrom
demo13/slow-payment
Nov 17, 2026
Merged

MAOFILHO merged 1 commit into
mainfrom
demo13/slow-payment

Conversation

@MAOFILHO

Copy link
Copy Markdown
Owner

Why

A reader of the LinkedIn post raised the saga's dangerous case: payment times out, the breaker counts a failure, the saga releases the stock, and the customer was charged anyway. The two fixes he named are built here.

What changed

  • An idempotency key on every charge. The key is the saga id. A retry returns the first charge, and a reused key with a different amount is refused (409).
  • A void before any undo. After a timeout, the order calls POST /api/payment/charges/<key>/void:
    • a key payment never saw becomes VOIDED, so a late charge is refused;
    • a charge stuck in PROCESSING for 30 s is voided;
    • anything else comes back as it stands.
  • The outcome. APPROVED confirms the order. DECLINED (402) or VOIDED (503) releases the stock. PROCESSING or no answer leaves the order pending, with the stock held, the cart locked (409), and a background check every 5 s.
  • The demo page. /patterns/slow-payment shows where the money and the stock end up.

Tests

  • 181 unit tests pass.
  • 25 local smoke tests pass, including the slow-payment path (1 charge, 1 sale) and the payment-down path (VOIDED, stock unchanged).
  • The Pact contract is regenerated and verified by payment.
  • The frontend typechecks and builds.

🤖 Generated with Claude Code

…e undo, pending orders

A LinkedIn reader pointed out the saga's dangerous case: a charge that times out may have gone
through, and releasing the stock then leaves a customer charged with no order.

- Every charge carries the saga id as its idempotency key; a retry returns the first charge, and a
  reused key with a different amount answers 409.
- Payment records PROCESSING before the slow part. After a timeout the saga voids the key
  (POST /api/payment/charges/<key>/void): an unseen key becomes VOIDED, so a late charge is refused;
  a charge stuck in PROCESSING for 30 s is voided; anything else is returned as it stands.
- APPROVED confirms the order, DECLINED (402) or VOIDED (503) releases the stock, PROCESSING or no
  answer leaves the order pending with the stock held. reconcile.py settles pending orders every 5 s.
- Demo page /patterns/slow-payment, Pact contract updated, smoke tests for both outcomes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MAOFILHO
MAOFILHO merged commit 72f2340 into main Nov 17, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant