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

การรับรองความไม่เปลี่ยนผลซ้ำใน Saga

นำการดำเนินการที่ไม่เปลี่ยนผลซ้ำภายในส่วนร่วมของ Saga ไปใช้ เพื่อป้องกันผลกระทบข้างเคียงที่ไม่ตั้งใจจากข้อความซ้ำหรือการลองใหม่

บทเรียน 1 จาก 412 ขั้นตอน

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

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

Understanding Idempotency

In distributed systems, idempotency is a crucial concept. An operation is idempotent if executing it multiple times produces the same result as executing it once.

  • Think of it like pressing a light switch: pressing it once turns it on (or off). Pressing it again doesn't change the state further if it's already on (or off).
  • This is vital because messages can be duplicated or retried.

The Challenge of Duplicates

When services communicate, especially asynchronously via message brokers, messages can sometimes be delivered more than once. This is known as "at-least-once" delivery.

  • Network issues: A service might send a response, but the sender doesn't receive it, leading to a retry.
  • Service failures: A service crashes after processing a message but before acknowledging it, so the message is redelivered.
  • Without idempotency, these duplicates can cause unintended side effects, like double-charging a customer or creating duplicate orders.

Idempotency in Sagas

The Saga pattern orchestrates complex business transactions across multiple services. Each step in a saga is an operation performed by a service.

  • If a saga step receives the same command or event twice, it could lead to inconsistent data.
  • For example, if a "deduct payment" command is processed twice, a customer's account could be overcharged.
  • Idempotency ensures that even if a saga participant receives a message multiple times, the overall business transaction remains correct.

Introducing the Idempotency Key

To achieve idempotency, we often use an idempotency key. This is a unique identifier associated with a specific operation or request.

  • The key is typically generated by the client or the initiating service and passed along with the request.
  • It allows the receiving service to detect if it has already processed this exact operation.
  • Commonly, this could be a UUID (Universally Unique Identifier) or a unique transaction ID.

The Check-Then-Act Pattern

A common approach to implementing idempotency is the "check-then-act" pattern. Before performing an action, the service checks if the operation associated with the idempotency key has already been completed.

Here's the basic logic:

  1. Receive a request with an idempotency key.
  2. Check if this key is already marked as processed.
  3. If processed, return the original result (or success) without re-executing.
  4. If not processed, execute the operation and then mark the key as processed.

Idempotent Processing Demo

Let's look at a conceptual Java example for an idempotent payment processing method. We'll use a simple in-memory set to track processed keys, though a real system would use a persistent store.

Try running this example:

import java.util.HashSet;
import java.util.Set;

public class PaymentProcessor {
  private static Set<String> processedKeys = new HashSet<>();

  public static String processPayment(String idempotencyKey, double amount) {
    if (processedKeys.contains(idempotencyKey)) {
      return "Payment (key: " + idempotencyKey + ") already processed.";
    }

    // Simulate payment processing
    System.out.println("Processing payment of $" + amount + " for key: " + idempotencyKey);
    processedKeys.add(idempotencyKey); // Mark as processed
    return "Payment of $" + amount + " (key: " + idempotencyKey + ") processed successfully.";
  }

  public static void main(String[] args) {
    System.out.println(processPayment("order-123-payment-A", 50.00));
    System.out.println(processPayment("order-124-payment-B", 75.00));
    System.out.println(processPayment("order-123-payment-A", 50.00)); // Duplicate
  }
}

Leveraging Database Features

For operations that involve database writes, you can often use database features to help enforce idempotency:

  • Unique Constraints: Add a unique constraint on the idempotency key column (e.g., request_id) in your table. If a duplicate key is inserted, the database will throw an error.
  • Conditional Updates (UPSERT): Use commands like INSERT ... ON CONFLICT DO NOTHING (PostgreSQL) or INSERT ... ON DUPLICATE KEY UPDATE (MySQL) to prevent inserting duplicates or to update only if a record exists.

Idempotent Order Creation

Consider creating an order. We want to ensure that if the same "create order" request is sent twice, only one order is created.

Using a unique request_id:

-- SQL example (conceptual)
INSERT INTO orders (order_id, customer_id, amount, request_id, status)
VALUES ('ORD001', 'CUST123', 100.00, 'req-uuid-123', 'PENDING')
ON CONFLICT (request_id) DO NOTHING;

This statement will insert the order if req-uuid-123 is new. If it already exists, the database ignores the insert, ensuring idempotency.

Idempotent Compensation Actions

Idempotency isn't just for forward-moving saga steps; it's equally important for compensation actions.

  • If a compensation request (e.g., "refund payment") is sent multiple times due to retries, you wouldn't want to issue multiple refunds.
  • Apply the same idempotency principles: use a unique key for the compensation request and check if it has already been processed before executing.
  • This ensures that the system correctly reverses the original action only once.

Idempotency Best Practices

To effectively implement idempotency in your sagas:

  • Use Robust Unique Keys: Generate truly unique, non-guessable IDs (like UUIDs) for each operation.
  • Store Processed Keys Persistently: Don't rely on in-memory storage. Use a database or a dedicated cache for tracking processed keys.
  • Handle Concurrency: Ensure your check-then-act logic is atomic to prevent race conditions where two identical requests are processed simultaneously. Database unique constraints are excellent for this.
  • Define Scope: Clearly define what constitutes an "idempotent operation" and at what level the key applies (e.g., per message, per business transaction).

Idempotency Check

A microservice receives a "charge customer" message with an idempotency key. Due to network issues, the message is delivered twice. If the service correctly implements idempotency, what will happen?

Recap: Keeping Sagas Consistent

We've learned that idempotency is critical for building robust distributed systems, especially when implementing the Saga pattern.

  • It ensures that an operation, when executed multiple times, yields the same result as executing it once.
  • This prevents unintended side effects from duplicate messages or retries, which are common in distributed environments.
  • By using idempotency keys and patterns like "check-then-act" or database unique constraints, saga participants can safely process messages, maintaining data consistency.
เริ่มต้นได้ฟรี

เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การรับรองความไม่เปลี่ยนผลซ้ำใน Saga” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การรับรองความไม่เปลี่ยนผลซ้ำใน Saga”

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

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การรับรองความไม่เปลี่ยนผลซ้ำใน Saga” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การรับรองความไม่เปลี่ยนผลซ้ำใน Saga
  2. กลยุทธ์การลองใหม่สำหรับ Saga
  3. ตรรกะการชดเชยขั้นสูง
  4. ล็อกเชิงความหมายและซากาที่ทำงานพร้อมกัน
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)