Fix: Tomcat taking very long to serve first request on fresh install on Ubuntu 14.04

Yesterday, I was trying to setup Apache Tomcat 7 on Ubuntu 14.04 on a new DigitalOcean droplet. But it was taking VERY long (10-15+ minutes) to serve first request. After that, it started serving requests normally. If the Tomcat was restarted, it again took very long to serve the first request.

I tried large droplets with more memory. Also, I tried installing Tomcat from both the Ubuntu repository package and from official Tomcat website. But in every case, I faced the same problem.

Turns out, the solution is pretty simple. We just need to install a package named  haveged like this:

Now restart Tomcat and you will see that it starts serving requests immediately at normal speed.

Basically what happens is that the entropy pools run dry on our virtual server running in the cloud. As a result, Tomcat blocks on /dev/random waiting for random numbers to be generated. Haveged seeds our system’s random source (usually /dev/random) using differences in our processor’s time stamp counter (TSC) after executing a loop repeatedly. Please refer to this article on DigitalOcean for details of this process.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: