Updated on 2026-01-08 GMT+08:00

What Is Initializer?

Introduction

An initializer is a logic entry for initializing functions. For a function with an initializer, FunctionGraph invokes the initializer to initialize the function and then invokes the handler to process function requests. For a function without an initializer, FunctionGraph only invokes the handler to process function requests.

Application Scenarios

FunctionGraph executes a function in the following steps:

  1. Allocate container resources to the function.
  2. Download the function code.
  3. Use the runtime to load the function code.
  4. Initialize the function.
  5. Process the function request and return the result.

Steps 1, 2, and 3 are performed during a systematic cold start, ensuring a stable latency through proper resource scheduling and process optimization. Step 4 is performed during an application-layer cold start in complex scenarios, such as loading large models for deep learning, building database connection pools, and loading function dependencies.

To reduce the latency caused by an application-layer cold start, FunctionGraph provides the initializer to identify function initialization logic for proper resource scheduling.

Benefits of the Initializer

  • Function initialization and request processing are isolated to enable clearer program logic and structure better and higher-performance code.
  • Smooth function upgrade prevents performance loss caused by cold start initialization on the application layer. After new function instances are started, FunctionGraph automatically executes the initialization logic and then processes requests.
  • The overhead of application layer initialization is identified to accurately determine when resource scaling will be performed and how many resources will be required. This feature makes request latency more stable when the application load increases and more function instances are required.
  • If there are continuous requests and the function is not updated, the system may still reclaim or update existing containers. Although no code starts on the platform side, there are cold starts on the service side. The initializer can be used to ensure that requests can be processed properly.

Features of the Initializer

The initializer of each runtime has the following features:

  • No custom parameters

    The initializer does not support the definition of custom parameters, but only uses the variables in context for logic processing.

  • No return values

    No values will be returned for initializer invocation.

  • Initialization timeout

    You can set an initialization timeout (≤ 300s) different from the timeout for invoking the handler.

  • Time for execution

    Function instances are processes that execute function logic in a container and automatically scale if the number of requests changes. When a new function instance is generated, the system invokes the initializer and then executes the handler logic if the invocation is successful.

  • One-time execution

    After each function instance starts, the initializer can only be executed once. If an instance fails to execute the initializer, the instance is abandoned and another instance is generated. A maximum of three attempts are allowed. If the initializer is executed successfully, the instance will only process requests upon invocation and will no longer execute the initializer again within its lifecycle.

  • Naming rule

    For all runtimes except Java, the initializer can be named in the format of [File name].[Initializer name], which is similar with the format of a handler name. For Java, a class needs to be defined to implement the predefined initializer.

  • Billing

    The initializer execution duration will be billed at the same rate as the function execution duration.