Two Languages in One App Engine App

AppEngine_512pxThe other day I was talking to students at a bootcamp about languages. I made the comment that language performance can vary depending on what a particular language is best at doing. When you run into performance issues it can sometimes be helpful to try rewriting pieces of your app in a particular language for a performance boost.

I thought about how that could be done in App Engine. Let’s say I have a section of an application that I wrote in PHP, but it was getting more load than expected, so I need to boost its performance. I want to try and see if Go could give me the boost I need. How hard is that to do?

Please keep in mind all of the caveats here.  Sometimes you can get a boost, sometimes it’s worth exploring. You know, it was a theoretical conversation. And for the record. This need to drop to another language doesn’t have to be performance related. It could be due to SDK or API restrictions, or developer knowledge, or just plain “I want to use another language to do this.”

In App Engine we do this through the use of modules.  Modules allow us to separate front end and back end code from each other.  But they allow us to break up large applications into manageable chunks.  In this case, we’re going to use them to allow us to break up code into multiple languages.

Let’s assume that you have an application with an app.yaml that looks like this:

application: <appengine project name>
module: default
version: 1
api_version: 1
runtime: php55
threadsafe: yes

handlers:
- url: /place
  script: place.php  

- url: /details
  script: details.php   

- url: /distance
  script: distance.php

Let’s say that you want to swap out the distance method for go. The first thing you need to do is write a dispatch.yaml, which looks like this:

application: <appengine project name>

dispatch:
- url: "*/*"
  module: default

This will redirect all calls to your App Engine app to the Php application above. Which is what has been happening to date. But this is a setup step for later.  You then have to add the dispatch file to your application. In a command prompt, from the folder containing dispatch.yaml, run:

appcfg.py --oauth2  update_dispatch .

Write a replacement for your distance method in Go. Go on, we’ll wait…

Ok, assuming you’ve done that you write out an app.yaml for the Go code you wrote:

application:  <appengine project name>
module: goapi
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

Take note of the module name. It has to be different from the original app’s module, which should be “default.”

Once you have all of that handled you need to tweak your dispatch.yaml to replace calls made to the php version of the distance method to the Go method:

application:  <appengine project name>

dispatch:
- url: "*/distance"
  module: goapi

- url: "*/*"
  module: default

Rerun the dispatch update:

appcfg.py --oauth2  update_dispatch .

And there you go, the original PHP service will answer all other calls, but the Go service will answer calls for /distance.

Running multiple language solutions in the same App Engine instance can solve some problems for you.  It also has a few interesting ramifications.  These include the ability to use the same shared Memcached instance between Go and Php. I’m going to show that off in my next blog post.

All code show here is licensed under Apache 2. For more details find the original source on Github.

4 thoughts on “Two Languages in One App Engine App

  1. Hi,

    Interesting article, sadly your Github link is invalid. I tried to make a helloworld app with modules for PHP on GAE but it keeps throwing errors and exceptions… I have read the doc many times but even the actual sequence and format of invocations of appcfg.py seem arcane… does your example (above) still work with the current release of the PHP version of App Engine ?

    If so, could you maybe send a links of zip file with a skeleton version that works ?

    Thanks in advance, 🙂
    Martin

    Like

  2. Martin,

    The current commands still work with the project I used. But they are a little outdated. I’ll revisit this post shortly and update it to more recent CLI calls.

    Like

  3. Morning Terrance,
    I currently have a GO lang website and i am trying to also get a PHP WordPress blog setup to handle just the /blog URLS.
    I first tried creating a new app, got PHP and WP deployed, no problems. But when i came to mapping the domain it wouldnt work as the domain is already mapped to my original APP.
    This is when i considered if you could do them in one GAE app with all the code for both deployed together.
    Based on your experience and knowledge, would this work?

    Like

  4. Mark B. I would split them into 2 modules in App Engine. This will allow you to share the url and have a dispatch file that handles diverting /blog urls to WordPress.

    Like

Leave a comment