06 How Function Compute Works

06 How Function Compute Works #

Function Compute Invocation Chain #

5.PNG

The diagram above shows the complete request and invocation chain of Function Compute. Function Compute is an event-driven serverless application, which means that functions can be automatically triggered by events. For example, when an object is uploaded to OSS, the function can be automatically triggered to process the newly uploaded image. Function Compute supports a variety of event sources, including log service, object storage, table storage, message service, API gateway, and CDN.

In addition to event triggering, functions can also be invoked directly through API/SDK. Invocations can be synchronous or asynchronous. When a request reaches Function Compute, it assigns an execution environment to the request. If it is an asynchronous invocation, Function Compute will enqueue the request and wait for consumption.

Function Compute Invocation Modes #

6.jpg

Synchronous invocation means that the client expects the server to immediately return the computation result. When a request reaches Function Compute, it immediately assigns an execution environment to execute the function.

Taking API Gateway as an example, when API Gateway synchronously triggers Function Compute, the client will wait for the execution result from the server. If an error occurs during the execution process, Function Compute will directly return the error instead of retrying it. In this case, the client needs to add a retry mechanism for error handling.

7.jpg

Asynchronous invocation means that the client doesn’t need to know the function result immediately. Function Compute can return success after queuing the request, without waiting for the function invocation to finish.

Function Compute gradually consumes the requests in the queue, assigns execution environments, and executes functions. If an error occurs during the execution process, Function Compute retries the erroneous requests. The function error is retried three times, and system errors are retried indefinitely using the exponential backoff approach until success.

Asynchronous invocation is suitable for data processing scenarios where delay is not critical but task execution success needs to be ensured. If users need to understand the failed requests and customize the handling, they can use the Destination feature.

Function Compute Execution Process #

Function Compute is serverless, which means that developers don’t need to care about servers. Function Compute assigns instances to execute functions on behalf of developers.

8.jpg

As shown in the diagram above, when a function is called for the first time, Function Compute needs to dynamically schedule instances, download code, unzip code, and start instances to obtain an executable function code environment. It then starts executing the user’s initialization function in the instances allocated by the system for actual function logic execution. This process of scheduling and starting instances is called cold start.

After the function logic execution is completed, the instance is not immediately released. It waits for some time. If there is a new invocation during this period, the instance is reused. For example, in the diagram above, Request 2 can directly use the execution environment because it has already been allocated. Therefore, Request 2 does not encounter a cold start.

After Request 2 is completed, if there is no new request allocated to the instance during the waiting period, the system reclaims the instance and releases the execution environment. After the instance is released, a new request, Request 3, arrives at Function Compute. It needs to go through the process of scheduling and starting instances, downloading code, and unzipping code, and encounters a cold start again.

Therefore, to minimize the impact of cold starts and reduce the delay caused by cold starts, developers should try to avoid cold starts and reduce their effects.

9.jpg

Using provisioned instances can completely avoid cold starts. Provisioned instances are allocated after users make reservations, and the instances are not automatically allocated or reclaimed by the system.

Provisioned instances allow users to control the lifecycle of instances and can reside without being destroyed. This completely eliminates the delay caused by cold starts, provides ultimate performance, and removes obstacles for migrating online applications to Function Compute.

If the business scenario is not suitable for provisioned instances, ways to reduce the delay caused by cold starts should be considered. For example, reducing the code package size can reduce the time required to download and unzip the code package. The Initializer function is the initialization function of an instance. It is executed only once in the same instance, so time-consuming common logic can be put in the Initializer. For example, loading dependencies and establishing connections in NAS. Furthermore, continuous and stable requests should be maintained to avoid sudden bursts of traffic. When the number of instances already started is insufficient to support a large amount of burst traffic, unavoidable cold starts will occur.