The Real Fullstack

The Broken Saga

The order failed and was compensated. Why did the customer get two refunds and an e-mail that lied?

معماري10 دقيقةمحاكي الخط الزمنيتحدي الشيفرة
microserviceskafkatemporaljava

لم يُترجم هذا التحدي إلى العربية بعد. أنت تقرأ النسخة الإنجليزية.

المهمة:With the refund timeout injected, order the compensations and make them safe to retry so the customer ends up with exactly one refund and a truthful notification.السياق · أهداف التعلّم ▾

Brightcart's checkout saga creates the order, reserves stock, charges the card and books a shipment. When the carrier rejects an address, the saga compensates. Last week the refund call timed out (but succeeded), was retried, and refunded twice; the cancellation e-mail had already promised “refunded”.

المهمة: With the refund timeout injected, order the compensations and make them safe to retry so the customer ends up with exactly one refund and a truthful notification.

أهداف التعلّم

  • Design compensations for completed saga steps.
  • Order compensations so side effects happen before notifications.
  • Make compensations idempotent because they are retried.
المحاكاةمحاكاة
orchestrator
1createOrder (PENDING) ✓
2reserveStock ✓
3chargePayment ✓
4bookShipment ✕ carrier rejected address
compensation
5cancelOrder → e-mail “refunded, stock released” sent before it happened
6releaseStock ✓
7refundPayment → timeout (refund was actually executed)
8retry refundPayment → second refund issued
تشغيل المحاكاة
// Orchestrated saga: compensate completed steps in reverse order, notify last.try {  orders.create(cmd); stock.reserve(cmd); payments.charge(cmd); shipping.book(cmd);} catch (StepFailed e) {  payments.refund(cmd.orderId(), refundKey(cmd));   // idempotent: same key → no second refund on retry  stock.release(cmd.orderId());  orders.cancel(cmd.orderId());                     // customer e-mail only after money and stock are back}

تبسيط

One failure point (shipment) and one timeout. Real sagas also persist state between steps and handle compensation failures. نموذج تعليمي — الأرقام توضّح سلوكًا وليست قياسات أداء.

المفاهيم

مفيد قبل هذا التحدي

تحديات ذات صلة

المراجع