Is it a good idea to use `DeferredResult<ResponseEntity>`

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.

  1. Return type for Blocking Servlet

ResponseEntity<Resource>

  • ResponseEntity<InputStreamResource>
  • ResponseEntity<ByteArrayResource>
  • ResponseEntity<FileSystemResource>
  1. Return type for Async Servlet(Servlet >= 3.0)
  • 2.1)DeferredResult<ResponseEntity<?>>
  • 2.2)ResponseBodyEmitter or ResponseEntity<ResponseBodyEmitter>
  • 2.3)SseEmitter or ResponseEntity<SseEmitter>
  • 2.4)StreamingResponseBody or ResponseEntity<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 of Zero 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, and focus on comparing among these 3 ways(FileSystemResource/StreamingResponseBody/DeferredResult<ResponseEntity<FileSystemResource>>).

3

As others have mentioned in the comments it all depends on the file size. But there’s not only a CPU/resource but also a connection timeout aspect.
What you are essentially doing is putting the connection on hold until the server has prepared the response.

Unless you have some hearbeat mechanism in action (which is not the default for HTTP/TCP), for any other party a connection on hold is indistinguishable from a server failure or connection drop. Indeed if your “blocking” is too long, intermediate gateways may assume the connection dead/timeout and drop it.

In my experience blocking the HTTP response is somewhat acceptable if you can guarantee a delay below 30 seconds. Above 30 seconds things become unreliable. That’s not just annecdotal, but from experience I’ve made while writing this long-poll library.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it a good idea to use `DeferredResult<ResponseEntity>`

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.

  1. Return type for Blocking Servlet

ResponseEntity<Resource>

  • ResponseEntity<InputStreamResource>
  • ResponseEntity<ByteArrayResource>
  • ResponseEntity<FileSystemResource>
  1. Return type for Async Servlet(Servlet >= 3.0)
  • 2.1)DeferredResult<ResponseEntity<?>>
  • 2.2)ResponseBodyEmitter or ResponseEntity<ResponseBodyEmitter>
  • 2.3)SseEmitter or ResponseEntity<SseEmitter>
  • 2.4)StreamingResponseBody or ResponseEntity<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 of Zero 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, and focus on comparing among these 3 ways(FileSystemResource/StreamingResponseBody/DeferredResult<ResponseEntity<FileSystemResource>>).

3

As others have mentioned in the comments it all depends on the file size. But there’s not only a CPU/resource but also a connection timeout aspect.
What you are essentially doing is putting the connection on hold until the server has prepared the response.

Unless you have some hearbeat mechanism in action (which is not the default for HTTP/TCP), for any other party a connection on hold is indistinguishable from a server failure or connection drop. Indeed if your “blocking” is too long, intermediate gateways may assume the connection dead/timeout and drop it.

In my experience blocking the HTTP response is somewhat acceptable if you can guarantee a delay below 30 seconds. Above 30 seconds things become unreliable. That’s not just annecdotal, but from experience I’ve made while writing this long-poll library.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật