รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้
เรียนรู้รูปแบบ outbox แบบมีธุรกรรม เพื่อยืนยันสถานะทางธุรกิจและข้อความขาออกอย่างเป็นอะตอมิก พร้อมขจัดปัญหาการเขียนสองแหล่งในระบบที่ขับเคลื่อนด้วยเหตุการณ์
รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้ เป็นบทเรียน RabbitMQ Messaging & Async Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน RabbitMQ Messaging & Async Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Dual-Write Problem
Saving to a database and publishing to RabbitMQ are two separate operations. If one succeeds and the other fails, your state and your events drift apart.
This is the dual-write problem.
What Is the Outbox Pattern?
The outbox pattern writes the business change and the outgoing message into the same database transaction, then publishes the message asynchronously from the outbox table.
The Outbox Table
Create a table that stores pending messages alongside your domain data.
CREATE TABLE outbox (
id UUID PRIMARY KEY,
payload JSONB,
published BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT now()
);Atomic Write
In one transaction, update the business row and insert the outbox row. Either both commit or neither does.
BEGIN;
INSERT INTO orders (id, status) VALUES ('42', 'paid');
INSERT INTO outbox (id, payload) VALUES (gen_random_uuid(), '{"order":"42"}');
COMMIT;The Relay Process
A separate relay (or message dispatcher) polls the outbox for unpublished rows, publishes them to RabbitMQ, and marks them published.
Polling the Outbox
The relay selects pending rows in order and publishes each one.
SELECT id, payload FROM outbox WHERE published = FALSE ORDER BY created_at;At-Least-Once Delivery
If the relay crashes after publishing but before marking the row, it republishes on restart. So consumers must be idempotent to tolerate duplicates.
Change Data Capture Alternative
Instead of polling, tools like Debezium tail the database write-ahead log and stream outbox inserts to RabbitMQ, reducing latency and load.
Combining With Confirms
The relay should use publisher confirms so it only marks a row published after the broker acknowledges the message, closing the loss window.
Outbox vs Saga
The outbox guarantees a single service reliably emits its events. A saga coordinates a multi-service workflow. They complement each other: each saga step can publish via its own outbox.
Cleanup and Retention
- Periodically delete or archive published rows
- Index on
publishedfor fast polling - Monitor relay lag to catch stalls early
Quick Check
Test your outbox understanding.
Recap
The outbox pattern stores outgoing messages in the same transaction as business data, then a relay publishes them to RabbitMQ asynchronously.
It eliminates the dual-write problem, requires idempotent consumers due to at-least-once delivery, and pairs well with publisher confirms and sagas.
คำถามที่พบบ่อย
บทเรียน “รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส RabbitMQ Messaging & Async Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้”
เรียนรู้รูปแบบ outbox แบบมีธุรกรรม เพื่อยืนยันสถานะทางธุรกิจและข้อความขาออกอย่างเป็นอะตอมิก พร้อมขจัดปัญหาการเขียนสองแหล่งในระบบที่ขับเคลื่อนด้วยเหตุการณ์ คุณปฏิบัติ RabbitMQ Messaging & Async Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน RabbitMQ Messaging & Async Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน RabbitMQ Messaging & Async Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน RabbitMQ Messaging & Async Systems นี้ได้ไหม
ได้ บทเรียน RabbitMQ Messaging & Async Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความเป็นไอดีมโพเทนต์ในการประมวลผลข้อความ
- รูปแบบ Saga ด้วย RabbitMQ
- การแยกความรับผิดชอบคำสั่งและการสอบถาม (CQRS)
- รูปแบบ Outbox สำหรับการเผยแพร่ที่เชื่อถือได้