Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



App Engine Modules in Java

App Engine Modules (or just "Modules" hereafter) is a feature that lets developers factor large applications into logical components that can share stateful services and communicate in a secure fashion. An app that handles customer requests might include separate modules to handle other tasks, such as:

  • API requests from mobile devices
  • Internal, admin-like requests
  • Backend processing such as billing pipelines and data analysis

Modules can be configured to use different runtimes and to operate with different performance settings.

  1. Application hierarchy
  2. Instance scaling and class
  3. Configuration
  4. Uploading modules
  5. Instance states
  6. Instance uptime
  7. Background threads
  8. Monitoring resource usage
  9. Logging
  10. Communication between modules
  11. Limits

Application hierarchy

At the highest level, an App Engine application is made up of one or more modules . Each module consists of source code and configuration files. The files used by a module represent a version of the module. When you deploy a module, you always deploy a specific version of the module. For this reason, whenever we speak of a module, it usually means a version of a module.

You can deploy multiple versions of the same module, to account for alternative implementations or progressive upgrades as time goes on.

Every module and version must have a name. A name can contain numbers, letters, and hyphens. It cannot be longer than 63 characters and cannot start or end with a hyphen.

While running, a particular module/version will have one or more instances . Each instance runs its own separate executable. The number of instances running at any time depends on the module's scaling type and the amount of incoming requests:

Hierarchy graph of modules/versions/instances

Stateful services (such as Memcache, Datastore, and Task Queues) are shared by all the modules in an application. Every module, version, and instance has its own unique URI (for example, v1.my-module.my-app.appspot.com ). Incoming user requests are routed to an instance of a particular module/version according to URL addressing conventions and an optional customized dispatch file .

Please note that in April of 2013, Google stopped issuing SSL certificates for double-wildcard domains hosted at appspot.com (i.e. *.*.appspot.com ). If you rely on such URLs for HTTPS access to your application, please change any application logic to use "-dot-" instead of ".". For example, to access version "1" of application "myapp" use "https://1-dot-myapp.appspot.com" instead of "https://1.myapp.appspot.com." If you continue to use "https://1.myapp.appspot.com" the certificate will not match, which will result in an error for any User-Agent that expects the URL and certificate to match exactly.

Instance scaling and class

While an application is running, incoming requests are routed to an existing or new instance of the appropriate module/version. The scaling type of a module/version controls how instances are created. There are three scaling types: manual , basic , and automatic .

Manual Scaling
A module with manual scaling runs continuously, allowing you to perform complex initialization and rely on the state of its memory over time.
Basic Scaling
A module with basic scaling will create an instance when the application receives a request. The instance will be turned down when the app becomes idle. Basic scaling is ideal for work that is intermittent or driven by user activity.
Automatic Scaling
Automatic scaling is the scaling policy that App Engine has used since its inception. It is based on request rate, response latencies, and other application metrics. Previously users could use the Admin Console to configure the automatic scaling parameters (instance class, idle instances and pending latency) for an application's frontend versions only. These settings now apply to every version of every module that has automatic scaling.

Each scaling type offers a selection of instance classes, with different amounts of CPU and Memory. The following tables list the features of the three types of scaling, and the service levels and costs of the various instance classes:

Scaling Types

