การทำงานพร้อมกันและการจัดการเธรด
กำหนดค่าและจัดการการทำงานพร้อมกันของผู้ใช้ภายในแอปพลิเคชัน Spring Boot Kafka เพื่อเพิ่มประสิทธิภาพอัตราการประมวลผลและการใช้ทรัพยากร
การทำงานพร้อมกันและการจัดการเธรด เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Boosting Consumer Throughput
When consuming messages from Kafka, processing them sequentially might not be fast enough. Concurrency allows your application to process multiple messages in parallel, significantly increasing throughput.
This is crucial for high-volume topics where messages arrive rapidly and need quick processing.
Default Consumer Behavior
By default, a @KafkaListener method in Spring Boot will create one consumer thread per listener container. This single thread is responsible for fetching and processing messages from the partitions assigned to it.
While simple, this setup doesn't always leverage multi-core processors for parallel message processing from a single topic effectively.
The `concurrency` Property
Spring for Apache Kafka provides a powerful concurrency property that allows you to specify the number of consumer threads to run for a given listener.
- Each concurrent consumer runs in its own thread.
- These threads collectively manage the partitions assigned to the consumer group.
This is key to scaling your consumer horizontally within a single application instance.
Setting Concurrency Level
You can set the concurrency level directly on the @KafkaListener annotation or globally via configuration properties.
For example, @KafkaListener(topics = "my-topic", groupId = "my-group", concurrency = "3") will start 3 consumer threads.
Alternatively, you can set it in application.properties for all listeners using a specific container factory.
Concurrency in Action
Let's see how to configure a listener with concurrency. This listener will process messages from "my-topic" using 3 concurrent threads.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@SpringBootApplication
@EnableKafka
public class ConcurrencyApp {
public static void main(String[] args) {
SpringApplication.run(ConcurrencyApp.class, args);
}
@Component
static class MyKafkaListener {
@KafkaListener(topics = "my-topic", groupId = "my-group", concurrency = "3")
public void listen(String message) {
System.out.println("Thread " + Thread.currentThread().getId() +
" received: " + message);
}
}
}How Threads are Managed
Spring Kafka uses ConcurrentKafkaListenerContainerFactory to create and manage the underlying consumer threads. When you set concurrency, this factory creates that many KafkaMessageListenerContainer instances.
Each container manages one consumer instance, which in turn is assigned a subset of topic partitions.
Advanced Thread Pool Customization
For more fine-grained control, you can customize the thread pool used by the container factory. This involves creating your own ConcurrentKafkaListenerContainerFactory bean.
You can set properties like max.poll.records and max.poll.interval.ms to optimize how many messages each consumer fetches and how often it commits offsets.
Partitions and Concurrency
The effective concurrency for a consumer group is limited by the number of partitions in the topic. Kafka guarantees that messages within a single partition are processed in order.
- If you have 5 partitions and set
concurrency = 10, only 5 threads will be active (one per partition). - It's best practice to set
concurrencyto be less than or equal to the number of topic partitions.
Key Concurrency Considerations
While concurrency boosts throughput, keep these in mind:
- Order: Messages within a single partition are ordered, but overall order across partitions is NOT guaranteed.
- Resource Usage: More threads mean more memory and CPU. Monitor your application's resources.
- Rebalancing: High concurrency can lead to more frequent consumer rebalances if not managed well.
Concurrency Quiz
Imagine a Kafka topic named "orders" with 4 partitions. A Spring Boot application has a @KafkaListener configured for this topic within a consumer group. If the concurrency property is set to 6, how many consumer threads will actively process messages from the "orders" topic?
Concurrency Recap
In this lesson, we explored how to manage consumer concurrency in Spring Boot Kafka applications. We learned about the concurrency property, how it relates to topic partitions, and key considerations for using it effectively.
By configuring concurrency, you can significantly optimize your application's throughput and resource utilization for high-volume event processing.
เรียนรู้ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การทำงานพร้อมกันและการจัดการเธรด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำงานพร้อมกันและการจัดการเธรด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำงานพร้อมกันและการจัดการเธรด”
กำหนดค่าและจัดการการทำงานพร้อมกันของผู้ใช้ภายในแอปพลิเคชัน Spring Boot 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำงานพร้อมกันและการจัดการเธรด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การยืนยันออฟเซ็ตด้วยตนเอง
- การหยุดชั่วคราวและเริ่มผู้ใช้ใหม่
- การทำงานพร้อมกันและการจัดการเธรด
- ตัวรับฟังการปรับสมดุลใหม่และสมาชิกแบบคงที่