Serverless engineering practice | Serverless application optimization and debugging secrets

2021/10/0918:41:02 technology 1236

Foreword: This article will take the calculation of Alibaba Cloud function as an example, and provide various application optimization and debugging solutions such as online debugging and local debugging.

Serverless application debugging tips

In the application development process, or when the application development is completed, the execution result does not meet expectations, we need to carry out certain debugging work. However, under the Serverless architecture, debugging is often subject to extreme environmental restrictions, and the developed applications can run locally and in line with expectations, but some unpredictable problems occur on the FaaS platform. Moreover, in some special environments, there is no way to simulate the online environment locally, which makes it difficult to develop and debug projects.

Serverless application debugging has always been criticized, but various cloud vendors have not given up in-depth exploration in the debugging direction. Taking Alibaba Cloud Function Computing as an example, it provides various debugging solutions such as online debugging and local debugging.

online debugging

1. Simple debugging

The so-called simple debugging is to debug on the console. Taking Alibaba Cloud Function Computing as an example, it can perform basic debugging through the "Execute" button on the console, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

function online simple debugging page

When necessary, we can also simulate some events by setting Event,as the picture shows.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

Simulate events by setting Event

The advantage of online debugging is that you can use some of the online environments for code testing. When the online environment has resources such as VPC, it is difficult to debug in the local environment. For example, the database needs to be accessed through the VPC, or there are business logic for 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 Computing, remote debugging of Tencent Cloud Cloud functions, etc. . Take the remote debugging of Alibaba Cloud Function Computing as an example, which can perform online debugging of functions through the console. After the function is created, the user can select remote debugging and click the "Open debugging" button, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

function online breakpoint debugging page (1)

After debugging is turned on, after a while, the system will enter the remote debugging interface, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

function online breakpoint debugging page (2)

At this time, you can perform some breakpoint debugging, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

function online breakpoint debugging page (3)

local debugging

1. Command line tool

span1Most 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. The method of code debugging through command line tools is very simple. Take Serverless Devs as an example to debug Alibaba Cloud function computing locally.

First make sure that there is a function calculation item locally, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

local function calculation project

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

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

command line tool to debug function calculation

2. Editor plugin

VS Code _span4 VS Code _span 4 VS Code _span plugin for example And after configuring the account information, you can create a new function locally, and you can perform breakpoint debugging after clicking, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

VSCode plug-in debugging function calculation

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

Other debugging solutions

1. Local debugging of the web framework

To develop a traditional web framework on the Aliyun FaaS platform, the following code can be added to the bottle framework. :

  app = bottle.default_app() and conditionally restrict the run method (if __name__ =='__main__'): if __name__ =='__main__': bottle.run(host='localhost', port= 8080, debug=True) For example: # index.pyimport [email protected]('/hello/')def index(name): return " Hello World  "app = bottle.default_app()if __name__ = ='__main__': bottle.run(host='localhost', port=8080, debug=True)  

When deploying the application online, you only need to fill in ndex.app at the entry method,You can achieve smooth deployment.

2. Local simulation event debugging

For non-web frameworks, we can build a method locally, for example, to debug the object storage trigger: , context): print(event)def test(): event = {"events": [{"eventName": "ObjectCreated:PutObject", "eventSource": "acs:oss", "eventTime": "2017-04 -21T12:46:37.000Z", "eventVersion": "1.0", "oss": {"bucket": {"arn": "acs:oss:cn-shanghai:123456789:bucketname", "name": " testbucket", "ownerIdentity": "123456789", "virtualBucket": "" }, "object": {"deltaSize": 122539, "eTag": "688A7BF4F233DC9C88A80BF985AB7329", "key": "image/a. jpg", "size": 122539 }, "ossSchemaVersion": "1.0", "ruleId": "9adac8e253828f4f7c0466d941fa3db81161****" }, "region": "cn-shanghai", "requestParameters": {"sourceIPAddress": "140.205.***.***" }, "responseElements": {"requestId": "58F9FF2D3DF792092E12044C" }, "userIdentity": {"principalId": "123456789"}}]} handler(json.dumps(event ), None) if __name__ == "__main__": print(test())