Feature Automatic Scaling Manual Scaling Basic Scaling
Deadlines 60-second deadline for HTTP requests, 10-minute deadline for tasks Requests can run indefinitely. A manually-scaled instance can choose to handle /_ah/start and execute a program or script for many hours without returning an HTTP response code. Same as manual scaling.
CPU/Memory Configurable by selecting an F1, F2, F4, or F4_1G instance class Configurable by selecting a B1, B2, B4, B4_1G, or B8 instance class Configurable by selecting a B1, B2, B4, B4_1G, or B8 instance class
Residence Instances are evicted from memory based on usage patterns. Instances remain in memory, and state is preserved across requests. When instances are restarted, an /_ah/stop request appears in the logs. If there is a registered stop callback method, it has 30 seconds to complete before shutdown occurs. Instances are evicted based on the idle_timeout parameter. If an instance has been idle, i.e. has not received a request, for more than idle_timeout , then the instance is evicted.
Startup and Shutdown Instances are created on demand to handle requests and automatically turned down when idle. Instances are sent a start request automatically by App Engine in the form of an empty GET request to /_ah/start . An instance that is stopped with appcfg stop (or via the Admin Console UI) has 30 seconds to finish handling requests before it is forcibly terminated. Instances are created on demand to handle requests and automatically turned down when idle, based on the idle_timeout configuration parameter. As with manual scaling, an instance that is stopped with appcfg stop (or via the Admin Console UI) has 30 seconds to finish handling requests before it is forcibly terminated.
Instance Addressability Instances are anonymous. Instances are addressable at URLs with the form: http://instance.version.module.app_id.appspot.com . If you have set up a wildcard subdomain mapping for a custom domain, you can also address a module or any of its instances via a URL of the form http://module.domain.com or http://instance.module.domain.com . You can reliably cache state in each instance and retrieve it in subsequent requests. Same as manual scaling.
Scaling App Engine scales the number of instances automatically in response to processing volume. This scaling factors in the automatic_scaling settings that are provided on a per-version basis in the configuration file uploaded with the module version. You configure the number of instances of each module version in that module’s configuration file. The number of instances usually corresponds to the size of a dataset being held in memory or the desired throughput for offline work. A basic scaling module version is configured with a maximum number of instances using the basic_scaling setting's max_instances parameter. The number of live instances scales with the processing volume.
Free Daily Usage Quota 28 instance-hours 8 instance-hours 8 instance-hours

Instance classes

Instances are priced based on an hourly rate determined by the instance class.

Instance Class Memory Limit CPU Limit Cost per Hour per Instance
B1 128 MB 600 Mhz $0.05
B2 256 MB 1.2 Ghz $0.10
B4 512 MB 2.4 Ghz $0.20
B4_1G 1024 MB 2.4 Ghz $0.30
B8 1024 MB 4.8 Ghz $0.40
F1 128 MB 600 Mhz $0.05
F2 256 MB 1.2 Ghz $0.10
F4 512 MB 2.4 Ghz $0.20
F4_1G 1024 MB 2.4 Ghz $0.30

Manual and basic scaling instances are billed at hourly rates based on uptime. Billing begins when an instance starts and ends fifteen minutes after a manual instance shuts down or fifteen minutes after a basic instance has finished processing its last request. Runtime overhead is counted against the instance memory limit. This will be higher for Java than for other languages.

Important: When you are billed for instance hours, you will not see any instance classes in your billing line items. Instead, you will see the appropriate multiple of instance hours. For example, if you use an F4 instance for one hour, you do not see "F4" listed, but you will see billing for four instance hours at the F1 rate.

Configuration

An App Engine application that uses modules is organized as an unpacked Java Enterprise Archive (EAR) directory structure. The top-level EAR directory contains a single META-INF subdirectory, and a separate directory for each module in the app. These module directories are organized as unpacked Java Web Application Archives (WAR). Each WAR directory usually has the same name as the module it defines, but this is not required. Although Java EE supports WAR files, module configuration uses unpacked WAR directories only. App Engine's Java SDK includes an Apache Maven tool that can build a skeletal EAR structure for you.

Hierarchy graph of EAR directories

The META-INF directory has two configuration files: appengine-application.xml and application.xml . The appengine-application.xml file contains general information used by App Engine tools when your app is deployed. The application.xml file declares the list of modules and their WAR directories that comprise the application. The WAR directory for each module contains two configuration files: appengine-web.xml , and web.xml . General information on these files can be found in the Java Getting Started tutorial.

The WAR directory file appengine-web.xml defines the configuration of modules. Each module's appengine-web.xml file defines the scaling type and instance class for a specific module/version. Different scaling parameters are used depending on which type of scaling you specify. If you do not specify scaling, automatic scaling is the default.

