Friday, June 18, 2010

How I live with cold starts on GAE's JVM

It has come to my attention the (obvious and logical) fact that loading the JVM is expensive, time-wise. And Google's Application Engine (GAE) makes no exception, so whenever one servlet is "hit" long after any previous server interaction, a JVM is started. In practice, this means that we're looking at a "loading..." spinning wheel with a duration of about 10s, +-5s. That's kind of a bother, but that's not the worse. If we're talking about a single-page modeled web app, the user is bound to leave it opened for some time (minutes, not to say hours, before he is able to gather all the info to fill in that devilish form!). And after that time, when he is eager to finish up his task (that has already took him enough time outside the browser window), guess what? That's right, wait another 10s for the server's JVM to wake up. Off course this is a non-issue for heavy-traffic web apps, but this platform is great for experiments and small business products, and it's this niche I'm worried with (perhaps because I'm also targeting it :) )

Today's scenario is yet a bit worse. The time it takes for a GAE JVM to get drowsy is often shorter than 5 minutes (here are some conclusions about timings, as well as other suggestions to alleviate the problem - but insufficient, still). I could only imagine Google is having a hard time escalating their servers to everyone, but they should get a solution to this. Otherwise people will start hacking this problem subversively. In fact, they're already doing it: exploits are there to make a repeated task that keeps the JVM in question alive - a ping, let's call it that! But as soon as this hack becomes mainstream and widespread, Google will hog down with requests. So I'm sort of against it. But since there is no viable solution so far, I started reconsidering things.

What I ended up was a compromise. For applications like the ones I'm developing there will be only 3 or 4 users, and their request will be very scarce and scattered. So I will not use a frequent and constant ping bot. Instead, I made a client-side that does that pinging, but only while the application is opened. So yes, the user has to wait some seconds to fire up the application for the first time. But afterwards, a background (i.e., asynchronous) periodic request keeps the JVM ready to listen for the application's demands.

Here's a possible Javascript code to make this happen. The trick here is to create a script dynamically (i.e., appending it to the body), and setting its source to any servlet's served URL (in this case, a ping one).

blog comments powered by Disqus