2

For the first time I noticed this error in my Spring WebFlux application:

org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

So WebFlux has a limit that it checks while deserializing client payload as described in the doc. I am curious to know why this check is not done in Spring Web MVC. Is an innovation for the most recent WebFlux or is there a technical reason for that?

SGiux
  • 619
  • 3
  • 10
  • 34

1 Answers1

2

One of the features of WebFlux is its ability to process incoming requests as a stream. This means that the input request's stream could potentially be endless, leading to a possible "out of memory" problem. That's why WebFlux's Decorers and HttpMessageReaders offer a useful feature with their maxInMemorySize properties. They can calculate the size of incoming chunks and trigger a DataBufferLimitException if the accumulated size exceeds the specified limit.

As for Spring MVC, the difference is that MVC usually relies on Servlet containers (like Tomcat, etc.) to process it. Whereas WebFlux relies on Netty and processes that part by themselves. So in MVC, you can limit input request size on the servlet container level, but in WebFlux, you need this property.

The reason for its implementation can be traced back to the original issue, which can be found at: https://github.com/spring-projects/spring-framework/issues/23884

The code is available here: https://github.com/spring-projects/spring-framework/commit/871464811cc1b18d684408f71725cead20c70796

Yevhenii Semenov
  • 1,587
  • 8
  • 18
  • I imagine that mvc will receive a stream of bytes as well. Every data that goes trough a network is streamed. When reading bytes from a channel web mvc could check the amount of bytes it is buffering – SGiux May 20 '23 at 09:44
  • 1
    You are correct that data transmitted over a network (TCP) is streamed. The difference is that MVC usually relies on Servlet containers (like Tomcat, etc.) to process it. Whereas WebFlux relies on Netty and processes that part by themselves. So in MVC, you can limit input request size on the servlet container level, but in WebFlux, you need this property. – Yevhenii Semenov May 20 '23 at 22:25
  • Ok, I imaged this answer. If you complete your initial response with what you said above I can vote it. Thank you. – SGiux May 21 '23 at 14:46
  • @SGiux, Thank you, I have updated my response. I am glad to assist you. – Yevhenii Semenov May 21 '23 at 17:55