File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Image caching with Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Image caching with Tomcat" Watch "Image caching with Tomcat" New topic

Image caching with Tomcat

Tahseen Jamal

Joined: Jan 06, 2011
Posts: 4
Dear All,

I have created a service in which Tomcat is serving the banners images. Now with high traffic, Tomcat's response has become slower.

Is there a way to reduce the load and speed up the things?

parampreet sethi

Joined: Aug 03, 2009
Posts: 27

You can try using multiple tomcats with load balancing approach. The load balancing can be done using storing images based on some algorithm into different tomcat servers. And then use Apache server at front (using workers module) which will connect to multiple tomcat servers (e.g. using mod_proxy ) based on URL regex.

If possible, you can host the banner images (If they are not coming from a backend source) in Apache server. Its faster and ideal for hosting static content.

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Before you go for a Tomcat cluster (which makes the setup more complex and requires additional hardware) you could try to make better use of caching features in browsers and proxy servers.

I've created a Servlet filter some time ago which sets and rewrites HTTP headers depending on the content type of a request. This way you can control which resources should be allowed to be cached by browsers and/or proxy servers. I'm not 100% sure if this is still true for current Tomcat versions but the one I had used in this project didn't set the HTTP headers correctly by default. This COULD already be enough reduce the load on your server. Unfortunately I don't have this code anymore. Basically it's not very complicated to implement but of course IE (at least up to 6.x) requires special settings because it doesn't implement the HTTP protocol correctly

If this is not enough to reduce the load you will most probably have ti use more hardware and servers as Parampreet pointed out.

I hope this will help you. If you have more questions, just feel free to ask.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Are the banner images the only thing Tomcat is serving?

If not, why are you sure that this is the bottleneck?

If so, how big are the images? Small enough to keep in memory to avoid extra trips to disc?

I agree. Here's the link:
subject: Image caching with Tomcat
It's not a secret anymore!