การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ
จัดการผู้ผลิตข้อมูลที่เร็วกับไคลเอ็นต์ WebSocket ที่ช้าผ่าน WebFlux โดยใช้ตัวดำเนินการแรงดันย้อนกลับของ Reactor เพื่อให้สตรีมยังเสถียรภายใต้โหลด
การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Fast Producer, Slow Consumer Problem
A reactive WebSocket may emit market ticks faster than a client can consume them. Without control, buffers grow until memory is exhausted. This mismatch is what backpressure solves.
What Backpressure Means
Backpressure is the consumer telling the producer how much it can handle. In Reactor, the subscriber requests n items; the publisher must not exceed that demand.
Reactor Is Demand-Driven
A reactive Flux built from a cold source naturally honors demand: nothing is produced until requested. The challenge appears with hot, time-driven sources like a price feed that emits regardless of demand.
WebSocketHandler Returns a Mono
In WebFlux a handler wires the outbound Flux into session.send. The framework subscribes and applies the transport's demand for you.
public Mono<Void> handle(WebSocketSession session) {
Flux<String> out = prices.map(p -> session.textMessage(p));
return session.send(out);
}onBackpressureBuffer
Buffer overflow items up to a limit, then take an action. Good when bursts are short.
flux.onBackpressureBuffer(1000,
dropped -> log.warn("dropped {}", dropped),
BufferOverflowStrategy.DROP_OLDEST);onBackpressureDrop
When the consumer is slow, simply drop new items. Ideal for telemetry where only the latest values matter.
flux.onBackpressureDrop(dropped -> metrics.increment("dropped"));onBackpressureLatest
Keep only the most recent item, discarding intermediate ones. Perfect for a live dashboard that shows the current value, not the history.
flux.onBackpressureLatest();Sampling and Throttling
Instead of dropping reactively, reduce the rate up front. sample emits the latest value at a fixed interval, smoothing a firehose into a manageable stream.
flux.sample(Duration.ofMillis(200));Bounding Buffers Everywhere
Unbounded buffers are the silent killer. Always cap buffers and choose a strategy (drop, error, latest) so a stalled client cannot consume the server's heap.
Detecting Overwhelmed Clients
If a client repeatedly triggers drops, it may be too slow for the feed. Consider lowering its update rate, sending deltas, or closing the session with a clear status.
session.close(CloseStatus.create(1011, "client too slow"));Choosing a Strategy
Match the operator to the data:
- Must-not-lose orders →
bufferwith a safe cap, or error - Live metrics/prices →
latestorsample - Best-effort telemetry →
drop
Quick Check
Test your backpressure understanding.
Recap
You tamed reactive streams:
- Backpressure aligns a fast producer with a slow consumer
- Reactor is demand-driven; hot sources need explicit handling
buffer,drop, andlateststrategies suit different datasamplethrottles a firehose at the source- Never leave buffers unbounded; close clients that cannot keep up
เรียนรู้ WebSockets & Real-Time Systems with Spring ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ”
จัดการผู้ผลิตข้อมูลที่เร็วกับไคลเอ็นต์ WebSocket ที่ช้าผ่าน WebFlux โดยใช้ตัวดำเนินการแรงดันย้อนกลับของ Reactor เพื่อให้สตรีมยังเสถียรภายใต้โหลด คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม
ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่การเขียนโปรแกรมเชิงรีแอกทีฟ
- ตัวจัดการ WebFlux WebSocket
- การสร้างบริการเรียลไทม์เชิงรีแอกทีฟ
- การจัดการแรงดันย้อนกลับในสตรีมแบบรีแอกทีฟ