Building your own Web service is one of the most common development scenarios for developers. Both the construction of full-stack websites and the creation of API interfaces require a Web framework that can quickly receive and process HTTP requests. With the advent of Serverless era, we hope to enjoy many advantages brought by cloud computing such as flexible scaling and free operation and maintenance, but we cannot easily try it due to the transformation cost brought by event-based triggering. How Web services fit perfectly with Serverless has become a problem we need to solve.

01. Trends in Web services development

From the perspective of development history, Web service development can be divided into three phases: local server deployment – cloud host deployment – Serverless deployment.

In the first stage, developers need to buy their own servers and deploy the Web services on the machine. Although developers have a wider scope of operation on the machine, for Web scenarios, developers often do not need to perform redundant operations on the machine, which brings extra maintenance costs on the machine.

The second stage, the development into the era of the cloud, you are more likely to buy host deployment on Yu Zaiyun services, free of maintenance costs, greatly improve the efficiency of development, this is also the most widely used way of development, but the host still forecast based on usage to purchase in advance, and will pay for unused computing resources, this way is having some the waste of resources and cost.

In the third stage, Serverless officially appeared on the stage. SCF cloud function has many advantages such as automatic expansion and shrinkage, billing by volume and so on. It is a perfect platform for Web service deployment because it perfectly meets the needs of developers for low development & maintenance costs. However, as cloud functions are event-based functions, they can only accept event type triggers and cannot directly receive and process native HTTP requests. Although cloud functions have introduced many solutions to help developers automatically convert formats, the creation of conversion layer and the processing of link length are still significant migration costs.

02. Problem of traditional API gateway + event function scheme

The API gateway automatically formats the incoming HTTP request into a JSON event format that the function can handle. On the function side, an adaptation layer function is added in front of the native Web services framework to convert the event JSON structure into a standard Web request, which is provided to the backend framework for processing.

Question:

  1. The transformation cost is high, so it is necessary to add an adaptation layer for format conversion and modify the monitoring mode of the Web framework.
  2. High debugging costs, inconsistent local code and cloud code, and inconsistent local and cloud debugging experience hinder the implementation of local debugging.
  3. Conversion link length, performance loss, one request requires four more conversions:
    1. http request –>> event
    2. event –>> http request
    3. HTTP Response –>> return structure required by APIGateway
    4. APIGateway requires a return structure –>> HTTP Response

In view of these problems, Tencent Cloud Serverless launched a new Function type – Web Function, to achieve the most elegant combination of Serverless and Web services.

03. Web Function – the next generation of website construction scheme

  1. After the HTTP request sent by the user passes through the API gateway, the gateway side directly transmits the native request through, and at the same time adds the function name, function region and other contents required when the gateway triggers the function in the request header to the function environment to trigger the execution of the back-end function.
  2. In the function environment, Nginx forwarding is realized through the built-in Proxy, and the header request information that is not in the product specification is removed, and the native HTTP request is sent to the user’s Web Server service through the specified port.
  3. The user’s Web Server is configured with the specified listening port (9000) and service startup files are deployed to the cloud, and HTTP requests are obtained and processed through the port.

04. Advantages of Web Function

  • Functions can directly receive and process HTTP requests, and the API gateway does not need to do JSON format conversion, reducing the link of request processing and improving Web service performance.
  • The experience of writing Web functions is closer to that of writing native Web services, ensuring consistency with the experience of developing services locally.
  • Rich framework support, you can use common Web frameworks (such as node.js Web framework:Express,KoaWriting Web functions can also quickly move your local Web framework services to the cloud with minimal modification.
  • The Web function automatically creates an API gateway service for you. After the deployment is complete, the gateway automatically generates a default URL for users to access and invoke, simplifying learning costs and debugging.
  • The console provides testing capabilities that allow you to quickly test your services in the function console.

05. Application for internal test of Web Function

Web Function is a Function type of cloud Function. Different from Event Function’s restriction on Event format, It focuses on optimizing Web service scenarios. Users can directly send HTTP requests to URLS to trigger Function execution.

  • Tencent Cloud Serverless is about to launch “Web Function”, now open for internal test application, click submit application.

One More Thing

Experience Tencent Cloud Serverless Demo immediately and receive Serverless new user package 👉 Tencent Cloud Serverless novice experience

Welcome to: Serverless Chinese!