Microservices Communication Patterns (Saga, Circuit Breaker) · درس

بروتوكول الالتزام ذي المرحلتين

افهم الالتزام ذي المرحلتين (2PC) للمعاملات الموزعة، وضماناته ونقاط ضعفه المتمثلة في الحجب، ولماذا تفضّل الخدمات المصغّرة استخدام sagas بدلًا منه.

الدرس 4 من 413 خطوة

بروتوكول الالتزام ذي المرحلتين درس مجاني في Microservices Communication Patterns (Saga, Circuit Breaker) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Microservices Communication Patterns (Saga, Circuit Breaker)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Microservices Communication Patterns (Saga, Circuit Breaker) 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Need for Atomic Distributed Writes

When one business action spans two databases, you want all-or-nothing: both commit or both roll back. A single local transaction cannot span separate systems, so we need a coordination protocol.

Enter Two-Phase Commit

Two-phase commit (2PC) is a classic protocol where a coordinator drives multiple participants to a unanimous decision: commit or abort.

Phase 1: Prepare

The coordinator asks every participant: can you commit? Each does the work, locks the resources, writes to its log, and replies yes (prepared) or no.

Phase 2: Commit or Abort

If all said yes, the coordinator tells everyone to commit. If anyone said no, it tells everyone to abort. The decision is final and recorded.

The Guarantee

2PC provides atomicity across participants: there is no state where one committed and another aborted, as long as the protocol completes.

The Blocking Problem

Here is the fatal flaw: if the coordinator crashes after phase 1, participants are stuck holding locks, unable to decide. They block indefinitely until the coordinator recovers.

Locks Kill Throughput

Resources stay locked for the whole protocol round-trip. Across services and networks this dramatically reduces concurrency and availability, which is unacceptable for high-scale microservices.

The CAP Tension

2PC favors consistency over availability. A network partition during commit can freeze the whole transaction, violating the availability that distributed systems usually prioritize.

XA Transactions

The XA standard implements 2PC across resource managers (databases, JMS brokers). It works, but it tightly couples services and inherits all of 2PC's blocking issues.

Why Microservices Avoid 2PC

Microservices prize loose coupling and availability. They generally reject 2PC in favor of the saga pattern: a sequence of local transactions with compensating actions, accepting eventual consistency instead of distributed locks.

When 2PC Still Makes Sense

2PC is not always wrong. Within a single trust boundary, low-volume, short transactions over reliable infrastructure (e.g. one DB plus one message broker) can use it safely.

Quick Check

Test your 2PC understanding.

Recap

You learned 2PC:

  • 2PC coordinates an all-or-nothing commit across participants
  • Phase 1 prepares (locks and votes); phase 2 commits or aborts
  • It guarantees atomicity but blocks on coordinator failure
  • Locks and the CAP trade-off hurt availability at scale
  • Microservices prefer sagas; 2PC suits small, reliable, single-boundary cases
البدء مجانًا

تعلم Microservices Communication Patterns (Saga, Circuit Breaker) مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «بروتوكول الالتزام ذي المرحلتين» مجاني؟

نعم — نص درس «بروتوكول الالتزام ذي المرحلتين» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Microservices Communication Patterns (Saga, Circuit Breaker)، انتقل إلى CoddyKit PRO. تتضمن دورة Microservices Communication Patterns (Saga, Circuit Breaker) 4 دروس في المجموع.

ماذا ستتعلم في «بروتوكول الالتزام ذي المرحلتين»؟

افهم الالتزام ذي المرحلتين (2PC) للمعاملات الموزعة، وضماناته ونقاط ضعفه المتمثلة في الحجب، ولماذا تفضّل الخدمات المصغّرة استخدام sagas بدلًا منه. تتمرن على Microservices Communication Patterns (Saga, Circuit Breaker) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Microservices Communication Patterns (Saga, Circuit Breaker)؟

لا تُشترط خبرة سابقة. Microservices Communication Patterns (Saga, Circuit Breaker) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «بروتوكول الالتزام ذي المرحلتين»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Microservices Communication Patterns (Saga, Circuit Breaker) هذا؟

نعم. كل درس في Microservices Communication Patterns (Saga, Circuit Breaker) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مبادئ ACID مقابل BASE
  2. فهم الاتساق النهائي
  3. إدارة المعاملات في الخدمات المصغّرة
  4. بروتوكول الالتزام ذي المرحلتين
← العودة إلى Microservices Communication Patterns (Saga, Circuit Breaker)