Google App Engine for Java Loading Requests

If you’ve been programming Java on Google App Engine, you’ll encounter them sooner rather than later: the loading request. A loading request to your application is a request that forces an initialization of the environment that runs your application. Depending on your application, the libraries you use, the size and the usage a loading request can take a long long time (over 20 seconds) and happen frequently. It’s something to be aware of. But what to do about them?

Loading requests happen more often when your application isn’t used very frequently. This seems logical: Keep an application in memory only if the chance is high that it will be used again in the very near future. In practice this means an application is swapped out after only a few minutes of inactivity.

If you’re application is fairly new with few users, there’s really no way around it: you have to either reduce the loading time or work around it.

Reducing loading time

Improving the time that loading requests will quickly lead you to this blog post and its sobering conclusion: your best bet for huge gains is to get rid of as many libraries as possible, especially the larger ones.

I haven’t tried it yet, but I fear my fancy architecture based on annotation driven Spring configuration and aspects isn’t going to be the best performer. For now, I don’t want to give up on the great flexibility and clean separation of concern it gives me.

There’s also a trick to avoid loading requests altogether by repeatedly pinging your application. However, according to this thread it is frowned upon and might even result in a ban if you overdo it.

Working around the loading request

So I ended up trying to work around the loading time. For now I’ve placed a static HTML file on another domain, which shows a nice loading message and does a JavaScript redirect to my actual application. It’s ugly but I do happen to have to separate domains for now (a landing page and a beta domain), so I’m ok with it for the time being.

At first I thought you’d need to domains to make this work (one with the static files, one with the Google App Engine application), but it turns out that’s not necessary. Also from the thread mentioned above:

“Google App Engine already serves static resources without intervening requests to application VMs. This means that, for example, you could serve a page that was entirely static content, with a small amount of JS to ping your VM with an asynchronous dynamic request to wake it up. That page would be served instantly to the user. You need to ensure though, that the resources are indeed specified as static content in your app.yaml or appengine-web.xml.”

In fact, all files except JSP and everything in WEB-INF is served statically.

Feel free to share more tips in the comments.

(image credit)