How to async stream big file through Spring MVC from external source?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If your Spring MVC endpoint proxies a large file from another service, the main goal is to avoid buffering the whole file in memory. The standard Spring MVC solution is to return a StreamingResponseBody, read from the upstream source in chunks, and write those bytes directly to the client response.
Why StreamingResponseBody Fits This Problem
StreamingResponseBody lets Spring handle the response asynchronously while your code writes directly to the output stream. That is a better fit than building a byte[] or Resource for very large responses, because the bytes can flow through the server incrementally.
This is especially useful when the file comes from an external HTTP source rather than local disk.
A Basic Proxy Controller
The example below uses Java’s HttpClient to fetch the upstream file as an InputStream, then streams it out through Spring MVC.
The key idea is that the controller never loads the whole file into a byte array.
Configure Async Execution Explicitly
Spring recommends configuring the async executor used for streaming responses. If you leave it to defaults, a busy system can behave unpredictably under load.
That gives streaming requests a predictable thread pool instead of relying on whatever default happens to be present.
Propagate Useful Response Headers
In a real proxy endpoint, you may also want to copy upstream headers such as content type, content length, or Content-Disposition. That improves the client experience and avoids guessing the file metadata.
Be selective. Forwarding every upstream header blindly is rarely a good idea.
Backpressure and Failure Behavior
Async streaming does not mean zero blocking. If the client reads slowly, the servlet output stream still determines how fast data can be written. The win is that you avoid materializing the whole response in memory and keep the controller model aligned with long-running transfers.
You should also decide what to do if the upstream request fails partway through. In many cases, the correct answer is to log the error and let the client receive a truncated or failed download rather than pretending the response completed successfully.
Common Pitfalls
- Reading the full external file into memory defeats the purpose of streaming.
- Forgetting to configure the async executor can cause poor behavior under load.
- Ignoring upstream response status codes leads to broken downloads wrapped in
200 OKresponses. - Copying every upstream header blindly can create incorrect downstream behavior.
- Assuming async streaming removes all I/O blocking is unrealistic. It mainly improves memory usage and request handling structure.
Summary
- Use
StreamingResponseBodyto proxy large files through Spring MVC without buffering them fully. - Read from the upstream source as a stream and write directly to the response output stream.
- Configure Spring MVC async execution explicitly for production workloads.
- Forward only the headers that matter to the client.
- Treat streaming as a memory-efficient transfer pattern, not as magic non-blocking I/O everywhere.
Related reading
- How to asynchronously call a method in Java
- How to asynchronously call a method in Java
- How to asynchronously force a file using AsynchronousFileChannel
- How to asynchronously iterate an ObservableCollection containing hierarchical elements?
- How to attach javadoc or sources to jars in libs folder?
- How to automatically scale up and scale down of micro services instances built using Spring Boot and Spring cloud?
- How to asynchronously load a google map in AngularJS?
- How to asynchronously run Matplolib server-side with a timeout? The process hangs randomly

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.