laitimes

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

author:Alibaba Cloud Yunqi

Foreword: This article takes Alibaba Cloud Function Compute as an example to provide various application optimization and debugging solutions such as online debugging and local debugging.

<h1 class="pgc-h-arrow-right" data-track="2" > Serverless application debugging tips</h1>

In the process of application development, or when the application development is completed and the results are not as expected, we must carry out certain debugging work. However, under the Serverless architecture, debugging is often subject to extremely limited environment limitations, with applications being developed that can run healthily and as expected locally, but with unpredictable issues on the FaaS platform. Moreover, in some special environments, there is no way to simulate the online environment locally, and it is difficult to develop and debug the project.

Debugging Serverless applications has always been highly criticized, but various cloud vendors have not given up on in-depth exploration in the debugging direction. Taking Alibaba Cloud Function Compute as an example, it provides a variety of debugging solutions such as online debugging and local debugging.

<h1 class="pgc-h-arrow-right" data-track="5" > online debugging</h1>

1. Simple debugging

The so-called simple debugging is to debug in the console. Taking Alibaba Cloud Function Compute as an example, you can use the Execute button in the console for basic debugging, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Function online simple debugging page

If necessary, we can also simulate some events by setting up Events, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Simulate events by setting up An Event

The advantage of online debugging is that you can use some environments online for code testing. When the online environment has resources such as a VPC, it is difficult to debug in the local environment, such as the database needs to be accessed through the VPC, or the business logic with object storage triggers.

2. Breakpoint debugging

In addition to simple debugging, some cloud vendors also support breakpoint debugging, such as remote debugging of Alibaba Cloud Function Compute and remote debugging of Tencent Cloud functions. Taking Alibaba Cloud Function Compute remote debugging as an example, it can debug functions online through the console. After creating the function, the user can select remote debugging and click the "Enable Debugging" button, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Function Online Breakpoint Debugging Page (1)

After turning on debugging, wait a few moments and the system will enter the remote debugging interface, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Function Online Breakpoint Debugging Page (2)

You can do some breakpoint debugging at this point, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Function Online Breakpoint Debugging Page (3)

<h1 class="pgc-h-arrow-right" data-track="19" > local debugging</h1>

1. Command line tools

At present, most FaaS platforms will provide users with relatively complete command-line tools, including AWS's SAM CLI, Alibaba Cloud's Funcraft, and some open source projects such as Serverless Framework, Serverless Devs and other support for multi-cloud vendors. Debugging code through command-line tools is simple. Take Serverless Devs as an example to debug Alibaba Cloud Function Compute locally.

First make sure you have a Function Compute project locally, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Local Function Compute project

Then execute debug instructions under the project, such as debugging in Docker, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Command-line tools debug Function Compute

2. Editor plugin

Taking the VScode plugin as an example, after downloading the VSCode plugin of Alibaba Cloud Function Compute and configuring the account information, you can create a new function locally and debug the breakpoint after dotting, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

The VSCode plugin debugs Function Compute

When the function debugging is complete, perform operations such as deployment.

<h1 class="pgc-h-arrow-right" data-track="30" > other debugging scenarios</h1>

1. Local debugging of the Web framework

To develop a traditional web framework on the Alibaba Cloud FaaS platform, take the Bottle framework written in python as an example, you can add the following code:

When the deployment is applied online, you only need to fill in the ndex.app at the entry method to achieve smooth deployment.

2. Local simulation event debugging

For non-web frameworks, we can build a method locally, such as to debug an object storage trigger:

In this way, simulated event firing can be achieved by constructing an event object.

<h1 class="pgc-h-arrow-right" data-track="39" >serverless application optimization</h1>

Resource assessments remain important

The Serverless architecture, while pay-as-you-go, does not necessarily mean that it is less expensive than traditional server rentals. If you don't evaluate your project accurately and set some metrics incorrectly, the cost of the Serverless architecture can be enormous.

