Skip to main content

Deep Dive Backpressure Flow Control with WebClient in Spring Boot

· 5 min read

1. Introduction to Backpressure​

Backpressure is the mechanism in reactive systems that helps manage the flow of data between producers and consumers. In systems where data is produced at high rates, backpressure allows consumers to signal how much data they can process, preventing overload.

Without backpressure, if the producer emits data faster than the consumer can process, it can lead to memory overflows, crashes, or degraded system performance.


2. Why Backpressure is Important​

Backpressure ensures:

  • Efficient resource management
  • Prevents overwhelming downstream services
  • Provides resilience against data surges
  • Allows consumers to control the data inflow rate

Reactive systems like Spring’s WebClient use backpressure to provide flow control, especially when dealing with streaming data or high-throughput APIs.


3. WebClient: Enabling Backpressure​

WebClient, built on Project Reactor, supports backpressure natively through Flux and Mono types. By controlling the rate at which data is processed, WebClient ensures that reactive streams can handle large datasets or slow consumers.

Example initialization:

WebClient webClient = WebClient.builder()
.baseUrl("https://dummy.restapiexample.com/api/v1/employees")
.build();

WebClient operates on non-blocking, reactive pipelines where consumers can request more data as needed, unlike traditional blocking REST clients.


4. Use Case​

Example: Streaming Large Dataset with Backpressure​

Imagine you're building a data ingestion service that consumes streaming data from an external API, such as real-time stock market prices. The API provides thousands of updates per second. Your system must process this data without overwhelming downstream services (e.g., databases or other microservices).

You can use WebClient with backpressure to process these updates at a controlled rate, avoiding memory overloads and ensuring stability.

Code Example:​

WebClient webClient = WebClient.create("https://dummy.restapiexample.com/api/v1/employees");

Flux<StockPrice> stockPriceStream = webClient.get()
.uri("/stream")
.retrieve()
.bodyToFlux(StockPrice.class)
.limitRate(100); // Limit the rate of data processing

stockPriceStream
.doOnNext(stock -> processStockPrice(stock))
.doOnError(error -> handleError(error))
.subscribe();

Here, limitRate(100) ensures that only 100 stock price updates are processed at a time, preventing overload.


5. Backpressure Strategies in WebClient​

Strategy 1: limitRate​

The simplest method to apply backpressure is by limiting the rate at which data is processed using limitRate().

Example:

Flux<StockPrice> stockPriceStream = webClient.get()
.uri("/stream")
.retrieve()
.bodyToFlux(StockPrice.class)
.limitRate(50); // Controls backpressure with 50 items at a time

This limits the number of items requested and processed at a time.


Strategy 2: onBackpressureBuffer​

The onBackpressureBuffer operator buffers data when backpressure occurs, until the downstream can handle it.

Flux.range(1, 1000)
.onBackpressureBuffer(50)
.subscribe(System.out::println);

Strategy 3: onBackpressureDrop​

The onBackpressureDrop operator discards data when backpressure occurs to prevent downstream overload.

Flux.range(1, 1000)
.onBackpressureDrop()
.subscribe(System.out::println);

Strategy 4: Using Flux.window()​

Another method is using window(), which batches items into smaller chunks for processing.

Example:

Flux<StockPrice> stockPriceStream = webClient.get()
.uri("/stream")
.retrieve()
.bodyToFlux(StockPrice.class)
.window(10) // Process in windows of 10
.flatMap(window -> window.collectList())
.doOnNext(batch -> processBatch(batch))
.subscribe();

This groups items into windows of 10 before processing, providing an additional layer of control.


Strategy 5: Combining flatMap with Concurrency​

For more complex backpressure control, you can combine flatMap() with concurrency settings. This allows concurrent processing of items with backpressure.

Example:

webClient.get()
.uri("/stream")
.retrieve()
.bodyToFlux(StockPrice.class)
.flatMap(price -> processStockPrice(price), 5) // Concurrently process 5 items
.subscribe();

Here, flatMap() ensures that a maximum of 5 stock prices are processed concurrently at any given time.


6. Diagrams​

Diagram 1: Basic Backpressure Flow​

[Producer] ---> [Backpressure Mechanism (limitRate, window)] ---> [Consumer]
^ |
| v
+--------------------------------------------------------------
  1. Producer sends data.
  2. Backpressure mechanism (e.g., limitRate) controls the flow.
  3. Consumer processes data at a controlled rate.

Diagram 2: Streaming Data with Concurrency and Backpressure​

[API] ---> [WebClient (Stream)] ---> [Backpressure] ---> [Concurrent Processing] ---> [Final Consumer]
^ ^ | | |
| | | +---> [Worker 1] |
| +------------------------+ +---> [Worker 2] |
+------------------------------------------------------------------------------------------------+

7. Conclusion​

In reactive systems, backpressure is a key mechanism that prevents overload and ensures stable operation by allowing consumers to control the rate of data consumption. With WebClient, backpressure can be easily managed using strategies like limitRate, window, and flatMap with concurrency.

Compared to traditional, blocking clients like RestTemplate, WebClient’s backpressure control is a major advantage for high-throughput, non-blocking operations.

By implementing these techniques, you can design robust, scalable applications that handle large datasets and high-velocity data streams with ease.