Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Introduction

In this tutorial, we’ll explore why we may see DataBufferLimitException in a Spring Webflux application. We’ll then take a look at the various ways we can resolve the same.

2. Understanding the Problem

Let’s understand the problem first before jumping to the solution.

2.1. What’s DataBufferLimitException?

Spring WebFlux limits buffering of data in-memory in codec to avoid application memory issues. By default, this is configured to 262,144 bytes. When this isn’t enough for our use case, we’ll end up with the DataBufferLimitException.

2.2. What’s a Codec?

The spring-web and spring-core modules provide support for serializing and deserializing byte content to and from higher-level objects through non-blocking I/O with reactive stream back pressure. Codecs offer an alternative to Java serialization. One advantage is that, typically, objects need not implement Serializable.  

3. Server Side

Let’s first look at how DataBufferLimitException plays out from a server perspective.

3.1. Reproducing the Issue

Let’s try to send a JSON payload of size 390 KB to our Spring Webflux server application to create the exception. We’ll use the curl command to send a POST request to our server:

curl --location --request POST 'http://localhost:8080/1.0/process' \
  --header 'Content-Type: application/json' \
  --data-binary '@/tmp/390KB.json'

As we can see, the DataBufferLimitException is thrown:

org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
  at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:99) ~[spring-core-5.3.23.jar:5.3.23]
  Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
  *__checkpoint ⇢ HTTP POST "/1.0/process" [ExceptionHandlingWebHandler]

3.2. Solution

We can use the WebFluxConfigurer interface to configure the same thresholds. To do this, we’ll add a new configuration class, WebFluxConfiguration:

@Configuration
public class WebFluxConfiguration implements WebFluxConfigurer {
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.defaultCodecs().maxInMemorySize(500 * 1024);
    }
}

We also need to update our application properties:

spring:
  codec:
    max-in-memory-size: 500KB

4. Client Side

Let’s now switch gears to look at the client-side behavior.

4.1. Reproducing the Issue

We’ll try to reproduce the same behavior with Webflux’s WebClient. Let’s create a handler that calls the server with a payload of 390 KB:

public Mono<Users> fetch() {
    return webClient
      .post()
      .uri("/1.0/process")
      .body(BodyInserters.fromPublisher(readRequestBody(), Users.class))
      .exchangeToMono(clientResponse -> clientResponse.bodyToMono(Users.class));
}

We see again that the same exception is thrown but this time due to the webClient trying to send a larger payload than allowed:

org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
  at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:99) ~[spring-core-5.3.23.jar:5.3.23]
  Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    *__checkpoint ⇢ Body from POST http://localhost:8080/1.0/process [DefaultClientResponse]
    *__checkpoint ⇢ Handler com.baeldung.spring.reactive.springreactiveexceptions.handler.TriggerHandler@428eedd9 [DispatcherHandler]
    *__checkpoint ⇢ HTTP POST "/1.0/trigger" [ExceptionHandlingWebHandler]

4.2. Solution

We’ve also got a programmatic way to configure the web clients to achieve this goal. Let’s create a WebClient with the following configuration:

@Bean("progWebClient")
    public WebClient getProgSelfWebClient() {
        return WebClient
          .builder()
          .baseUrl(host)
          .exchangeStrategies(ExchangeStrategies
	  .builder()
	  .codecs(codecs -> codecs
            .defaultCodecs()
            .maxInMemorySize(500 * 1024))
	    .build())
          .build();
}

We also need to update our application properties:

spring:
  codec:
    max-in-memory-size: 500KB

And with that, we should now be able to send payloads larger than 500 KB from our application. It’s worth noting that this configuration gets applied to the entire application, which means to all web clients and the server itself.

Hence, if we want to configure this limit only for specific web clients, then this won’t be an ideal solution. Additionally, there is a caveat with this approach. The builder used to create the WebClients must be auto-wired by Spring like the below:

@Bean("webClient")
  public WebClient getSelfWebClient(WebClient.Builder builder) {
  return builder.baseUrl(host).build();
}

5. Conclusion

In this article, we understood what DataBufferLimitException is and looked at how to fix them on both the server and client sides. We looked at two approaches for both, based on properties configuration and programmatically. We hope this exception won’t be a trouble for you anymore.

As always, the complete code is available over on GitHub.

Course – LS (cat=Spring)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> THE COURSE
res – Junit (guide) (cat=Reactive)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.