In general, the charges of the FaaS platform are directly related to three indicators, namely the configured function specifications (such as memory specifications, etc.), the time consumed by the program, and the traffic charges incurred. Typically, the time consumed by a program can be related to the memory specifications and the business logic that the program itself handles. The cost of traffic is related to the packet size of the program itself and the client interaction. Therefore, among these three common indicators, the memory specification that may cause a large deviation in billing due to irregular configuration is the memory specification. Taking Alibaba Cloud Function Compute as an example, suppose there is a Hello World program that is executed 10,000 times a day, and the costs incurred by different specifications of memory (excluding network costs) are shown in the table.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

As you can see from the table, when the program can execute normally in 128MB of memory, if the memory specification is incorrectly set to 3072MB, the monthly cost may skyrocket by 25 times! Therefore, before launching the Serverless application, the resources should be evaluated to further reduce costs with a more reasonable configuration.

Reasonable code package specifications

There are limits on the size of code packages in the FaaS platforms of various cloud vendors. Throwing aside the cloud vendor's restrictions on code packages, the impact that simply the specifications of the code package may have can be seen through the cold-start process of the function, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Function cold-start flow diagram

During the function cold boot process, when the uploaded code package is too large, or the decompression speed is too slow due to too many files, the loading code process will be longer, which will further lead to a longer cold boot time.

Imagine that when there are two compressed packages, one is a code package of only 100KB, the other is a 200MB code package, both of which are idealized under the gigabit intranet bandwidth (that is, do not consider the storage speed of the disk, etc.) download, even if the maximum speed can reach 125MB/s, then the former download time is less than 0.01 seconds, and the latter takes 1.6 seconds. In addition to the download time, plus the decompression time of the file, the cold start time of the two may be 2 seconds apart. In general, for traditional web interfaces, if you want a response time of more than 2 seconds, it is actually unacceptable for many businesses, so when packaging code, it is necessary to reduce the compressed package size as much as possible. Taking the Node .js project as an example, when packaging code packages, we can use Methods such as Webpack to compress the dependent package size, further reduce the specifications of the overall code package, and improve the cold start efficiency of the function.

Reuse instances reasonably

In order to better solve the problem of cold start and make more reasonable use of resources, there is instance reuse in the FaaS platform of various cloud vendors. The so-called instance reuse is that when an instance completes a request, it is not released, but enters a silent state. Within a certain time frame, if a new request is assigned, the corresponding method will be called directly, without the need to initialize various resources, etc., which greatly reduces the cold start of the function. For verification, we can create two functions:

Click the "Test" button in the console to test the above two functions to determine whether they have "Test" printed in the log, and the statistical results are shown in the table.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Functions reuse records

As you can see, in fact, the case of instance reuse exists. Thinking further, if the print("Test") statement is an initial database connection, or if functions 1 and 2 load a deep learning model, is function 1 executing every request, and function 2 can reuse existing objects?

So in a real project, there are some initialization operations that can be implemented according to function 2, such as:

In the machine learning scenario, load the model at initialization time to avoid loading the model every time the function is triggered.

Create a linked object at initialization time to avoid creating a linked object every time you request it.

Others need to be downloaded at the first load, and the loaded files are implemented at initialization time to improve the efficiency of instance reuse.

Be good at using function features

The FaaS platforms of various cloud vendors have some features. The so-called platform features mean that these functions may not be the capabilities specified in CNCF WG-Serverless Whitepaper v1.0 or the capabilities described, but only the functions that are mined and implemented from the user's point of view as the cloud platform according to its own business development and demands, and may only be the functions owned by a cloud platform or several cloud platforms. In general, if properly utilized, this type of function will qualitatively improve business performance.

1.Pre-freeze &amp; Pre-stop

Taking Alibaba Cloud Function Compute as an example, during the development of the platform, the pain points of users (especially those that hinder the smooth migration of traditional applications to the Serverless architecture) are as follows.

