0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ

ทำให้การทำงานของไมโครเซอร์วิสปลอดภัยต่อการลองใหม่ด้วยการออกแบบตัวจัดการที่ไม่ขึ้นต่อการทำซ้ำและขจัดข้อความซ้ำในระบบแบบกระจาย

ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Retries Cause Duplicates

Distributed systems deliver messages at least once. A timeout or retry can make the same request arrive twice — and charge a card or create an order twice.

What Idempotency Means

An operation is idempotent if doing it many times has the same effect as doing it once. Repeating it changes nothing beyond the first application.

Naturally Idempotent Operations

Some operations are idempotent by nature — absolute SET, HTTP PUT and DELETE. Others, like balance = balance + 10, double-apply on a retry.

The Idempotency Key

The standard fix is an idempotency key: the client generates a unique id per operation and resends it on every retry, so the server processes it only once.

POST /payments
Idempotency-Key: 9f1c-7a22-...
{ "amount": 4999 }

Storing Processed Keys

The server records each processed key. If the same key reappears, it returns the stored result instead of re-running the work.

if (store.exists(key)) return store.result(key);
Result r = process(request);
store.save(key, r);
return r;

Deduplication at the Consumer

For queues, the consumer does deduplication: it tracks message ids it has already handled and silently skips any repeats.

if (seen.contains(msg.id())) { ack(msg); return; }
handle(msg);
seen.add(msg.id());
ack(msg);

Atomicity Matters

Recording the key and doing the work must be atomic. If they are separate, a crash between them reopens the duplicate window — use a transaction or unique constraint.

INSERT INTO processed(key) VALUES (?) -- unique constraint
-- if it fails, the work already ran

Expiring the Dedup Store

You cannot store keys forever. Give the dedup store a TTL sized to your max retry window — Redis with expiry is a common choice.

redis.set("idem:" + key, result, "EX", 86400, "NX");

Idempotency vs Exactly-Once

True exactly-once delivery is extremely hard. The practical pattern is at-least-once delivery plus idempotent processing, giving effectively-once behavior.

Designing Idempotent Events

Make event handlers idempotent: include a unique event id, prefer absolute state changes, and guard side effects so a redelivered event never repeats them.

Where to Apply It

Apply idempotency at every retry boundary — API gateways, queue consumers, saga steps, webhooks. Anywhere a message can arrive twice needs a dedup strategy.

Quick Check

The same message just arrived twice — does your design still do the right thing? Check your idempotency grasp.

Recap

You made operations retry-safe: at-least-once causes duplicates, idempotent operations repeat harmlessly, use keys and dedup, keep record-and-process atomic with a TTL.

คำถามที่พบบ่อย

บทเรียน “ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ”

ทำให้การทำงานของไมโครเซอร์วิสปลอดภัยต่อการลองใหม่ด้วยการออกแบบตัวจัดการที่ไม่ขึ้นต่อการทำซ้ำและขจัดข้อความซ้ำในระบบแบบกระจาย คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสื่อสารแบบประสานเวลาเทียบกับแบบไม่ประสานเวลา
  2. ความท้าทายในระบบกระจาย
  3. เกตเวย์ API และการค้นหาบริการ
  4. ความไม่ขึ้นต่อการทำซ้ำและการขจัดข้อความซ้ำ
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)