For each module you can also specify settings that map URL requests to specific Java servlets and identify static files for better server efficiency. These settings are included in the web.xml and appengine-web.xml files and are described in the Deployment Descriptor and App Config sections. The following examples show how to configure modules for each scaling type.

Note that while every appengine-web.xml file must contain the <application> tag, the name you supply there is ignored. The name of the application is taken from the <application> tag in the appengine-application.xml file.

Manual Scaling

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>simple-app</application>
  <module>default</module>
  <version>uno</version>
  <threadsafe>true</threadsafe>
  <instance-class>B8</instance-class>
  <manual-scaling>
    <instances>5</instances>
  </manual-scaling>
</appengine-web-app>
instance-class:
The instance class size for this module. When using manual scaling, the B1, B2, B4, B4_1G, and B8 instance classes are available. If you do not specify a class, B2 is assigned by default.
manual-scaling:
Required to enable manual scaling for a module.
instances:
The number of instances to assign to the module at the start. This number can later be altered by using the Modules API setNumInstances() function.

Basic Scaling

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>simple-app</application>
  <module>default</module>
  <version>uno</version>
  <threadsafe>true</threadsafe>
  <instance-class>B8</instance-class>
  <basic-scaling>
    <max-instances>11</max-instances>
    <idle-timeout>10m</idle-timeout>
  </basic-scaling>
</appengine-web-app>
instance-class:
The instance class size for this module. When using basic scaling, the B1, B2, B4, B4_1G, and B8 instance classes are available. If you do not specify a class, B2 is assigned by default.
basic-scaling:
Required to enable basic scaling for a module.
max-instances:
Required. The maximum number of instances for App Engine to create for this module version. This is useful to limit the costs of a module.
idle-timeout:
Optional. The instance will be shut down this amount of time after receiving its last request.
Note: Basic scaling is not currently available in the Java Development Server.

Automatic Scaling

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>simple-app</application>
  <module>default</module>
  <version>uno</version>
  <threadsafe>true</threadsafe>
  <instance-class>F2</instance-class>
  <automatic-scaling>
    <min-idle-instances>5</min-idle-instances>
    <!-- ‘automatic’ is the default value. -->
    <max-idle-instances>automatic</max-idle-instances>
    <!-- ‘automatic’ is the default value. -->
    <min-pending-latency>automatic</min-pending-latency>
    <max-pending-latency>30ms</max-pending-latency>
    <max-concurrent-requests>50</max-concurrent-requests>
  </automatic-scaling>
</appengine-web-app>
instance-class:
The Instance Class size for this module. When using automatic scaling, only the F1, F2, F4, and F4_1G instance classes are available. If you do not specify a class, F1 is assigned by default.
automatic-scaling:
Optional. Automatic scaling is assumed by default.
min-idle-instances:
The minimum number of idle instances that App Engine should maintain for this version. Only applies to the default version of a module, since other versions are not expected to receive significant traffic. Please keep in mind:
  • A low minimum helps keep your running costs down during idle periods, but means that fewer instances may be immediately available to respond to a sudden load spike.
  • A high minimum allows you to prime the application for rapid spikes in request load. App Engine keeps that number of instances in reserve at all times, so an instance is always available to serve an incoming request, but you pay for those instances. This functionality replaces the deprecated "Always On" feature, which ensured that a fixed number of instances were always available for your application. Once you've set the minimum number of idle instances, you can see these instances marked as "Resident" in the Instances tab of the Admin Console.

    If you set a minimum number of idle instances, pending latency will have less effect on your application's performance. Because App Engine keeps idle instances in reserve, it is unlikely that requests will enter the pending queue except in exceptionally high load spikes. You will need to test your application and expected traffic volume to determine the ideal number of instances to keep in reserve.

