How do I create a new instance for each request?

Updated at:
Copy as MD

Function Compute does not create a new instance for each request. After an instance finishes processing a request, it is frozen rather than destroyed. When the next request arrives before the frozen instance is destroyed, Function Compute reuses that instance — a behavior known as a warm start.

Why instance reuse can be a problem

Because the same instance handles multiple requests sequentially, any state written outside the handler function persists across invocations. If your function stores user-specific data, session tokens, or other sensitive information in global variables or module-level objects, that data may be accessible to a subsequent request. This is the most common reason developers want per-request instance isolation.

What you can do

Function Compute does not support forcing a new instance per request. To avoid state leakage within Function Compute, follow these practices:

  • Isolate state inside the handler: Keep all request-specific data within the handler function scope, not in global variables or module-level objects.

  • Clear shared resources explicitly: If you use connection pools, caches, or global counters, reset or scope them per invocation.

If your workload requires strict per-request process isolation, use the job feature of Serverless App Engine (SAE). SAE jobs provision a fresh environment for each execution, guaranteeing that no state carries over between runs.