Updated on 2025-07-15 GMT+08:00

Performance Optimization

As serverless technologies become increasingly popular, performance optimization is crucial for better efficiency and user experience.

This section focuses on optimizing FunctionGraph, covering cold start and function execution improvements. It offers practical tips to achieve optimal performance in various scenarios, helping you build more efficient and stable applications.

Code Optimization

  • Write idempotent code. This ensures that functions handle repeated events in the same way.
  • Use connection pools (HTTP, database, and Redis) properly to reduce the cold start overhead of creating new connections.
  • Avoid reinitializing variables on each invocation. Use global static variables or singletons. For middleware like Redis or Kafka, initialize clients in an init method or as global variables to reduce cold start overhead.
  • Enhance client retry mechanisms. When a function invocation returns non-200 status codes (such as 500, 429, 504), implement retry logic based on service needs to improve reliability.
  • Log appropriately. When accessing third-party services, Huawei Cloud services, or performing key operations in FunctionGraph, log the actions for troubleshooting, performance optimization, and service analysis.

Performance Stress Testing

Performance stress testing is crucial for choosing the optimal configuration. During the testing, use platform metrics, logs, and call chains to analyze performance data and optimize configuration. For more details on observable metrics, see Function Metrics.

Code Simplification and Image Downsizing

FunctionGraph downloads function code during cold start, which affects startup time. Larger code files extend download time and increase startup duration.

For custom image-based functions, larger images also prolong startup. To reduce cold start time, optimize applications by removing unnecessary code and reducing third-party library dependencies. For example, run the command npm prune in Node.js and autoflake in Python.

In addition, clearing test cases, unused binaries, and data files in third-party libraries can also reduce code download and decompression time.

Larger Memory

Increasing function memory can enhance CPU performance and accelerate startup and execution. Monitor execution time to assess the effect of various memory settings and select the best configuration.

For details, see Function Monitoring. For details about how to configure the memory, see Configuring Basic Settings.

Public Dependencies

When developing functions, especially in Python, third-party dependencies are often required. During cold start, large dependencies can increase startup time.

FunctionGraph provides public dependencies that are pre-loaded on execution nodes to mitigate this issue. Therefore, prioritize using public dependencies and reduce the use of private ones. For details, see Configuring Dependency Packages

Reserved Instances

After reserved instances are created for a function, the code, dependencies, and initializer of the function are automatically loaded. Reserved instances are always alive in the execution environment, eliminating the influence of cold starts on your services. For details about how to configure reserved instances, see Reserved Instance Management.

Function Initializer

For functions that are invoked frequently, initializing HTTP or database connections in the Initializer can make each execution much faster. For details about how to configure the function initializer, see Configuring Initialization.