0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่

เรียนรู้การใช้ @RetryableTopic ของ Spring Kafka เพื่อทำการลองใหม่แบบไม่บล็อกและหน่วงเวลา ซึ่งช่วยให้ผู้บริโภคยังตอบสนองได้ขณะประมวลผลข้อความที่ล้มเหลวอีกครั้ง

การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่ เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

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

The Problem with Blocking Retries

Blocking retries (Spring Retry) pause the consumer thread between attempts. A long backoff blocks the partition, stalling all later records.

Non-blocking retries solve this by moving failed records to dedicated retry topics.

How Retry Topics Work

When processing fails, the record is forwarded to a retry topic with a delay. A separate listener consumes the retry topic after the delay and tries again.

  • The main consumer keeps moving forward.
  • Each retry level can have a longer delay.

Enabling RetryableTopic

Annotate your listener with @RetryableTopic. Spring auto-creates the retry and DLT topics.

@RetryableTopic
@KafkaListener(topics = "orders")
public void handle(Order order) {
    paymentService.charge(order);
}

Configuring Attempts and Backoff

Customize the number of attempts and the delay strategy directly in the annotation.

@RetryableTopic(
    attempts = "4",
    backoff = @Backoff(delay = 1000, multiplier = 2.0))
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }

Generated Topic Names

By default Spring creates topics like orders-retry-0, orders-retry-1, and finally orders-dlt.

Each retry topic corresponds to one backoff level.

Exponential vs Fixed Backoff

Set multiplier for exponential growth, or omit it for fixed delays.

  • Fixed: 1s, 1s, 1s.
  • Exponential: 1s, 2s, 4s.

Exponential is better for transient downstream outages.

Selecting Retryable Exceptions

Only retry exceptions that are transient. Use include or exclude to control which exceptions trigger retries.

@RetryableTopic(
    include = { RemoteServiceException.class },
    exclude = { ValidationException.class })
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }

Handling the DLT

After all attempts fail, the record lands on the DLT. Add a @DltHandler method to react.

@DltHandler
public void handleDlt(Order order) {
    log.error("Order permanently failed: {}", order.getId());
}

Reading Retry Headers

Retry records carry headers such as the attempt count and original timestamp, letting you log how far a message progressed.

@Header(KafkaHeaders.ORIGINAL_TOPIC) String originalTopic

Blocking vs Non-Blocking

Use blocking retries for very short, immediate transients; use non-blocking retry topics when delays are longer and partition throughput matters.

Putting It Together

Non-blocking retries keep consumers responsive while still giving failed messages multiple chances. Combine @RetryableTopic, exponential backoff, exception filtering, and a @DltHandler.

Quick Check

Test your understanding of retry topics.

Recap

You learned non-blocking retries.

  • @RetryableTopic creates retry topics automatically.
  • Configure attempts, backoff, and a multiplier.
  • Filter retryable exceptions with include/exclude.
  • Use @DltHandler for permanently failed records.

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

บทเรียน “การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

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

เรียนรู้การใช้ @RetryableTopic ของ Spring Kafka เพื่อทำการลองใหม่แบบไม่บล็อกและหน่วงเวลา ซึ่งช่วยให้ผู้บริโภคยังตอบสนองได้ขณะประมวลผลข้อความที่ล้มเหลวอีกครั้ง คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม

ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การจัดการข้อยกเว้นของผู้บริโภค
  2. กลไกการลองใหม่ด้วย Spring Retry
  3. การใช้งานหัวข้อจดหมายตีกลับ (DLT)
  4. การลองใหม่แบบไม่บล็อกด้วยหัวข้อการลองใหม่
← กลับไปที่ Advanced Spring Boot 4: Event-Driven Architecture (Kafka)