Configuring Warmup Requests to Improve Performance

You can use warmup requests to reduce request and response latency during the time when your app's code is being loaded to a newly created instance.

App Engine frequently needs to load your app's code into a fresh instance. Loading an instance can happen in the following situations:

  • When you redeploy a version of your app.
  • When new instances are created due to the load from requests exceeding the capacity of the current set of running instances.
  • When maintenance and repairs of the underlying infrastructure or physical hardware occur.

Loading your app's code to a new instance can result in loading requests. Loading requests can result in increased request latency for your users, but you can avoid this latency using warmup requests. Warmup requests load your app's code into a new instance before any live requests reach that instance.

If warmup requests are enabled for your application, App Engine attempts to detect when your application needs a new instance and initiates a warmup request to initialize a new instance. However, these detection attempts do not work in every case. As a result, you might encounter loading requests, even if warmup requests are enabled in your app. For example, if your app is serving no traffic, the first request to the app will always be a loading request, not a warmup request.

Warmup requests use instance hours like any other request to your App Engine application. In most cases where warmup requests are enabled, you won't notice an increase in instance hours because your application is simply initializing in a warmup request instead of a loading request. Your instance hour usage can increase if you decide to do more work, such as pre-caching during a warmup request. If you set min_idle_instances to greater than 0, you might encounter warmup requests when those instances first start, but they will remain available after that time.

Enabling warmup requests

Warmup requests are used by the App Engine scheduler, which controls the auto scaling of instances based on user-supplied configuration. With warmup requests enabled, App Engine issues GET requests to /_ah/warmup. You can implement handlers for this request to perform application-specific tasks, such as pre-caching application data.

The scheduler starts up instances when it determines that more instances are needed. Warmup requests may appear in logs even if they are disabled because the scheduler uses them to start instances.

Note that warmup requests are not guaranteed to be called. In some situations loading requests are sent instead: for example, if the instance is the first one being started up, or if there is a steep ramp-up in traffic. However, there will be a "best effort" attempt to send requests to already warmed-up instances if warmup requests are enabled.

To enable warmup requests, add the warmup element under the inbound_services directive in your app.yaml file, for example:

inbound_services:
- warmup

Creating your handler

Create a handler that will process the requests that are sent to /_ah/warmup. Your handler should perform any warmup logic that is needed by your app.


// Sample warmup demonstrates usage of the /_ah/warmup handler.
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"cloud.google.com/go/storage"
)

var startupTime time.Time
var client *storage.Client

func main() {
	// Perform required setup steps for the application to function.
	// This assumes any returned error requires a new instance to be created.
	if err := setup(context.Background()); err != nil {
		log.Fatalf("setup: %v", err)
	}

	// Log when an appengine warmup request is used to create the new instance.
	// Warmup steps are taken in setup for consistency with "cold start" instances.
	http.HandleFunc("/_ah/warmup", func(w http.ResponseWriter, r *http.Request) {
		log.Println("warmup done")
	})
	http.HandleFunc("/", indexHandler)

	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
		log.Printf("Defaulting to port %s", port)
	}

	log.Printf("Listening on port %s", port)
	if err := http.ListenAndServe(":"+port, nil); err != nil {
		log.Fatal(err)
	}
}

// setup executes per-instance one-time warmup and initialization actions.
func setup(ctx context.Context) error {
	// Store the startup time of the server.
	startupTime = time.Now()

	// Initialize a Google Cloud Storage client.
	var err error
	if client, err = storage.NewClient(ctx); err != nil {
		return err
	}

	return nil
}

// indexHandler responds to requests with our greeting.
func indexHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
	uptime := time.Since(startupTime).Seconds()
	fmt.Fprintf(w, "Hello, World! Uptime: %.2fs\n", uptime)
}