Asynchronous background metric data delay or loss: If the send is not successful during the request, it may be delayed until the next request, or the data point may be discarded.

Synchronous sending metrics increases latency: If you call a Flush-like interface after each request ends, you not only increase the latency of each request, but also create unnecessary pressure on the backend service.

The function gracefully goes offline: When the instance is closed, the application has the requirements of cleaning up the connection, closing the process, and reporting the status. When an instance goes offline in Function Compute, the developer cannot grasp it, and the webhook notification function instance offline event is missing.

Based on these pain points, Alibaba Cloud released the Runtime Extensions feature. This functionality extends on the existing HTTP service programming model, adding PreFreeze and PreStop Webhooks to existing HTTP server models. The extension developer is responsible for implementing the HTTP handler, listening for function instance lifecycle events, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

Extended programming model versus existing programming model handles work descriptions

PreFreeze: Before each time the Function Compute service decides to freeze the current function instance, the Function Compute service calls the HTTP GET/prefreeze path, and the extension developer is responsible for implementing the corresponding logic to ensure that the necessary operations before the instance are frozen, such as waiting for the indicator to be sent successfully, as shown in the figure. The time the function invokeFunction is called does not include the execution time of the PreFreeze Hook.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

PreFreeze timing diagram

PreStop: Before each time Function Compute decides to stop the current function instance, the Function Compute service will call the HTTP GET/prestop path, and the extension developer is responsible for implementing the corresponding logic to ensure that the necessary operations before the instance is released, such as waiting for the database link to close, as well as reporting and updating the status, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

PreStope timing diagram

2. Single instance multi-concurrency

As we all know, the function computing of various cloud vendors is usually a request-level isolation, that is, when the client initiates 3 requests to function Computing at the same time, it will theoretically produce 3 instances to deal with, which may involve cold start and state correlation between requests. Therefore, some cloud vendors provide the ability to multi-concurrently a single instance (for example, Alibaba Cloud Function Compute). This capability allows the user to set an instance concurrency for a function, that is, a single function instance can process multiple requests at the same time, as shown in the figure.

Serverless Engineering Practice | Serverless App Optimization & Debugging TipsServerless App Debugging Tips Online Debugging Local Debugging Other Debugging ScenariosServerless App Optimization

A simple diagram of a single-instance multi-concurrency effect

As shown in the preceding figure, suppose there are 3 requests to be processed at the same time, when the instance concurrency is set to 1, Function Compute needs to create 3 instances to process the 3 requests, each instance to process 1 request, and when the instance concurrency is set to 10 (that is, 1 instance can process 10 requests at the same time), Function Compute only needs to create 1 instance to process these 3 requests.

The advantages of single-instance multi-concurrency are as follows.

Reduce execution time and save money. For example, a partial I/O function can process requests concurrently within an instance, reducing the number of instances and thus the overall execution time.

State can be shared between requests. Multiple requests can pool database connection pools within a single instance, reducing the number of connections to the database.

Reduces the probability of cold start. Because multiple requests can be processed within a single instance, the number of new instances created is reduced and the probability of a cold launch is reduced.

Reduce the use of VPC IP. Under the same load, single-instance multi-concurrency can reduce the total number of instances, thereby reducing vpc IP consumption.

Single-instance multi-concurrent scenarios are more extensive, such as scenarios where functions have more time waiting for downstream service responses. Single-instance multi-concurrency also has scenarios that are not suitable for applications, such as when there is shared state in a function and cannot be accessed concurrently, and the execution of a single request consumes a lot of CPU and memory resources, which is not suitable for using the single-instance multi-concurrency function.

About the author: Liu Yu (Jiang Yu) Ph.D. in Electronic Information, National University of Defense Technology, Alibaba Cloud Serverless Product Manager, Alibaba Cloud Serverless Cloud Evangelist, CIO College Distinguished Lecturer.

Original link: http://click.aliyun.com/m/1000299610/

This article is the original content of Alibaba Cloud and may not be reproduced without permission.

Read on