รูปแบบ Saga สำหรับธุรกรรมแบบกระจาย
เรียนรู้การประสานการเปลี่ยนแปลงข้อมูลข้ามบริการหลายรายการโดยไม่ใช้ธุรกรรมแบบกระจาย ด้วยรูปแบบ saga และการดำเนินการชดเชย
รูปแบบ Saga สำหรับธุรกรรมแบบกระจาย เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 สำหรับธุรกรรมแบบกระจาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบ Saga สำหรับธุรกรรมแบบกระจาย”
เรียนรู้การประสานการเปลี่ยนแปลงข้อมูลข้ามบริการหลายรายการโดยไม่ใช้ธุรกรรมแบบกระจาย ด้วยรูปแบบ saga และการดำเนินการชดเชย คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างไมโครเซอร์วิสขับเคลื่อนด้วยเหตุการณ์
- การผสานรวมกับ Amazon EventBridge
- การประมวลผลแบบเรียลไทม์ด้วย Kinesis
- รูปแบบ Saga สำหรับธุรกรรมแบบกระจาย