انتقل إلى المحتوى
أمين أبوزيد
MID-031

سير العمل الموزّع

  • 8 درس
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • السّاغا (Sagas)
  • التنسيق الذاتي مقابل التنسيق المركزي (Choreography vs Orchestration)
  • التعويض (Compensation)

تعلّم أولًا

4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Model a business process as a saga with local transactions and compensations.
  2. Compare choreography and orchestration and choose for a given process.
  3. Implement an orchestrated saga with persisted state and timeouts.
  4. Design compensations that are idempotent and semantically correct.

الدروس

  1. 01لماذا لا تتوسع المعاملات الموزّعة
  2. 02السّاغا (Sagas)
  3. 03التنسيق الذاتي (Choreography)
  4. 04التنسيق المركزي (Orchestration)
  5. 05تصميم التعويض
  6. 06حالة السّاغا والمهلات والسّاغا العالقة
  7. 07عرض عملي: ساغا الطلب، المسار الناجح
  8. 08عرض عملي: رفض الدفع وتشغيل التعويضات

المختبر والتمارين

Lab

MID-031-LAB1 · Order saga

Outcomes: 1–4 · Time: ~4 h Implement an orchestrated saga in orders: place order → reserve stock (inventory) → authorize payment (payments) → confirm. Persist saga state with a deadline. On payment failure, release stock and cancel the order. Handle timeouts with a sweeper. Use the outbox and inbox from MID-029 for all messages.

Acceptance: every failure path is tested end to end: stock failure, payment declined, payment timeout, duplicate and late replies, orchestrator restart mid-saga.

Exercises (assessment: saga design for a new process, returns)

IDOutcomeTask
MID-031-EX011Explain in five sentences why 2PC across services is avoided and what replaces it.
MID-031-EX021Model "customer returns an item" as a saga: steps, local transactions, compensations, and non-undoable steps.
MID-031-EX032Compare choreography and orchestration for the returns saga and choose one with reasons.
MID-031-EX043Define the saga state enum and table for returns, including deadline and version.
MID-031-EX054Write an idempotent refund compensation that is safe if the payment was never captured.
MID-031-EX064A Reserve message arrives after Release for the same order. Describe the bug and the fix.
MID-031-EX073Define timeouts per step and what the sweeper does when each expires.
أظهر الحلول
  • EX01 2PC holds locks across services, blocks on coordinator failure, multiplies unavailability, and often is unsupported. Use local transactions with compensations.
  • EX02 Steps: create return REQUESTED; receive item (inventory); refund (payments); restock; close. Compensations: cancel return; none for received item (flag for inspection); void refund request. A "refund email" is last and non-undoable.
  • EX03 Orchestration: five steps with branching (item rejected after inspection) and deadlines, so one owner of the flow is clearer. Choreography fits only if the flow stays linear.
  • EX04 RETURN_REQUESTED, ITEM_RECEIVED, REFUNDED, COMPLETED, COMPENSATING, FAILED; table return_saga(id, state, deadline, attempts, version).
  • EX05 Look up capture by return id; if none or already refunded, return; otherwise refund with the return id as the idempotency key and store the refund id.
  • EX06 Without a record, the late reserve re-reserves stock that nobody releases. Store a tombstone (cancelled reservation by order id) and reject the reserve.
  • EX07 Example: reserve 30 s (retry twice, then compensate); payment 2 min (void and cancel); overall 10 min (mark FAILED and alert).

Quiz Q01 to Q03 also cover outcome 1; outcome 3 is mostly verified through the lab.

الاختبار

Outcomes: O1 saga model · O2 choreography vs orchestration · O3 orchestrated saga state · O4 compensation design.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A saga consists of:O1
  2. 2A weakness of 2PC across services:O1
  3. 3Sagas lack:O1
  4. 4In choreography:O2
  5. 5A flow of 6 steps with branching and deadlines usually suits:O2
  6. 6A risk of orchestration:O2
  7. 7Why persist saga state?O3
  8. 8A reply arrives for a saga already in COMPENSATING. The handler should:O3
  9. 9A compensation must be:O4
  10. 10Which is a good compensation for a captured payment?O4

الجزء العملي

Seven exercises MID-031-EX01 to EX07, the returns saga design, and MID-031-LAB1. Pass: at least six exercises, a returns design with compensations and timeouts, and a lab where all failure paths pass.

أخطاء شائعة

Compensations that assume the forward step happened; no timeout handling.

التقييم

Saga design for a new process (returns).

الصلة بالمسار الوظيفي

Required in the MID capstone; orchestration (SRB-001).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.