نمط Saga للمعاملات الموزعة
تعلّم تنسيق تغييرات البيانات عبر خدمات متعددة دون معاملات موزعة باستخدام نمط saga والإجراءات التعويضية.
نمط Saga للمعاملات الموزعة درس مجاني في Serverless AWS Lambda Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Serverless AWS Lambda Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Distributed Data Problem
In event-driven systems each service owns its own data. A single business operation may span several services, but there is no shared transaction to roll them all back.
What is a Saga?
A saga breaks one big transaction into a sequence of local transactions. If a step fails, earlier steps are undone with compensating actions.
A Worked Example
Placing an order: reserve inventory, charge payment, create shipment. If payment fails, you must release the reserved inventory.
Compensating Actions
Every forward step needs a matching undo. Reserve has Release; Charge has Refund. Compensations make the system eventually consistent.
steps = [
('reserveInventory', 'releaseInventory'),
('chargePayment', 'refundPayment'),
('createShipment', 'cancelShipment')
]Choreography Style
In choreography, each service reacts to events and emits the next event. There is no central coordinator, but the flow is implicit and harder to follow.
Orchestration Style
In orchestration, a coordinator (often AWS Step Functions) explicitly drives each step and triggers compensations on failure. The flow is centralized and visible.
Step Functions Fits Sagas
Step Functions supports a Catch on each task to invoke a compensation, making it a natural home for orchestrated sagas.
{
"ChargePayment": {
"Type": "Task",
"Resource": "arn:...:chargePayment",
"Catch": [{"ErrorEquals": ["States.ALL"], "Next": "ReleaseInventory"}],
"Next": "CreateShipment"
}
}Idempotency is Essential
Saga steps and compensations may be retried, so each must be idempotent. Use a unique transaction id to detect and ignore duplicates.
Eventual Consistency
During a saga the system is temporarily inconsistent. Design the UI and downstream consumers to tolerate states like "payment pending".
Semantic Locks
To avoid conflicts mid-saga, mark records with a pending status (a semantic lock) so other operations know the data is in flight.
When to Use a Saga
Use sagas when a workflow truly spans services. If everything lives in one service or one database, a normal local transaction is simpler and safer.
Quick Check
Test your saga knowledge.
Recap
You learned the saga pattern: split a distributed operation into local steps with compensating undos, choose choreography or orchestration, and keep steps idempotent.
الأسئلة الشائعة
هل درس «نمط Saga للمعاملات الموزعة» مجاني؟
نعم — نص درس «نمط Saga للمعاملات الموزعة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless AWS Lambda Development، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
ماذا ستتعلم في «نمط Saga للمعاملات الموزعة»؟
تعلّم تنسيق تغييرات البيانات عبر خدمات متعددة دون معاملات موزعة باستخدام نمط saga والإجراءات التعويضية. تتمرن على Serverless AWS Lambda Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Serverless AWS Lambda Development؟
لا تُشترط خبرة سابقة. Serverless AWS Lambda Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «نمط Saga للمعاملات الموزعة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Serverless AWS Lambda Development هذا؟
نعم. كل درس في Serverless AWS Lambda Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- بناء الخدمات المصغّرة المعتمدة على الأحداث
- التكامل مع Amazon EventBridge
- المعالجة الفورية باستخدام Kinesis
- نمط Saga للمعاملات الموزعة