Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 45 ยท Compute

App Engine

Learn Google App Engine: standard and flexible environments, app.yaml, services and versions, and traffic splitting, with a runnable splitting stand-in.

App Engine is Google Cloud's oldest platform service. You hand it your code and a small configuration file, and it runs the app, scales it with traffic and handles load balancing and TLS for you. You never manage a VM. It is a good fit for web apps and APIs that follow the request-and-response pattern.

Today many new projects choose Cloud Run instead, because containers are more portable. App Engine is still worth knowing: it is widely deployed, its model of services, versions and traffic splitting is clear, and the ideas carry over to other platforms.

Standard and flexible environments

App Engine has two environments. They differ in how your code is run:

Standard or flexible?

Standard Sandboxed runtimes. Starts fast, can scale to zero.
Flexible Runs in containers on VMs. More freedom, slower to start.

Standard is cheaper for spiky traffic; flexible fits custom needs.

1 / 2

Standard runs your code in a managed sandbox for a set of supported language runtimes. Instances start quickly and the app can scale down to zero instances when nobody is using it. Flexible runs your app in a Docker container on Compute Engine VMs. You get arbitrary libraries and system packages, but instances take longer to start and at least one stays running, so you pay while idle. Which languages and versions are available changes over time, so look at the current runtime list before you start.

The app.yaml file

Each service is described by an app.yaml file next to your code. The smallest one for a standard Node.js app looks like this (read-only; a runtime version must be one that App Engine currently supports):

YAML
runtime: nodejs22

instance_class: F1

automatic_scaling:
  min_instances: 0
  max_instances: 5
  target_cpu_utilization: 0.65

env_variables:
  APP_MODE: production

handlers:
  - url: /static
    static_dir: static

The important parts are the runtime, how instances scale, environment variables and optional static file handlers. Your app listens on the port named in the PORT environment variable, as on Cloud Run. Secrets do not belong in env_variables, because the file is usually committed. Use Secret Manager.

Deploying is a single command from the folder that holds app.yaml:

Terminal
gcloud app create --region=us-central
gcloud app deploy
gcloud app browse

gcloud app create happens once per project and fixes the app's region permanently, so choose it with care (see Regions, Zones and Latency).

Services and versions

An App Engine application is made of services. A project has a service called default and may add others, for example api or worker, each with its own app.yaml that names it:

YAML
service: api
runtime: nodejs22

Every deployment creates a version of a service. Versions are kept side by side, each with its own URL such as https://v2-dot-api-dot-my-project-id.appspot.com. Exactly which version receives the public traffic is a separate decision, which makes safe releases easy:

Terminal
# Deploy v2 without sending it any traffic
gcloud app deploy --version=v2 --no-promote

# Test it on its own URL, then send 10% of users to it
gcloud app services set-traffic default --splits=v1=0.9,v2=0.1

# Happy? Move everyone, or roll back by moving everyone to v1
gcloud app services set-traffic default --splits=v2=1

By default the split is made by IP address. You can also split by cookie so that one user keeps seeing the same version, which matters when versions behave differently, or randomly per request. Old versions that are no longer needed should be stopped or deleted, because they can still cost money.

Traffic splitting, in miniature

The stand-in below shows how a cookie-style split can be both weighted and sticky. It hashes a user ID into a number from 0 to 99 and maps ranges of that number to versions. This is our own hash for illustration, not the algorithm App Engine uses.

Node.js Stand-in: sticky weighted traffic split

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The counts come close to 900 and 100 but not exactly, because a hash spreads users roughly evenly, not perfectly. Growing the second version's share only moves users in one direction (from v1 to v2), and nobody bounces back and forth.

Rolling back and cleaning up

Because old versions stay deployed, a rollback is one set-traffic command, not a rebuild. Keep the last known good version until the new one has proved itself. Afterwards, list versions and remove what you no longer need:

Terminal
gcloud app versions list
gcloud app versions delete v1

You cannot delete an App Engine application. You can only disable it or delete the whole project. Deleting the project is the clean way to end a practice app, as long as you are sure nothing else lives in it.

Another look at splitting by percentage

Splits in a request-by-request scenario can also be tested with a fixed random source, which gives exact repeatable numbers. This second stand-in uses a seeded generator and shows a gradual canary rollout in steps.

Node.js Stand-in: gradual canary rollout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In a real rollout you would watch error rates and latency between steps and go back if they worsen. Cloud Logging and Cloud Monitoring (covered later in this course) give you those signals.

Launch stages and working with other services

Experimental and preview features. Google labels features by launch stage: alpha and beta (also called preview) are available for testing, may change without notice and usually carry no service-level agreement, while GA (general availability) is stable and supported. Some App Engine features, runtimes and gcloud commands are in beta (gcloud beta app ...). Use them for experiments, not for the critical path of a production system, and check the release notes before you upgrade a runtime.

Third-party and other services. An App Engine app rarely stands alone. It talks to Google services through client libraries (Firestore or Cloud SQL for data, Cloud Storage for files, Cloud Tasks for background work, Pub/Sub for messages, Secret Manager for credentials) and to third-party services over HTTPS from the app, such as payment providers, email APIs or a hosted database like MongoDB Atlas or Redis. Rules for these calls:

  • Keep third-party API keys in Secret Manager, not in app.yaml or source (Secret Manager and Cloud KMS).
  • Plan for the provider being slow or down: set timeouts and retries.
  • The older bundled App Engine APIs (Memcache, Task Queue, Search) were designed for the first-generation runtimes. For new code use their modern replacements: Memorystore, Cloud Tasks and a dedicated search service.
  • Scalability comes from the platform, but it also scales your calls to those services: a spike in your traffic is a spike for them, so check their quotas.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which App Engine environment can scale to zero instances?

    Standard runs in a sandbox with fast startup and can scale down to zero. Flexible keeps at least one VM-based instance running.
  2. 2. What does each gcloud app deploy create?

    Every deployment is a version. Versions coexist, and traffic settings decide which receives requests.
  3. 3. Why split traffic by cookie instead of by IP address?

    A cookie identifies the browser, so the user stays on one version even if their IP address changes.
  4. 4. Where should a database password for an App Engine app live?

    app.yaml is typically committed to source control, so secrets belong in Secret Manager.

Summary

  • App Engine runs your code without VM management; standard is sandboxed and scales to zero, flexible runs containers on VMs.
  • app.yaml defines runtime, scaling, environment variables and handlers.
  • A project has services, each with versions; deployment creates a version.
  • Traffic splitting by IP, cookie or randomly enables canary releases and instant rollback.
  • Clean up old versions, and remember an app can only be removed by deleting its project.

Next: Cloud Run