Reliable data serving
How can i make sure my file serving is reliable and scalable? How many parallel request it can handle?
I am thinking beyond the hardware capability and band width.
i am following Streaming large files in a java servlet
Solution 1:
If those are static files, just link to it directly. All decent servletcontainers/appservers have a well-developed DefaultServlet
. If those are static files located outside the webapplication from where you'd link them to, then you can also just add the root folder of those files as another context. It's unclear which server you're using, but if it were Tomcat, you could just add a new <Context>
to server.xml
:
<Context docBase="/path/to/static/files" path="/files" />
This way it's accessible by http://example.com/files/...
.
If those are dynamically generated files or files coming from a database, then you need to develop a servlet which does the IO job efficiently: i.e. do not unnecessarily store the entire data in memory (e.g. in a ByteArrayInputStream
or byte[]
before emitting them to the output. Just write the bytes immediately to the output as it comes in. You may find this those examples of a basic fileservlet and a more advanced fileservlet (supporting resumes and so on) useful.