like this,By constructing an event object, the simulated event trigger can be realized.

Serverless application optimization

Resource evaluation is still important

Serverless architecture is pay-as-you-go, but it does not necessarily mean that the cost of renting a server is lower than that of traditional servers. If the evaluation of your own projects is inaccurate, and some indicators are set unreasonably, the costs incurred by the serverless architecture may be huge.

Generally speaking, the FaaS platform charges 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 generated. Under normal circumstances, the time consumed by the program may be related to the memory specifications and the business logic handled by the program itself. The traffic cost is related to the size of the data packet that the program itself interacts with the client. Therefore, among the three common indicators, the memory specification may cause a relatively large deviation in billing due to non-standard configuration. Take Alibaba Cloud Function Computing as an example. Assuming that there is a Hello World program that is executed 10,000 times a day, the costs (excluding network costs) incurred by different specifications of memory are shown in the table.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

As can be seen from the table, when the program can be executed normally in the 128MB memory, if the memory specification is set to 3072MB by mistake, the monthly cost may skyrocket 25 Times! Therefore, before launching Serverless applications, resources must be evaluated in order to further reduce costs with a more reasonable configuration.

Reasonable code package specifications

Each cloud vendor’s FaaS platform has restrictions on the code package size.Abandoning the restrictions of cloud vendors on code packages, simply speaking of the impact that 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 application optimization and debugging secrets - DayDayNews

function cold start process diagram

During the function cold start process, when the uploaded code package is too large or the decompression speed is too slow due to too many files, the code loading process will change. Long, further leading to longer cold start time.

Imagine that when there are two compression packages, one is a 100KB code compression package, and the other is a 200MB code compression package. Both are idealized under the gigabit intranet bandwidth (that is, not Considering the storage speed of the disk, etc.) download, even if the maximum speed can reach 125MB/s, the download time of the former is less than 0.01 seconds, and the download time of the latter is 1.6 seconds. In addition to the download time, plus the file decompression time, the cold start time of the two may differ by 2 seconds. In general, for the traditional web interface, if the response time is more than 2 seconds, it is actually unacceptable for many businesses. Therefore, when packaging the code, it is necessary to reduce the size of the compressed package as much as possible. Taking the Node.js project as an example, when packaging the code package, we can use methods such as Webpack to compress the size of the dependent package, further reducing the overall code package specification and improving the cold start efficiency of the function.

Reasonable reuse of instances

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

  Function 1: # -*- coding: utf-8 -*-def handler(event, context): print("Test") return'hello world' function 2: # -*- coding: utf-8 -*-print("Test")def handler(event, context): return'hello world'  

Click the "Test" button in the console, and check the above two A function is tested to determine whether it has output "Test" in the log. The statistical results are shown in the table.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

function reuse record

As you can see, there are actually instances of instance reuse. Further thinking, if the print("Test") statement is an initial database connection, or function 1 and function 2 load a deep learning model, is it function 1 or every request will be executed, and function 2 can reuse existing Object?

So in actual projects, some initialization operations can be implemented according to function 2, for example:

  • In the machine learning scenario,Load the model during initialization to avoid loading the model every time the function is triggered.
  • creates a link object during initialization to avoid creating a link object for every request.
  • Some other files that need to be downloaded and loaded at the first load are implemented at the time of initialization, which improves the efficiency of instance reuse.

is good at using function features

FaaS platforms of various cloud vendors have some features. The so-called platform features mean that these functions may not be the capabilities or described in CNCF WG-Serverless Whitepaper v1.0, but are merely functions that are excavated and implemented from the perspective of users based on the development and demands of the cloud platform. , May just be a function owned by a certain cloud platform or a few cloud platforms. Under normal circumstances, if this type of function is used properly, business performance will be qualitatively improved.

1.Pre-freeze & Pre-stop