max-idle-instances:
The maximum number of idle instances that App Engine should maintain for this version. Please keep in mind:
  • A high maximum reduces the number of idle instances more gradually when load levels return to normal after a spike. This helps your application maintain steady performance through fluctuations in request load, but also raises the number of idle instances (and consequent running costs) during such periods of heavy load.
  • A low maximum keeps running costs lower, but can degrade performance in the face of volatile load levels.

Note: When settling back to normal levels after a load spike, the number of idle instances may temporarily exceed your specified maximum. However, you will not be charged for more instances than the maximum number you've specified.

min-pending-latency:
The minimum amount of time that App Engine should allow a request to wait in the pending queue before starting a new instance to handle it.
  • A low minimum means requests must spend less time in the pending queue when all existing instances are active. This improves performance but increases the cost of running your application.
  • A high minimum means requests will remain pending longer if all existing instances are active. This lowers running costs but increases the time users must wait for their requests to be served.
max-pending-latency:
The maximum amount of time that App Engine should allow a request to wait in the pending queue before starting a new instance to handle it.
  • A low maximum means App Engine will start new instances sooner for pending requests, improving performance but raising running costs.
  • A high maximum means users may wait longer for their requests to be served (if there are pending requests and no idle instances to serve them), but your application will cost less to run.
max-concurrent-requests:
Optional. The number of concurrent requests an automatic scaling instance can accept before the scheduler spawns a new instance (Default: 10, Maximum: 100). Note that the scheduler may spawn a new instance before the actual maximum number of requests is reached.

The default module

Every application must have a single default module. To define the default module, include the setting <module>default</module> in the module's appengine-web.xml file, or leave the setting out.

An example

Here is an example of how you would configure the various files in an EAR directory structure for an application that has two modules: a default module that handles web requests, plus another module (named my-module ) for backend processing.

Assuming that the top-level EAR directory is "my-application," define the file my-application/META-INF/appengine-application.xml :

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-application xmlns="http://appengine.google.com/ns/1.0">
  <application>my-application</application>
</appengine-application>

Create WAR directories for the two modules: my-application/default and my-application/my-module .

Now create an appengine-web.xml file in each WAR that specifies the parameters for the module. The file must include a version name for the module. To define the default module, you can explicitly include the <module>default</module> parameter or leave it out of the file. Here is the file my-application/default/WEB-INF/appengine-web.xml that defines the default module:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>my-application</application>
  <module>default</module>
  <version>uno</version>
  <threadsafe>true</threadsafe>
</appengine-web-app>

The file my-application/my-module/WEB-INF/appengine-web.xml defines the module that will handle background requests:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>my-application</application>
  <module>my-module</module>
  <version>uno</version>
  <threadsafe>true</threadsafe>
  <manual-scaling>
    <instances>5</instances>
  </manual-scaling>
</appengine-web-app>

Finally, define the file my-application/META-INF/application.xml that enumerates the modules. Note that the default module should be the first module listed.

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

<application
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/application_5.xsd"
  version="5">

  <description>GAE Java SuperFun app</description>
  <display-name>SuperFun</display-name>

  <!-- Modules -->
  <!-- The default module should be listed first -->
  <module>
    <web>
      <web-uri>default</web-uri>
      <context-root>default</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>my-module</web-uri>
      <context-root>my-module</context-root>
    </web>
  </module>

</application>

App Engine will ignore the <context-root> elements, so HTTP clients need not prepend it to the URL path when addressing a module.

Uploading modules

To deploy the example above, use the appcfg update command, passing the directory path of the EAR:

appcfg update <directory-path>

To only upload a specific module, give the directory path for its WAR:

appcfg.sh <directory-path>/my-application/my-module

If you are uploading the app for the first time, you must either pass the EAR path, or upload the default module first.

You will receive verification via the command line as each module is successfully deployed. Once the application has been deployed you can access it at http://my-application.appspot.com .

Similarly, you can access each of the modules individually:

  • http://default.my-application.appspot.com
  • http://my-module.my-application.appspot.com

If you run multiple versions of a module, you can access a specific version by prepending the version name to the URI. For example, http://uno.default.my-application.appspot.com will target version uno of the default module.

