Slow initial start-up time in your Java App Engine Application?

Saturday 24 April 2010

A lot of developers have been complaining about slow start-up times of their applications in app-engine production recently on the App Engine issues list. 10-20 second start-up times is not uncommon causing long waits for app users and potential errors in some applications. This is made worse by applications that rely on frameworks that do quite a bit on start-up. With app engine your application doesn't get a permanent JVM instance like you would usualy so this means that if there's been no activity for a while the JVM goes cold so your app will have to start-up again on the next request.

The good news for people suffering with this issue (and that is all App Engine developers I would say) is that the GAE team has accepted the issue and introduced it into their roadmap - "Ability to reserve instances to reduce application loading overhead". Personally I really won't mind paying for my own JVM instance if it's not too expensive, and I would guess that this is probably the direction the GAE team will go.

My "quick-fix" (or hack) for this issue at least until the GAE team sorts it out, is to ping your app every few minutes to keep the JVM "warm". Here I've created a blank servlet that gets called by the AE background cron.xml tasks every few minutes...

web.xml:
...
<servlet>
    <display-name>BlankServlet</display-name>
    <servlet-name>BlankServlet</servlet-name>
    <servlet-class>my.package.BlankServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>BlankServlet</servlet-name>
    <url-pattern>/blank.html</url-pattern>
</servlet-mapping>
...

BlankServlet.java:
public class BlankServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        // Nothing needed here.
    }
}

cron.xml:
<?xml version="1.0" encoding="UTF-8"?>

<cronentries>
    <cron>
        <url>/blank.html</url>
        <description>Ping a blank page to keep the JVM warm</description>
        <schedule>every 5 minutes</schedule>
    </cron>
</cronentries>
You could of course ping your main welcome page instead but you might have some code in your pages that you might not want to be executed by an automatic process - maybe your Google Analytics javascript for example.

2 comments:

Anonymous said...

Cool post. We could solve our warmup related problem within minutes :)

Eurig Jones said...

Actually, after posting this I wouldn't rely in this technique for avoiding the app start-up issue.

After a while your polling will become recognised and it won't be effective. I'm sure you might be able to poll from different sources randomly but It's still going against Google's intentions.

A lot of developers are complaining about it because they're developing new apps which of course, have low traffic :-)

Basically you should remember that this is only an issue on apps with very low traffic. As soon as your app starts gaining traffic then it really isn't an issue.

If you know your app will be low traffic permanently then I suggest attempting to lower your star-tup time by choosing and tweaking your chosen frameworks carefully.