CodexBloom - Programming Q&A Platform

Java 11: implementing Spring WebFlux and Reactive Streams during backpressure handling

👀 Views: 1 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-07
spring-boot webflux reactive-streams Java

I'm integrating two systems and I need help solving I'm stuck on something that should probably be simple... I'm working with a scenario with backpressure handling while using Spring WebFlux in my Java 11 application... I have a reactive stream that processes a large number of records from a database, but it seems that the backpressure isn't being respected, leading to memory issues and timeouts. When I run my application, I see the following behavior in the logs: ``` java.lang.OutOfMemoryError: Java heap space ``` Here's a simplified version of my code: ```java import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; @Service public class MyService { public Flux<Record> fetchRecords() { return Flux.defer(() -> Flux.fromIterable(database.findAll())) .subscribeOn(Schedulers.boundedElastic()) .onBackpressureBuffer(1000); // Buffering up to 1000 records } } ``` The `fetchRecords` method fetches records from a database and returns a `Flux` of these records. I thought using `onBackpressureBuffer` would help manage the flow, but I still encounter the `OutOfMemoryError`. I've tried increasing the JVM heap size with `-Xmx2g`, but the question continues. I've also attempted to implement `onBackpressureDrop`, but it seems to drop records too aggressively and doesn't suit my use case where I need all records retrieved. What am I missing? Should I consider using a different strategy for backpressure management, or is there a better way to handle the stream to avoid hitting memory limits? Any suggestions would be appreciated! I'd really appreciate any guidance on this. I'm developing on CentOS with Java. How would you solve this? I'm using Java 3.11 in this project. Is there a better approach? This is for a application running on Ubuntu 20.04. Hoping someone can shed some light on this.