|
| 1 | +# App Engine API Client Configuration |
| 2 | + |
| 3 | +The App Engine Java runtime communicates with Google Cloud APIs (such as |
| 4 | +Datastore, Task Queue, and Memcache) using an HTTP-based RPC mechanism. The |
| 5 | +runtime includes two HTTP client implementations for this purpose: a default |
| 6 | +client based on Jetty and an alternative client using the JDK's built-in HTTP |
| 7 | +facilities. |
| 8 | + |
| 9 | +This document describes both clients and how to configure them using environment |
| 10 | +variables and Java system properties. |
| 11 | + |
| 12 | +## Jetty HTTP Client (Default) |
| 13 | + |
| 14 | +The Jetty HTTP client is the default client used by the runtime. It is based on |
| 15 | +the [Eclipse Jetty](https://eclipse.dev/jetty/) HTTP client and is optimized for |
| 16 | +high performance and efficient connection management. |
| 17 | + |
| 18 | +By default, the client is configured to allow a maximum of 100 concurrent |
| 19 | +connections to API backends. This limit helps prevent memory exhaustion on |
| 20 | +smaller App Engine instance types (like F1 or F2) and avoids overwhelming |
| 21 | +backend services during sudden traffic spikes or high rates of failing requests |
| 22 | +with aggressive retry logic. If the connection limit is reached, subsequent |
| 23 | +requests are queued until a connection becomes available. |
| 24 | + |
| 25 | +### Configuration |
| 26 | + |
| 27 | +You can configure the Jetty client using the following environment variables: |
| 28 | + |
| 29 | +* **`APPENGINE_API_MAX_CONNECTIONS`**: Sets the maximum number of concurrent |
| 30 | + connections for API calls. If you observe API call latency or |
| 31 | + connection-related errors under high load, you might consider adjusting this |
| 32 | + value. |
| 33 | + * Default: `100` |
| 34 | +* **`APPENGINE_API_CALLS_IDLE_TIMEOUT_MS`**: Sets the idle timeout in |
| 35 | + milliseconds for connections in the connection pool. Connections that are |
| 36 | + idle for longer than this duration may be closed. |
| 37 | + * Default: `58000` (58 seconds) |
| 38 | + |
| 39 | +## JDK HTTP Client |
| 40 | + |
| 41 | +The JDK HTTP client uses Java's built-in `HttpURLConnection` for API calls. It |
| 42 | +is provided as an alternative to the Jetty client and can be useful for |
| 43 | +troubleshooting network- or connection-related issues that might be specific to |
| 44 | +one client implementation. In general, it may be less performant than the |
| 45 | +default Jetty client. |
| 46 | + |
| 47 | +To use the JDK client instead of the Jetty client, set the |
| 48 | +`APPENGINE_API_CALLS_USING_JDK_CLIENT` environment variable to any non-null value |
| 49 | +(e.g., `true`). |
| 50 | + |
| 51 | +### Configuration |
| 52 | + |
| 53 | +When using the JDK client, you can configure its threading behavior: |
| 54 | + |
| 55 | +* **`appengine.api.use.virtualthreads`** (Java System Property): If set to `true`, |
| 56 | + the JDK client will use Java Virtual Threads (when available on the JVM) to |
| 57 | + handle API requests. This allows for a very high number of concurrent |
| 58 | + requests without being limited by platform thread stack sizes. Note, |
| 59 | + it is different than `appengine.use.virtualthreads` |
| 60 | + * Default: `false` |
| 61 | + |
| 62 | +If `appengine.api.use.virtualthreads` is `false` or not set, the JDK client uses a |
| 63 | +traditional thread pool model. In this mode, you can control the thread pool |
| 64 | +size using an environment variable: |
| 65 | + |
| 66 | +* **`APPENGINE_API_MAX_CONNECTIONS`**: Sets the maximum number of threads in |
| 67 | + the pool for handling API calls. This limits the number of concurrent API |
| 68 | + calls, similar to how it works for the Jetty client. This variable is |
| 69 | + **ignored** if `appengine.api.use.virtualthreads` is set to `true`. |
| 70 | + * Default: `100` |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +### Example |
| 75 | + |
| 76 | +To change the API call path to use the JDK client instead of the Jetty client, |
| 77 | +and to enable virtual threads for this client, add the following environment |
| 78 | +variable and system property to `appengine-web.xml` and redeploy your |
| 79 | +application: |
| 80 | + |
| 81 | +```xml |
| 82 | +<env-variables> |
| 83 | + <env-var name="APPENGINE_API_CALLS_USING_JDK_CLIENT" value="true" /> |
| 84 | + <env-var name="APPENGINE_API_MAX_CONNECTIONS" value="100" /> |
| 85 | + <env-var name="APPENGINE_API_CALLS_IDLE_TIMEOUT_MS" value="58" /> |
| 86 | +</env-variables> |
| 87 | +<system-properties> |
| 88 | + <property name="appengine.api.use.virtualthreads" value="true" /> |
| 89 | +</system-properties> |
| 90 | +``` |
0 commit comments