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

التنسيق الموزّع

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

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

نظرة عامة

الموضوعات

  • عقود الإيجار (Leases)
  • مفاهيم انتخاب القائد
  • رموز الحماية (Fencing Tokens)

تعلّم أولًا

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

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

  1. Explain leases and leader election and when a system needs a single active worker.
  2. Explain why a lock held via an expired lease is unsafe and how fencing tokens prevent damage.
  3. Implement a single-runner scheduled job across instances (database-based lock, e.g., ShedLock).

الدروس

  1. 01عندما يجوز لنسخة واحدة فقط أن تعمل
  2. 02عقود الإيجار وانتخاب القائد
  3. 03الأقفال الموزّعة وحدودها
  4. 04رموز الحماية (Fencing Tokens)
  5. 05عرض عملي: مهمة ليلية تعمل مرة واحدة عبر ثلاث نسخ

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

Lab

MID-032-LAB1 · Single runner

Outcomes: 1–3 · Time: ~2 h Make the nightly reconciliation job in orders run once across three replicas using a database lock (ShedLock or an equivalent lease table). Add a fencing token: each lock grant increments a number that the job writes with its results, and the update rejects lower tokens.

Acceptance: a test that pauses the lock holder past its lease, lets another instance run, then resumes the first does not double-apply any change.

Exercises (assessment: quiz)

IDOutcomeTask
MID-032-EX011List three Shop jobs and say which truly need a single active worker and which can use another design.
MID-032-EX021Explain leader election and what must happen when the leader dies.
MID-032-EX032Draw the timeline of an unsafe lease: holder pause, expiry, takeover, double write.
MID-032-EX042Write the SQL update that applies a fencing token.
MID-032-EX053Configure lockAtMostFor and lockAtLeastFor for a job that normally takes 3 minutes and at worst 20.
MID-032-EX062Review a design that "uses Redis SET NX for exactly-once payouts" and recommend changes.
أظهر الحلول
  • EX01 Reconciliation: one runner (lock + fencing). Outbox relay: SKIP LOCKED. Email digest: lock or unique "sent for date" record. Cleanup of old rows: idempotent, can run anywhere.
  • EX02 Instances compete for a lease; the winner acts and renews it; if it dies the lease expires and another wins. The old leader must stop on failed renewal.
  • EX03 A acquires; A stalls; lease expires; B acquires and writes; A resumes and writes: both applied.
  • EX04 UPDATE state SET last_token = :t, ... WHERE id = :id AND last_token <= :t and treat 0 rows as stale.
  • EX05 lockAtMostFor of about 30 minutes (above worst case, still releases after a crash); lockAtLeastFor of 1 to 5 minutes to prevent immediate re-run through clock skew.
  • EX06 Redis locks cannot guarantee exclusion under pauses or failover. Make payouts idempotent (unique payout key), add fencing in the payout table, and use the lock only to reduce duplicate work.

Outcome 3 is mainly assessed by the lab and quiz Q09 and Q10.

الاختبار

Outcomes: O1 leases and leaders · O2 lock safety and fencing · O3 single-runner jobs.

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

  1. 1Three replicas each run a @Scheduled job. By default the job runs:O1
  2. 2A lease differs from a plain lock because it:O1
  3. 3Which design avoids needing a lock for a task queue?O1
  4. 4Why is an expired-lease holder dangerous?O2
  5. 5A fencing token is:O2
  6. 6Where must a fencing check live?O2
  7. 7A Redis SET NX PX lock alone is:O2
  8. 8A write with token 7 arrives after token 8 was accepted. The resource should:O2
  9. 9ShedLock is mainly used to:O3
  10. 10If lockAtMostFor is shorter than the job's runtime:O3

الجزء العملي

Six exercises MID-032-EX01 to EX06 plus MID-032-LAB1. Pass: at least five exercises, 8 of 10 quiz answers, and a paused-instance test with no double-applied change.

أخطاء شائعة

Trusting a Redis lock for correctness; no fencing.

التقييم

Quiz.

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

Consensus concepts (SRB-003).

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