2

Introduction

I don't know when, a pattern called Serverless architecture appeared. Look at the English word Serverless, which means there is no service. How to build an application without a service?

After careful study, I found that Serverless does not mean that services are not needed, but that services are built on BaaS or FaaS platforms. Usually suitable for single-page applications or programs that are not responsible for business logic.

Obviously, this serverless architecture was created by cloud vendors, and the purpose is to let you use their services. This is similar to the more popular cloud native recently.

Although this type of architecture eliminates the need to build services in the traditional architecture, it may benefit from significantly reduced operating costs, complexity and project delivery time, but at the cost of increased dependence on suppliers and relatively immature support services.

This article will discuss serverless and the story behind it in detail.

What is serverless

The concept of serverless is undoubtedly put forward by cloud vendors, such as Microsoft, Google, and Amazon are all admirers of serverless, and they are deeply bound and recommended in the services they provide.

So what is serverless?

Serverless can actually describe two states. The first state is those rich clients. For rich clients, business logic can be completed on the client. In the cloud, only database services or authentication services are needed. These types of services are called BaaS.

Another is that server-side logic is still written by application developers, but unlike traditional architectures, it runs in stateless computing containers. These containers are event-triggered, short-lived (may be called only once), and are completely called Called by a third party. This kind of service is called function as a service or FaaS. The most famous is the Lambda service on the cloud, which is now more popular.

serverless example

Simple three-tier service

Next, let's give a few specific examples where serverless can be used to facilitate everyone's understanding.

Consider one of the most common web projects, which provides the function of adding, deleting, modifying and checking. Obviously, we need a client, a server and a database, as shown in the following figure:

<img src="https://img-blog.csdnimg.cn/20210608161800351.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_0,text_aHR0cDovL3d3dy5mbHlkZWFuLmNvbQ==,size_25,color_8F8F8F,t_70" style="zoom:50%;" />

The above picture is an example of the simplest service. We have a client to display the corresponding UI interface. Generally speaking, this client is a browser. There is also a server to receive all client requests and business logic processing. Finally, there is a database used to store the corresponding data.

If the above service is converted to a serverless architecture, how to modify it?

In the serverless architecture, the server is gone and replaced by various FaaS. Then the function of the client will be enhanced to become a rich client, most of the business logic will be carried out on the client, and even in some cases, the database can be read directly from the client.

The business logic that must use the FaaS service needs to be split, as shown in the following figure:

<img src="https://img-blog.csdnimg.cn/20210608162257543.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_0,text_aHR0cDovL3d3dy5mbHlkZWFuLmNvbQ==,size_25,color_8F8F8F,t_70" style="zoom:50%;" />

In the picture above, we use a third-party cloud authentication service for security authentication. At the same time, the client can be directly authorized to query the database for unimportant data.

For the update service, it is still necessary to use the update API provided by FaaS to update the database.

As you can see, the serverless architecture is completely different from the original architecture. The benefit is that the system has become more flexible, and the functions have been re-divided, which reduces the business logic of the server, and has a somewhat distributed effect, and the corresponding server cost is lower.

The disadvantage is that the original service is split into multiple services, and multiple services need to be monitored, and then basically all the data is stored in the cloud, which puts higher requirements on the security capabilities of the service provider. Finally, this flexibility and cost reduction will bring system complexity and increase the difficulty of maintenance.

Message-driven

A common message-driven example is clickstream reporting on the front end. When the user clicks a button on the client side, an interface on the server side will be called. This interface will send the click message to the message queue, and then enable the asynchronous back-end service to fetch the message from the message queue, and finally update the database.

<img src="https://img-blog.csdnimg.cn/20210608164349230.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_0,text_aHR0cDovL3d3dy5mbHlkZWFuLmNvbQ==,size_25,color_8F8F8F,t_70" style="zoom:50%;" />

So how can the above example be implemented with Serverless?

We need to replace the server with FaaS, and replace the asynchronous service with the corresponding FaaS:

<img src="https://img-blog.csdnimg.cn/20210608170417491.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_0,text_aHR0cDovL3d3dy5mbHlkZWFuLmNvbQ==,size_25,color_8F8F8F,t_70" style="zoom:50%;" />

The advantage here is that it can take advantage of the rapid expansion function of FaaS. In the case of a large number of messages, the message processing function can be dynamically expanded, thereby improving the processing speed of the system.

FaaS

We mentioned FaaS many times above, so what exactly is FaaS?

According to its original meaning in English, FaaS is a function as a service. Or you can think of it as Amazon's AWS Lambda service.

AWS Lambda can run without any server. You only need to upload your business code to automatically generate a Lambda service. Then this service can be called externally.

Of course, not requiring a server here means that customers do not need to purchase a server and build services on it. In fact, lambda also needs to run on the server.

FaaS is basically compatible with code written in Javascript, Python, Go and any jvm language, and it can be regenerated as a FaaS service with only a few changes.

Another advantage of FaaS is that it can be scaled horizontally, and this horizontal expansion is completely automatic. This horizontal expansion automatic management is controlled by the operator, and the user does not need to consider the underlying details of the implementation. This horizontal expansion capability is very effective for the peak application of the service at a certain moment.

We only need to design the FaaS function, and leave everything to the cloud vendor to do.

Disadvantages of FaaS

FaaS is stateless, which means that you cannot use local memory variables or local disk data, because FaaS cannot guarantee the validity and durability of these data.

Therefore, the data to be stored needs to be externally persisted.

In addition, due to the limitations of cloud servers, each FaaS call has a maximum timeout period, so FaaS is only suitable for programs that can respond quickly.

In addition, FaaS may need to be initialized when it is started, and the instantiation of this function may cause a delay in the request. Therefore, it is necessary to consider the startup strategy of the cloud provider and make corresponding adjustments.

When we decide to use any outsourcing strategy, you hand over control of part of the system to a third-party supplier. This lack of control may manifest itself as system downtime, unexpected restrictions, cost changes, loss of functionality, mandatory API upgrades, etc.

  • Multi-tenant issues

Multi-tenancy refers to the situation where multiple software instances of multiple different customers (or tenants) run on the same machine, and may run in the same hosted application. This is a strategy for cloud service providers to achieve economies of scale. Service providers do their best to make customers feel that each of them is the only one who uses their system. However, there is no perfect solution that can simultaneously solve the security of multi-tenancy (one customer can see the data of another customer), robust Issues in terms of performance (a bug in one customer’s software causes another customer’s software to malfunction) and performance (a high-load customer).

  • Supplier binding

If you use serverless in one service provider, the cost of switching it to another provider is huge. The corresponding operating tools may need to be updated, and the code may also need to be updated.

Advantages of FaaS

We can regard Serverless as the simplest outsourcing solution. You don't need to manage servers and databases yourself. These can be hosted by cloud vendors.

On the one hand, the investment in infrastructure services has decreased, and on the other hand, the labor cost of maintaining these infrastructures can be saved.

In addition, any performance optimizations you make to the code will not only increase the speed of the application, but they will be directly or indirectly related to reducing operating costs, depending on the service provider’s charging plan. For example, suppose an application initially needs one second to process an event. If you reduce this time to 200 milliseconds through code optimization, you will immediately see an 80% savings in computational cost without any infrastructure changes.

Compared with deploying an entire server, packaging and deploying FaaS functions is simple. All you do is package all the code into a zip file and upload it.

to sum up

The serverless architecture is currently a more popular architecture approach. We can try to use this new architecture approach to see if it can bring different changes to our business. But you also need to see that not all services can use the serverless architecture. We need to weigh it.

This article has been included in http://www.flydean.com/11-serverless-architecture/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com