Google App Engine as a Super Aggressive Cache Service for Grails Apps.

I’ve been thinking a bit about how to use the Google App Engine in a Grails deployment infrastructure. In the following post, I want to sketch out how to use the Google App Engine as an aggressive cache mechanism for Grails applications to reduce the cost of Grails experiments.

Let’s look at some of the pros and cos of using the Google App Engine for Grails Application Development.

Good:
Lots of memory, memcache, bandwidth and disk space.
Bad: No threads, no hibernate, no full JRE, limited GORM, can’t write to file. Many plugins don’t work.

A possible way around these problems, of course, is to get a low cost hosting provider and use the Google App Engine to amplify the available bandwidth and processing power available to your web applications. Instead of thinking of the App Engine as another complete deployment platform, this just becomes a loudspeaker to help scale your tiny little app.

Here is an idea of how something like this might look like:

By The Power of App Engine, I am a Grails App!

Building the caching part of this is not terribly difficult, Google App Engine already provides us with all the pieces we need via the MemCache Service and the URLFetch Service.

I would imagine that a typical request handler would look something like this in Groovy:

Set up your Cache

import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

 Cache cache;

// expires cache in one hour
Map props = new Map;
props.put(GCacheFactory.EXPIRATION_DELTA, 3600);

try {

CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();

cache = cacheFactory.createCache(props);

} catch (CacheException e) {

// ...

}

Handle Requests

Simple Sketch of a Cache Service

processRequest(){

    // get cache ...

   def key = buildRemoteGrailsAppRequest( params )
   def page

   if( cache.containsKey( key ) ){
       page = (String) cache.get(key)}
   } else {
       page = new URL( key ).getText()
       cache.put( key, page )
   }
   render page
}

In the above code, we basically just issue a call to an externally hosted Grails server unconstrained by the file and thread restrictions of the app engine. This allows to quickly take an existing Grails application and scale it up using the App Engine. It offloads much of the page serving to the App Engine.

You could also set up  a more intelligent caching mechanism for your application that expires your caches based on a write. I would simply set this up as a onUpdate or onSave Hibernate Events listener on your externally hosted Grails application.

Given that you get ten application instances for each Google App Engine account, it becomes very viable to also offload processing intensive operations into services that are hosted on the App Engine. Say you have an App that does a lot of image manipulation and resizing. You could simply write a few simple services on the App Engine that use the supplied Image Manipulation API and store these into cache.

It helps conserve your server resources ( expensive ) and offload them onto the App Engine ( cheap ).

The App Engine affords a really neat service oriented infrastructure for Grails developers because we no longer need to consider bandwidth and server resources as an issue. By front-ending our bandwidth-intensive apps with an App-Engine Service and offloading computationally expensive operations into it, we can provide highly scalable, powerful applications at a low cost.

Of course, it requires a little bit more planning and engineering, but this type of infrastructure allows us to keep all the productivity gains of Grails without having to deal with the constrained App-Engine development environment.

What do you guys think?

3 thoughts on “Google App Engine as a Super Aggressive Cache Service for Grails Apps.

  1. Marcel Overdijk

    Very interesting thoughts Tomas.

    I’m developing a personal application and I considered GAE for it. But because of the non-relational datastore this was a no-go for me.

    Now building on your idea I could use an external (Grails or even something else) application to store data in a relational way.
    I won’t have that many writes but need to do some smart grouping/computation queries.
    Off course these computations would be cached. So not for every request I need to query the database.
    So the GAE app would just query these computations once an hour.

    Again, a very intersting thought, and I will think about it.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s