I’ve went through all the answers for this question
download a file from Spring boot rest service, realizing that the differences are related to the return value. Basically, there are two type families.
- Return type for Blocking Servlet
ResponseEntity<Resource>
ResponseEntity<InputStreamResource>
ResponseEntity<ByteArrayResource>
ResponseEntity<FileSystemResource>
- Return type for Async Servlet(Servlet >= 3.0)
- 2.1)
DeferredResult<ResponseEntity<?>>
- 2.2)
ResponseBodyEmitter
orResponseEntity<ResponseBodyEmitter>
- 2.3)
SseEmitter
orResponseEntity<SseEmitter>
- 2.4)
StreamingResponseBody
orResponseEntity<StreamingResponseBody>
Someone mentions that, for downloading big files, it might go OOM if we use ByteArrayResource
.
IIUC, either FileSystemResource
or StreamingResponseBody
is friendly with big files, with http chunked encoding
under the hood.
FileSystemResource
servlet container thread is blocked but can take advantage ofZero Copy
. More efficient for memory.StreamingResponseBody
servlet container thread is released. More efficient for CPU.
If that’s true, will it be the best choice to use DeferredResult<ResponseEntity<FileSystemResource>>
instead, since it’s more efficient for both memory and CPU?
P.S. Let’s ignore CDN and some other tech like BitTorrent for downloading big files in real world.