Instance states

A manual or basic scaled instance can be in one of two states: Running or Stopped . All instances of a particular module/version share the same state. You can change the state of all the instances belonging to a module/version using the appcfg command or the Modules API.

Startup

Each module instance is created in response to a start request, which is an empty GET request to /_ah/start . App Engine sends this request to bring an instance into existence; users cannot send a request to /_ah/start . Manual and basic scaling instances must respond to the start request before they can handle another request. The start request can be used for two purposes:

  • To start a program that runs indefinitely, without accepting further requests
  • To initialize an instance before it receives additional traffic

Manual scaling instances and basic scaling instances startup differently. When you start a manual scaling instance, App Engine immediately sends a /_ah/start request to each instance. When you start an instance of a basic scaling module, App Engine allows it to accept traffic, but the /_ah/start request is not sent to an instance until it receives its first user request. Multiple basic scaling instances are only started as necessary, in order to handle increased traffic.

When an instance responds to the /_ah/start request with an HTTP status code of 200–299 or 404 , it is considered to have successfully started and can handle additional requests. Otherwise, App Engine terminates the instance. Manual scaling instances are restarted immediately, while basic scaling instances are restarted only when needed for serving traffic.

Shutdown

The shutdown process may be triggered by a variety of planned and unplanned events, such as:

  • You manually stop an instance using the appcfg stop command or the Modules API stopVersion function call.
  • Manually stop an instance from the Admin Console Versions page.
  • You update the module version using appcfg update .
  • The instance exceeds the maximum memory for its configured instance_class .
  • Your application runs out of Instance Hours quota.
  • The machine running the instance is restarted, forcing your instance to move to a different machine.
  • App Engine needs to move your instance to a different machine to improve load distribution.

The following code sample demonstrates a basic shutdown hook:

LifecycleManager.getInstance().setShutdownHook(new ShutdownHook() {
  public void shutdown() {
    LifecycleManager.getInstance().interruptAllRequests();
  }
});

Alternatively, the following sample demonstrates how to use the isShuttingDown() method:

while (haveMoreWork()
&& !LifecycleManager.getInstance().isShuttingDown()) {
  doSomeWork();
  saveState();
}

Instance uptime

App Engine attempts to keep manual and basic scaling instances running indefinitely. However, at this time there is no guaranteed uptime for manual and basic scaling instances. Hardware and software failures that cause early termination or frequent restarts can occur without prior warning and may take considerable time to resolve; thus, you should construct your application in a way that tolerates these failures. The App Engine team will provide more guidance on expected instance uptime as statistics become available.

Here are some good strategies for avoiding downtime due to instance restarts:

  • Use load balancing across multiple instances.
  • Configure more instances than are normally required to handle your traffic patterns.
  • Write fall-back logic that uses cached results when a manual scaling instance is unavailable.
  • Reduce the amount of time it takes for your instances to start up and shutdown.
  • Duplicate the state information across more than one instance.
  • For long-running computations, checkpoint the state from time to time so you can resume it if it doesn't complete.

It's also important to recognize that the shutdown hook is not always able to run before an instance terminates. In rare cases, an outage can occur that prevents App Engine from providing 30 seconds of shutdown time. Thus, we recommend periodically checkpointing the state of your instance and using it primarily as an in-memory cache rather than a reliable data store.

Background threads

Code running on a manual scaling instance can start a background thread that may outlive the request that spawns it. This allows instances to perform arbitrary periodic or scheduled tasks or to continue working in the background after a request has returned to the user.

A background thread's logging entries are independent of those of the spawning thread. You can read more about background threads in App Engine's ThreadManager documentation.

import com.google.appengine.api.ThreadManager;
import java.util.concurrent.AtomicLong;

AtomicLong counter = new AtomicLong();

Thread thread = ThreadManager.createBackgroundThread(new Runnable() {
  public void run() {
    try {
      while (true) {
        counter.incrementAndGet();
        Thread.sleep(10);
      }
    } catch (InterruptedException ex) {
      throw new RuntimeException("Interrupted in loop:", ex);
    }
  }
});
thread.start();

