2020.5.16 Front-end Serverless

preface

I just graduated from front-end Mengxin less than a year ago, front-end Early Chat conference is a very good technical conference organizer, except for the first session, I have attended every session since. However, the previous sessions of the conference did not pay attention to it after watching the live broadcast, mainly holding the mentality of watching it later (in fact, it is likely to be a long time before they will pay attention to it). Aware of the previous problems, I plan to summarize each share from this session and keep paying attention to relevant information in my spare time.

This year is the sixth, I will make a simple summary of the topic “front-end Serverless” from the following perspectives:

  • Why Serverless
  • What is Serverless
  • How to use Serverless
  • How is Serverless designed
  • What will be the impact of the emergence and adoption of Serverless
  • What are the current practices of Serverless

Statement:

  • Xiaobai front-end, long ago on Zhihu concerned about Serverless, but has not been to study this piece, this summary is my Serverless entry bar. This article is a summary of comprehension of the type of article, if the article involves infringement of things, or wrong things, I hope you can point out, thank you!!

  • I have never contacted Serverless before. Due to time problem, I just played with the simple Serverless instance of Aliyun recently. I also hope to communicate and learn from you!!

  • This article is my first article in Nuggets, and my updated articles are also posted on CSDN and Zhihu. Please post the link address of Zhihu and Github (recently I started to focus on making wheels, so there is not much open source). I also hope to communicate and learn with friends or leaders on these platforms: www.zhihu.com/people/bi-a… , github.com/yaodebian

  • This article involves a lot of things, due to the time problem, the later will be updated according to the situation.

  • Finally, briefly introduce myself, Yaodebian or “the other side of Yao” (I mention this because many people refer to yaodebian as “yaodebian”) is my common nickname, “Winter mu” is my name (if I can enter Ali in the future), and that’s the end of the talk (run away…

The evolution of Serverless from scratch

Before looking at this evolution in detail, I think it’s important to get a sense of how the “back and forth” (Web) has evolved.

Front-end to full stack evolution

This chart shows some historical nodes of front-end development:

  1. Before Ajax, web development architecture and processes looked something like this:

There is no distinction between FrontEnd and BackEnd, and all work is performed by one post.

  1. With the advent of Ajax, some JS libraries based on Ajax technology (jquery), JSP, and so on, front-end work has become complicated.

  2. Then, in 2015, Google put forward the concept of PWA, the emergence of a variety of front-end and back-end separation Web frameworks (angular, Vue, React), webpack, gulp and some other packaging tools… Today, the front-end has engineering development, component-based development, complexity is no less than the back end, the front and back end has really separated the division of labor (although some old projects are still JSP) :

Industry division of labor, generally we will have three concepts: front end, back end, full stack.

Full stack developers, from the perspective of mastering technology, as long as they master both front-end technology and back-end technology, they are full stack (or some simpler view is: can write page + can write interface + can tune database).

But the full stack actually requires much more than that:

Full stack (or architecture) not only requires a master of front-end and back-end technologies, but also business and architecture (code structure, performance optimization, front-end architecture, back-end architecture, etc.).

I am not familiar with this particular stack (escape…

Serverless from nothing

  1. Prehistoric times, Serverfull

Developers only need to focus on business development. After development, a separate operation and maintenance staff will be responsible for releasing iteration versions, merging branches, putting the application online, rolling back problems, and if there are problems online, developers will be required to help solve the problems before releasing.

  1. Impergium, DevOps

There was OpsConsole, an operations console, through which developers could self-release iterations and self-capture error logs.

  1. The industrial age

The OpsConsole system was further upgraded to automate code distribution through pipelining:

Code scan -> Test -> Gray Validation -> Go online

Now developers can even use OpsConsole without logging in, just merge the code into the designated branch of git repository, and the rest of OpsConsole will pipelined automatically for us to distribute.

In fact, this is our current Serverless.

  1. In the future

Further optimizations for OpsConsole: better resource saving, more user-friendly…

Therefore, the emergence of Serverless is the result of advancing the needs of The Times. The purpose is to better liberate the labor force and save costs.

What is the problem Serverless is trying to solve

  1. To reduce overhead

    • From the perspective of service providers: reduce the cost of hardware, expand the market, and effectively reduce the operating cost of long tail applications in enterprises;
    • Application deployment personnel: zero o&M experience, minute deployment;
    • Ordinary users: spend as much as you want to reduce the purchase cost;
  2. Improving r&d effectiveness

    SFF (Serverless For Frontend) allows front-end students to be responsible For the arrangement of data interfaces by themselves, while Baas of microservices enables back-end students to pay more attention to domain design.

What is Serverless

The word Serverless consists of two parts: Server + Less. Server refers to server-side operation and maintenance, and Less refers to the purpose, i.e. Less operation and maintenance or no operation and maintenance NoOps. Together, the server is o&M free. Of course, NoOps is ideal, so Serverless, not ServerZero.

define

Serverless currently has two definitions:

  • In a narrow sense (generally) :

    Serverless = Serverless Computing Architecture = FaaS Architecture = Trigger (event-driven) + FaaS (function-as-a-service) + BaaS (back-end as-a-service, persistent or third-party services) = FaaS + BaaS

  • Broad sense:

    Serverless = Server O&M free = Cloud services with Serverless features

Cloud services with Serverless features can be understood as follows:

  • There is no need for users to care about the service side (fault tolerance, disaster recovery, security verification, automatic capacity expansion, log debugging, etc.);

  • Pay by usage (number of calls, duration, etc.), low cost and high performance in parallel, saving in most scenarios;

  • Fast iteration & trial-and-error ability (multi-version control, gray scale, CI&CD, etc.);

Serverless Practices – Cloud development

Serverless is just a concept, and the corresponding practice is cloud development.

  1. What is CloudBase?

Serverless architecture is adopted to provide cloud functions, cloud databases, cloud storage and other basic resource services without environment setup and other operation and maintenance transactions. Meanwhile, static hosting, command-line tool (CLI), VS Code plug-in, Flutter SDK and other capabilities are provided to greatly reduce the threshold of application development. Help build applets, Web applications and apps quickly.

  1. The overall architecture of cloud development

  1. Cloud development infrastructure

  1. Cloud development supports engineering with several characteristics

    • High efficiency:

      • Compared with traditional development mode, cloud development is out of the box;

      • Reduce back-end and operation costs, reduce r&d manpower investment; (The development of front-end, back-end, system operation and maintenance, and middleware might take 7-10 days, but now it only needs front-end investment of 2 days)

    • Quality:

      • Cloud development second capacity expansion carrying hundreds of millions of traffic; (Automatic scaling, pay-as-you-go)

      • Taking Tencent Cloud as an example, the daily volume of cloud development platform can withstand 700 million;

    • security

    • multiterminal

Faas (Function As A Server)

Faas is the basis of automatic expansion of Serverless. Its function is to get through the data call link through the trigger mode, in order to replace the current use of more BFF or traditional call mode based on nGINx reverse proxy.

  1. Run the logic

The Faas request flow looks like this: The user accesses the function trigger -> the function trigger generates an HTTP Request event to notify the function service -> The function service checks if there is an instance of the function, executes it and returns data to the trigger, and if there is no instance of the function, builds a function instance first -> The function execution returns data to the trigger, and the trigger returns data to the user

Faas cold start: The call to a function starts until the function instance is ready to complete.

The average cold startup time of current cloud service providers is between 100 and 700 milliseconds, depending on the language features. Thanks to Google’s JavaScript engine Just In Time, Node.js is the fastest at cold launches.

Faas warm-up cold start: Since some resources are slower to load during cold start (such as function code download that takes a long time during cold start), once you update your code, the cloud service provider will secretly start scheduling resources to download a mirror image of your code build function instance. When requesting the first access, the cloud service provider can take advantage of the built cache image and directly skip the cold start process of downloading function code and start the container from the image, also known as warm cold start.

In fact, the biggest difference between Faas and application hosted Paas is resource utilization: Faas can shrink to 0, while Paas cannot. Why is that?

  • Faas cold start time is very short;

  • It usually takes tens of seconds to create an application instance. To ensure service availability, you must maintain at least one server running your application instance at all times. (In order to adapt to the diversity of users, application hosting platform PaaS must support multi-language compatibility, and also provide traditional background services, such as MySQL and Redis. This also means that application hosting platform PaaS has a large number of dependencies and multi-language versions to be compatible when initializing the environment. And application code that is compatible with multiple users often adds time to the application build process.)

Resource utilization is further enhanced by layering: Faas instances are executed in at least three layers — containers, runtime, and concrete function code. Think of the container as an operating system, run as a player, and function code as an MP4 file of a movie. (My personal understanding is that such a hierarchical structure decouples the three layers of structure, and the corresponding resources are individually loaded without waiting for the other part to be loaded, which can speed up the cold start time of Faas. I don’t know the details, so I won’t go into details.)

  1. Two Faas process models and application scenarios

The two Faas process models are “use up and destroy” and “resident process”.

“Use or destroy” : when a function instance is ready, the function is terminated immediately after execution. This is the purest use of FaaS.

“Resident process” : After the function instance is ready, it does not finish executing the function, but returns to wait for the next function to be called. It is important to note that even if FaaS are resident processes, the function instance will be destroyed by the cloud service provider if no event is triggered for a period of time. (If the application based on traditional MVC architecture is directly migrated to the Serverless architecture cloud development platform, this process mode can be adopted. My personal understanding is that if the traditional architecture code is not reconstructed with Serverless, the cold startup time will increase. So this mode can barely reduce the unfriendliness caused by a long cold start time.)

The most popular data choreography mode currently used is BFF, but if you migrate to Serverless, SFF can be chosen because the Faas Trigger can be thought of as a data clarity layer.

Similar to data choreography, service choreography is the combination and processing of various services provided by cloud service providers. In our Paas service, functions based on different operating environments can be added. The combination of these functions (NodeJS, Java, PHP, etc.) can increase the flexibility of the entire application, that is, services are no longer limited to a single language and diversified scenarios.

  1. How does Faas rapidly expand and shrink

The default expansion and contraction mode of Faas is horizontal expansion and contraction, that is, to increase the number of machines, and the price is stable.

Note that a node could only be stateless, whereas a node that was stateful was used to store data.

What is with state:

Application state is the data that application components need to do their job (that is, perform a task). From software architecture, coding paradigm to programming language itself, application state is inseparable from the participation, application state essentially describes how to manage an application’s behavior (task, operation, etc.) and state (data). (State: Global variables, such as links to local file storage, long, personal understanding is a single node is different from other similar nodes, so this kind of node is stateful, such as database expansion machine is definitely not the original data of the machine, it needs to put the data on the original machine synchronous to come over, this is time cost in the middle, So can’t arbitrarily expand or shrink)

Baas

Faas avoids operating directly on the database, because this operation requires connecting to the database, which directly slows down the cold start of Serverless. So traditional database operations are encapsulated by the Baas service.

  1. NoOps microservices

Baas service actually uses microservices. The current modules are disintegrated into small functional modules with single responsibilities. The data models among the modules are independent from each other, and the modules use API interfaces to expose themselves to provide services externally.

[bug Mc-108383] – Data operations were stateful.

Additional processes allow the database and replica to be synchronized directly through message queues, so microservice applications can focus only on their own proprietary databases. Microservices made the back-end application Stateless through database decoupling, but the database itself was Stateful. (Personal understanding is that all read and write operations of the current database will be recorded through message queue, which is equivalent to logging. Then, based on message queue, these operations will be synchronized and updated to other expanded database nodes, which will have some time delay. Therefore, the whole Baas service is Stateless from the outside. But it was Stateful.)

  1. The dismantling and merging of business logic

Baas services are based on the concept of microservices, so how to dismantle Baas services can be translated into how to dismantle microservices, here is the DDD idea:

Through the hierarchical abstraction of the business, the domain model is analyzed and defined, and the domain model is used to drive our system design. Finally, the complex business model is disassembled into the domain model of independent operation and maintenance.

After the disassembly, we have to consider the combination. We can chain our microservices, or Faas, together by event sequence through the idea of choreography of event streams.

How do I build applications with Serverless

  1. Serverless Building an application can be done in three steps

Namely: Installation and initialization → configure yML files → deploy

  1. Or if we need to customize our Serverless component dependencies, we need to do the following:

Finally, the Serverless application is initialized and yML files are configured and deployed based on the developed components.

How to design Serverless Architecture (taking Ali as an example)

  1. Focus of the solution
  • Build a separate gateway;

  • Establish an online unified development process platform;

  • Complementing existing Baas capabilities;

  1. Abstract high-frequency scene

  1. Building the R&D process

The traditional development process is to build project templates locally through CLI scaffolding, then build and deploy to an online environment (with some logging, monitoring, debugging capabilities), and the access layer requests proxies through Nginx.

By Serverless, I personally understand that the Serverless process platform is used to initialize the project and then develop it based on the function template of the existing relevant environment on the platform. Meanwhile, in order to unify the runtime environment, the same environment is uniformly constructed through online Docker. Of course, there is no need to manually or automatically configure the Nginx proxy, because based on Serverless, you can directly request access and proxy based on Trigger.

  1. Verify versioning

Each iteration will have code changes, after which the commit on GitLab will have a unique commitId. When the application is deployed, the Serverless platform will first pull the code from a Git branch, and at some point commitId will be pulled. Somehow a route is generated based on commitId, and finally online access is based on the generated route to the deployed project.

  1. Local development supports remote debugging

  1. The gateway

By synchronizing the request context into the access layer, the request context can be used to determine the function to be executed when traffic requests come in.

  1. Baas ability

  1. The domain model

This part of my personal understanding is as follows: there is a lot of coupling in the domain model of the middle and background, and even some independently constructed domain models exist in the domain model, so we need to build functions independently based on these domain models. However, the structure of shopping guide is single. What we need to do is to cut out the data we want, so we can automatically generate functions from the model through a scheme, instead of building corresponding functions for a domain model alone.

Impact of Serverless on the front-end and back-end

What does Serverless bring to the front end

  • Industry positioning changes: before serverless, only UI development is required, and after serverless, the whole application (business) interaction is required;

  • Collaboration and development models, upstream and downstream processes will also change;

What changes does Serverless bring to the back end

  • Provide capability support from lower levels (e.g., focus on Baas service unbundling);

  • The future growth direction will likely be business solution-oriented industry architect;

  • Serverless provides the back end with corresponding opportunities in the Serverless architecture building domain

Practical use of Serverless

Server vendor

At present, Serverless server manufacturers include Tencent Cloud and Ali Cloud in China and Aws in foreign countries.

The open source project

  1. Midway FAAS (github.com/midwayjs/mi…)

Midway FaaS is a Serverless framework for building Node.js cloud functions to help you focus on product development and reduce maintenance costs in the cloud native era.

The graph above describes some of the features and support for Midway FAAS. What attracted me to midway FAAS was its support for the nodeJS framework (EggJS and KoajS), two of the major domestic service providers, as well as support for the mainstream front and back end separation framework.

Its architecture is as follows:

  1. Serverless (github.com/serverless/…).

The Serverless Framework – Quickly deploy your Serverless applications, support event triggering, flexible scaling, and pay on demand. This greatly reduces the cost of building and maintaining applications and allows developers to focus on business logic.

Serverless Framework is a command line tool that uses event-based computing resources such as Tencent Cloud function SCF, AWS Lambda, etc. In addition, the Serverless Framework provides scaffolding, automated workflows, and best practices for developing and deploying the Serverless architecture. And it supports functionality extension through rich plug-ins.

About front-end early chat conference: front-end early chat conference goal to become useful, understand, copy to go of the technology conference, planned in 2020 >= 15, jointly held by front-end early chat and Nuggets, front-end early chat conference schedule dynamic, video recording /PPT/ lecture notes download, please pay attention to the “front-end early chat” public account follow up.

On May 30, held the 7th – front end how to make the front, please stamp: www.huodongxing.com/go/tl7, posters and lecturer itinerary is as follows:

Held on May 31, the eighth – new strategy, front-end job-hopping registration please stamp: www.huodongxing.com/event/25439… , posters and lecturers’ itineraries are as follows: