Serverless

  • Serverless in broad terms: An architectural idea that builds and runs software without caring about the server. Although Serverless translates to “Serverless”, this does not mean that applications do not need a server to run, but that developers do not need to care about servers. The software architecture based on Serverless is Serverless architecture.

  • At present, the implementation of Serverless is mainly based on FaaS (function as a service) and BaaS (Back-end as a service).

Why is Serverless a combination of Faas + Baas?

Fass

First of all, if we want to implement an interface and make this interface provide services to the outside world, we need a server to deploy our code. Since Serverless doesn’t care about the server, we need Fass.

Function as a Service (FaaS) is essentially a Function running platform. Most FaaS products support Node.js, Python, Java and other programming languages. You can choose your favorite programming language to write functions and run them. When the function is running, you are unaware of the underlying server, and the FaaS product takes care of the scheduling and operation of resources, which is one of its features, no operation and maintenance.

In addition, functions in FaaS do not run continuously, but are triggered by events, such as HTTP events, message events, etc. The source of events is called triggers. FaaS platform integrates these triggers, and we can use them directly, which is the second feature of FaaS, event-driven.

A third feature of FaaS is pay-as-you-go. FaaS charging is based on the number of times a function is executed and the CPU, memory and other resources consumed during execution. In addition, FaaS will automatically generate multiple function instances according to the amount of concurrency when running functions, and the concurrency theory has no upper limit, which is its fourth feature, elastic scaling.

Bass

In essence, BaaS encapsulates back-end functions and provides services in the form of interfaces. In Serverless architecture, common BaaS products include AWS DynamoDB, Ali Cloud table storage, message middleware, etc. These services can be accessed through API.


Other along

  • At first, cloud vendors all sold hardware, such as AWS EC2, Ali Cloud ECS and Azure Virtual Machines. This cloud computing form is also called Infrastructure as a Service (IaaS).

  • With the development of business forms, cloud vendors find that they can abstract some common platforms, such as middleware and database, and sell these functions as services on the cloud. This is Platform as a Service (PaaS).

  • In SaaS (Software as a Service), vendors directly provide Software services and users spend money to buy and use them without designing their own website architecture

  • FaaS provides the ability to run function code with automatic elastic scaling. With FaaS, the composition of our application is no longer a collection of functions, but individual functions. Each function implements its own business logic, which makes up a complex application.

  • BaaS encapsulates back-end capabilities as services and provides services in the form of interfaces. Such as database, file storage and so on. Through the interface of the BaaS platform, our functions running in FaaS can invoke various back-end services, thus implementing complex business logic at a lower development cost.


Advantages and disadvantages of Serverless

advantages

No operation and maintenance, flexible expansion, cost saving, simple development, reduced risk, easy to expand

disadvantages

  • Relying on third-party services
  • Diversity of underlying hardware
  • Application performance bottleneck => Cold startup
  • Function communication is inefficient
  • Complex development and debugging

Function life cycle: cold start and hot start

In the FaaS platform, functions are not run by default and no resources are allocated. Function code is not even saved in FaaS. Only when the FaaS receives the event from the trigger does the function start and run. The whole operation process of the function can be divided into four stages.

  1. Download code: The FaaS platform does not store the code itself. Instead, it stores the code in the object store. When a function needs to be executed, the function code is downloaded and decompressed from the object store.
  2. Start container: After the code is downloaded, FaaS will start the corresponding container according to the function configuration. FaaS uses the container to isolate resources.
  3. Initialize the runtime environment: analyze code dependencies, execute user initialization logic, initialize code outside of entry functions, and so on
  4. Run code: Call the entry function to execute the code.

When the function is first executed, it goes through four complete steps, the first three of which are collectively called “cold start” and the last “hot start”.

Write a simple Serverless application

Here we use AliYun Serverless for demonstration, other platforms have some minor differences, but the essence is the same.

Steps for developing Serverless applications

Let’s write a simple Serverless application that provides an interface accessible to all and responds to request parameters (such as request example.com/?name=Serve… Return Hello, Serverless). You can go directly to the function evaluation console by clicking On New Function and create a new function. Create a new HTTP function and go to the code editing page.

The code is as follows:

exports.handler = (request, response, context) = > {
    // From request
    const { name } = request.queries;
    // Set the HTTP response
    response.setStatusCode(200);
    response.setHeader("Content-Type"."application/json");
    response.send(JSON.stringify({ message: `Hello, ${name}` }));
 }
Copy the code

This is essentially a function that takes three arguments:

• Request is request information from which you can retrieve request parameters; • Response is a response object that you can use to set HTTP response data; • Context is a function context

Save when you’re done, and you’ll see the requested test address below. Input the corresponding parameters for debugging, and find that the right side can return data normally, indicating that our simple Serverless application is written.

subsequent

At this point, a simple Serverless application is realized, compared with the traditional way, this way for the front end, completely get rid of the cost of operation and maintenance, and then take you to achieve a simple login registration, deepen the understanding of Serverless.