If code running in an automatic scaling module attempts to start a background thread, it raises an exception.

Monitoring resource usage

The Instances Console section of the Administration Console provides visibility into how instances are performing. By selecting your module and version in the dropdowns, you can see the memory and CPU usage of each instance, uptime, number of requests, and other statistics. You can also manually initiate the shutdown process for any instance.

You also can use the Runtime API to access statistics showing the CPU and memory usage of your instances. These statistics help you understand how resource usage responds to requests or work performed, and also how to regulate the amount of data stored in memory in order to stay below the memory limit of your instance class.

Logging

You can use the Logs API to access your app's request and application logs. In particular, the fetch() function allows you to retrieve logs using various filters, such as request ID, timestamp, module ID, and version ID.

Application (user/app-generated) logs are periodically flushed while manual and basic scaling instances handle requests; since modules can run on a request a long time, logs may not flush for a while. You can tune the flush settings, or force an immediate flush, using the Logs API. When a flush occurs, a new log entry is created at the time of the flush, containing any log messages that have not yet been flushed. These entries show up in the Logs Console marked with flush , and include the start time of the request that generated the flush.

Communication between modules

Modules can share state by using the Datastore and Memcache. They can collaborate by assigning work between them using Task Queues. To access these shared services, use the corresponding App Engine APIs. Calls to these APIs are automatically mapped to the application’s namespace.

The Modules API provides functions to retrieve the address of a module, a version, or an instance. This allows an application to send requests from one module, version, or instance to another module, version, or instance. This works in both the development and production environments. The Modules API also provides functions that return information about the current operating environment (module, version, and instance).

The following code sample shows how to get the module name and instance id for a request:

import com.google.appengine.api.modules.ModulesService;
import com.google.appengine.api.modules.ModulesServiceFactory;

ModulesService modulesApi = ModulesServiceFactory.getModulesService();

// Get the module name handling the current request.
String currentModuleName = modulesApi.getCurrentModule();
// Get the instance handling the current request.
int currentInstance = modulesApi.getCurrentInstance();

The instance ID of an automatic scaled module will be returned as a unique base64 encoded value, e.g. e4b565394caa .

You can communicate between modules in the same app by fetching the hostname of the target module:

import com.google.appengine.api.modules.ModulesService;
import com.google.appengine.api.modules.ModulesServiceFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

// ...

ModulesService modulesApi = ModulesServiceFactory.getModulesService();

// ...
    try {
        URL url = new URL("http://" +
            modulesApi.getVersionHostname("my-backend-module","v1") +
            "/fetch-stats");
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(url.openStream()));
        String line;

        while ((line = reader.readLine()) != null) {
            // Do something...
        }
        reader.close();

    } catch (MalformedURLException e) {
        // ...
    } catch (IOException e) {
        // ...
    }

You can also use the URL Fetch service.

To be safe, the receiving module should validate that the request is coming from a valid client. You can check that the Inbound-AppId header or user-agent-string matches the app-id fetched with the AppIdentity service. Or you can use OAuth to authenticate the request.

You can configure any manual or basic scaling module to accept requests from other modules in your app by adding the "<login>admin</login>" specification to the module's handler. In that case any URLFetch from any other module in the app will be automatically authenticated by App Engine, and any URLFetch call from a module that is not part of the application will be rejected.

If you want a module to receive requests from anywhere, you must code your own secure solution as you would for any App Engine application. This is usually done by implementing a custom API and authentication mechanism.

Limits

The maximum number of modules and versions that you can deploy depends on your app's pricing:

Limit Free App Paid App Premier App
Maximum Modules Per App 5 20 20
Maximum Versions Per App 15 60 60

There is also a limit to the number of instances for each module with basic or manual scaling:

Maximum Instances per Manual/Basic Scaling Version
Free App US App (Paid/Premier) EU App (Paid/Premier)
20 200 25

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.