Take Alibaba Cloud Function Computing as an example. During the development of the platform, user pain points (especially hindering the smooth migration of traditional applications) to Serverless are as follows .

  • Asynchronous background indicator data is delayed or lost: if it is not sent successfully during the request period,It may be delayed until the next request, or the data point may be discarded.
  • Synchronous sending metrics increase latency: If you call a similar Flush interface after each request ends, it will not only increase the latency of each request, but also create unnecessary pressure on the back-end service.
  • function gracefully offline: When the instance is closed, the application has the requirements of cleaning up connections, closing processes, and reporting status. When the instance goes offline in function calculation, the developer cannot grasp it, and there is no Webhook to notify the function instance offline event.

Based on these pain points, Alibaba Cloud released the Runtime Extensions function. This function is extended on the existing HTTP service programming model, adding PreFreeze and PreStop Webhook to the existing HTTP server model. The extension developer is responsible for implementing the HTTP handler and monitoring the life cycle events of the function instance, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

A sketch of the work content handled by the extended programming model and the existing programming model

  • PreFreeze: Before each function computing service decides to freeze the current function instance, the function computing service will call HTTP GET/ For the prefreeze path, the extension developer is responsible for implementing the corresponding logic to ensure the completion of the necessary operations before the instance is frozen, such as waiting for the indicator to be sent successfully, as shown in the figure. The time when the function calls InvokeFunction does not include the execution time of PreFreeze Hook.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

PreFreeze timing diagram

  • PreStop: Before each function calculation decides to stop the current function instance, the function calculation service will call the HTTP GET/prestop path to implement the corresponding logic to extend the developer Make sure to complete the necessary operations before the instance is released, such as waiting for the database link to be closed, and reporting and updating the status, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

PreStope sequence diagram

2. Single instance multiple concurrent

is commonly known as the isolation function of each client at the same time. 3 requests to the function calculation, theoretically, 3 instances will be generated to deal with, this time may involve issues such as cold start and state correlation between requests. Therefore, some cloud vendors provide single-instance multi-concurrency capabilities (for example, Alibaba Cloud Function Computing). This capability allows users to set an instance concurrency (InstanceConcurrency) for a function, that is, a single function instance can handle multiple requests at the same time, as shown in the figure.

Serverless engineering practice | Serverless application optimization and debugging secrets - DayDayNews

single-instance multiple concurrency effect diagram

As shown in the figure above, assuming that there are 3 requests to be processed at the same time, when the instance concurrency is set to 1, the function calculation needs to create 3 instances. To process these 3 requests, each instance processes 1 request separately; when the instance concurrency is set to 10 (that is, 1 instance can handle 10 requests at the same time),Function calculation only needs to create 1 instance to handle these 3 requests.

The advantages of single instance multiple concurrency are as follows.

  • Reduce execution time and save costs. For example, partial I/O functions can process requests concurrently in one instance, reducing the number of instances, thereby reducing the total execution time.
  • status can be shared between requests. Multiple requests can share the database connection pool in one instance, thereby reducing the number of connections to the database.
  • Reduce cold start probability. Since multiple requests can be processed in one instance, the number of times to create a new instance will be reduced and the probability of a cold start will decrease.
  • reduces the occupation of VPC IP. Under the same load, a single instance with multiple concurrency can reduce the total number of instances, thereby reducing the occupation of VPC IP.

Single-instance multi-concurrency application scenarios are more extensive. For example, scenarios where there is a lot of time in the function to wait for the response of downstream services are more suitable to use this function. Single-instance multiple concurrency also has scenarios that are not suitable for applications. For example, when the function has shared state and cannot be accessed concurrently, the execution of a single request consumes a lot of CPU and memory resources. At this time, it is not suitable to use the single-instance multi-concurrency function.

About the author: Liu Yu (Jiang Yu) Ph.D. candidate in Electronic Information, National University of Defense Technology,Alibaba Cloud Serverless product manager, Alibaba Cloud Serverless cloud evangelist, special lecturer of CIO Academy.

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

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

.

technology Category Latest News