Home Blog Talks

From MVC to FaaS – How to develop enterprise-grade FaaS applications

2019-04-22

This is the content of my speech at Tencent Cloud Hello, Serverless Salon, reproduced from the blog of the Simple Psychology Technology Team.

Because it is in the form of a speech and contains a large amount of information, it may not be suitable for reading. I will introduce each concept and idea in detail by topic later.

If you have any questions, please leave a message.

Hello everyone, I am Zhu Feng from Simple Psychology. I am very happy to share with you some of Simple Psychology’s practical experience in serverless.

I believe that everyone is here today with expectations and confusion about Serverless. Especially for enterprise-level development, most people may be more confused.

Simple Psychology started experimenting with Serverless in the third quarter of last year. This year it officially switched to Serverless architecture. It took about half a year to develop a complete set of enterprise-level Serverless application frameworks. With the birth and maturity of this framework, we have discovered more and more features of Serverless that distinguish it from monolithic applications or microservices. These features made us realize that although Serverless is not a silver bullet, it can indeed effectively improve development efficiency and reduce subsequent development and maintenance costs.

Today’s speech is mainly divided into three parts:

What is FaaS? Why use FaaS? How to use FaaS?

The previous speeches today have already introduced the process from hardware servers to virtualization and then to serverless, so I will not introduce this process again.

But Serverless is actually divided according to the managed objects, and there are two subsets. One is function hosting service, which is FaaS (Function as a Service). One is application hosting services, often called PaaS (Platform as a Service).

FaaS refers specifically to function services in Serverless services. Developers only need to upload functions, and the service provider is responsible for on-demand execution and horizontal expansion.

Currently, most of the Serverless services provided by cloud service providers are FaaS services, such as Amazon’s Lambda, Tencent Cloud’s cloud functions, etc.

The main PaaS provider is Google’s App Engine.

After understanding the concept of FaaS, let’s take a look at why we should choose FaaS? Compared with single applications, microservice architecture, etc., what are the advantages of FaaS architecture?

I summarized it from three levels:

The first is the development level.

In terms of learning cost, since the basic unit of FaaS is a function, and the function itself is a very pure process-oriented coding method, which is very close to people’s daily expression, so from a technical perspective or a communication perspective, its learning and development The costs are lower.

When multiple people collaborate, it is usually unlikely that multiple people will modify the same function at the same time, so there will be almost no code conflicts, which is very convenient for multi-person collaborative development.

Finally, from the perspective of technical debt, whether it is a single application or a microservice, it is impossible to avoid a large number of difficult-to-handle technical debt problems caused by rapid business growth. FaaS cannot avoid the occurrence of technical debt, but because the granularity of functions is small enough, we can easily find the entry point for reconstruction, carry out green and environmentally friendly construction, and easily perform various degrees of reconstruction work, making the technical debt Reimbursement costs are greatly reduced.

This is also the advantage that attracts me the most in FaaS. Barbaric growth is a stage that every company will go through, and technical debt is also a pain for every developer. Since the MVC framework usually affects the whole body, how to appropriately choose the entry point for reconstruction and control the scope of influence requires not only technology, but also More experience is needed. In contrast, the entry point of FaaS is very free and flexible. With the support of a good framework and tool chain, the scope of influence can be reduced to the function level.

For new businesses full of uncertainty, FaaS will be an architecture that is very easy to get started without worrying too much about technical debt.

At the operation and maintenance level, in addition to the service provider fully providing services such as execution, capacity expansion, security and disaster recovery, because the cloud function itself has the characteristics of independent execution, when a single point of failure occurs, the scope of its impact is relatively controllable.

The cost feature is simply that it is cheap, because it is billed on a pay-as-you-go basis and there is no need to purchase servers, bandwidth, etc. It will also automatically scale during peak times, but currently the performance ramp is relatively slow due to cold start. However, if you are using Tencent Cloud, you can contact customer service to apply to preheat the instance in advance to avoid performance ramping to a certain extent.

At present, FaaS has actually met the basic usable needs at the service provider level. However, in the process of enterprise-level development, due to the lack of framework and best practices, it is difficult to implement. So today I will focus on sharing the specific practical experience of Simple Psychology in FaaS.

This section is divided into three parts, first the knowledge reserve, then the framework, and finally the command line.

FaaS itself is very easy to get started, so in terms of knowledge reserve, I also control it to a level that a junior developer with basic programming knowledge can get started with.

The first is non-FaaS-related knowledge: basic usage of programming languages and business-relevant knowledge.

I won’t talk about business-related knowledge, but let’s talk about the basic usage of programming languages.

Because Simple Psychology uses Node.js, I will use Node.js as an example. The basic usage here refers to the most commonly used knowledge, such as common types and their methods. You need to master such as: how to splice and replace strings, the current time How to get it and so on.

But here it is specially noted that knowledge of the MVC framework is not required. At most, depending on the actual business situation, you may also need to master some built-in libraries (such as reading and writing local files) and third-party libraries (such as mysql, redis, etc.) that need to be used in specific situations.

Next are the basics of FaaS. There are two main concepts: cloud functions and trigger events. Cloud function is the basic unit of FaaS, which is a function responsible for receiving and processing events. For triggering events, different business types may have different emphases. Simple Psychology mainly uses network requests and timing triggers. If you are a company with data processing as its core business, you may use more events related to message queues, so I will not go into details here.

Now that you understand the basics of FaaS, next comes the basics required for the Cloud Functions framework.

Let me first explain that this framework is currently only used internally by Simple Psychology. To get started with this framework, you need to understand three abstract concepts: naming rules, templates and plug-ins.

I will briefly introduce these three concepts first, and then introduce each concept in more detail.

The first is the naming rule. The role of the naming rule is to manage a large number of cloud resources. This cloud resource includes cloud functions, cloud message queues, cloud gateway interfaces, etc. Through naming rules, these cloud services are integrated for development and use.

Then there are templates. In order to simplify development work and improve development efficiency, we abstract some common business processes and make them into templates. Developers can use templates to generate the cloud resources required for business processes.

Plug-ins are encapsulation of common technologies, such as databases, third-party interfaces, etc. Of course, we also support the direct use of third-party npm packages.

When operating according to the best practice of one cloud function only processing one business process, the first problem we encounter is that a complex enterprise-level application must contain many business processes. How to manage these cloud functions? In addition, FaaS, as a cloud-native architecture, will definitely interact with various other cloud services. How to interact and manage these cloud services?

In order to solve these two problems, we designed a set of naming rules that cover cloud functions, message queues, and gateway interfaces, integrating these commonly used cloud resources.

The naming rules of cloud functions are <environment><type>--<namespace>--<resource>--<behavior>

The naming rule of the message queue is <environment>--<namespace>--<resource>--<behavior>. The message queue does not contain a type because the message queue will be shared in this behavior. The specific details will be discussed later in the template. Will talk about it.

The naming rule of the gateway interface is /<namespace>/<resource>/<behavior>. Because different environments use different gateways, the naming rules of the gateway do not include the environment.

Next, let’s talk about the meaning of these variables:

The first is the environment. We distinguish two environments, the formal environment and the test environment.

Next is the type. We classify cloud functions according to their triggering conditions. There are two broad categories: execution functions and trigger functions. A trigger function is a cloud function that is evoked by a trigger condition.

In order to facilitate sharing, the trigger function usually does not execute business logic. Instead, the execution function is called through the Tencent Cloud interface, and the execution function executes the business logic.

Both scheduling functions and gateway functions are trigger functions. Scheduling functions are generally triggered by timed triggers, and gateway functions are triggered by network requests. In addition, Simple Psychology has multiple gateways, so we actually distinguish multiple types of gateway functions, but due to the specific circumstances of our company’s business, we will not expand on them here.

In addition, for gateway cloud functions, we will automatically create the corresponding gateway interface, and the request path will be generated according to the rules of the gateway interface above. The scheduling function will have a corresponding message queue, and the naming rules of the message queue are also generated as mentioned above.

Namespaces, resources, and behaviors are basically designed in the style of Restful. The namespace is to add a layer of naming hierarchy to facilitate the management of complex applications.

Because there is a command line, you only need to know the naming rules. If the entered function name does not comply with the rules, there will be a prompt on the command line. Before we actually start development, we will first discuss the name of the new cloud function and then start development.

Next let’s talk about templates.

Since in enterprise-level applications, one cloud function is usually not enough to complete a more complex business process, various usage scenarios require multiple cloud functions to be completed collaboratively, but creating cloud functions one by one will be very cumbersome, so we designed Template mechanism to optimize development experience.

Before introducing the details of templates, let us first understand from a novice’s perspective how to quickly find templates that are consistent with business processes.

We designed four questions to quickly locate suitable templates. Questions are as follows:

Is it triggered by the gateway in the first place? To make it easier to understand, gateway triggering can also be considered as being triggered by user behavior. Because generally speaking, most business processes are initiated by user behavior, so if Yes is selected here, the first cloud function in the final recommended template will be a gateway function.

The second question is: does it need to be executed immediately? We have an internal agreement that unless it is a business process that purely reads data, try not to execute it immediately. On the surface this will result in reduced performance, but in the long run it will make subsequent feature expansion easier. And because there are ready-made templates, not executing them immediately will not lead to greater development workload.

The third question is: can it be executed asynchronously? For business processes that are not executed immediately, you can choose to execute them synchronously or asynchronously. Synchronous execution refers to the case of transactional operations such as calculating account balances.

At the same time, it is also specially stated here that in order to simplify the development logic, we prohibit the use of distributed transactions. For a single transaction operation, it is completed through synchronous execution process. For transactions that combine multiple operations, the method we use is to string together multiple synchronous execution processes. For performance problems that may arise, an allocation step can be added as appropriate. For example, when calculating the balance, because the balance is actually calculated separately for different accounts, it can be divided into ten synchronous processing processes based on the last number of the account ID from 0 to 9. Let me mention it again. On the one hand, this is to ensure that the code and business processes are clear and easy to understand. On the other hand, it is also to ensure that junior programmers can also master this model.

The last question is whether the amount of concurrency needs to be controlled for asynchronous execution? Do you still remember the scheduling function mentioned on the previous page? The scheduling function has two uses, one is for synchronous execution, and the other is to control the concurrency of asynchronous execution. Why should we control the amount of concurrency? A typical reason is the shifting peaks and flat valleys. For example, some operations that place a high load on the database can prevent the database from running at full capacity by controlling the amount of concurrency. Or during major promotions, some non-core business processes can be temporarily reduced or even suspended to ensure the stable operation of core business, and the backlog of events can be processed after the peak period has passed.

Now let’s review these four questions as a whole:

Whether triggered by a gateway will determine whether a gateway function is required. Immediate execution determines whether just a gateway function or another type of trigger function is sufficient. Synchronous execution or asynchronous execution to control the amount of concurrency requires a scheduling function. For non-immediate execution, an execution function is required.

Novices can get a clear template by answering these four questions, which will contain 1-3 cloud functions.

When novices know which template to use, they can actually start development. However, everyone present will not be satisfied with this, so I will talk about templates in more depth.

Let’s talk about the template type first.

Templates are divided into two categories: general templates and special templates. As mentioned earlier, how to choose a template, the final template is the basic template and combined template among the general templates.

Each function has a corresponding basic template, which specifically optimizes the development experience of this type of function. Some specific templates will be shown later, so I won’t go into details now.

A combined template means that one template contains multiple other templates and will generate multiple cloud functions.

Extension templates are templates for some specific purposes, or components of combined templates.

At the same time, in order to facilitate development, we have also designed dedicated templates to develop specific templates under various namespaces and resources without disturbing the general template.

Compared with templates, plug-ins are much simpler, and only general plug-ins and special plug-ins are distinguished. Common plug-ins such as database request encapsulation, gateway request encapsulation, etc.

Plug-ins can be used independently, reference each other, or be referenced by templates. Therefore, some cross-template function encapsulation will also be split into plug-ins.

We mainly use a combination of templates and plug-ins to greatly simplify daily development work.

The command line is a tool for quickly processing the entire life cycle of cloud functions.

Let me briefly introduce the functions of these command lines.

Create Cloud Function is a command for creating a cloud function based on a template.

The add plug-in command is used to add plug-ins to the specified cloud function later.

Updating cloud function templates and plug-ins is used to update existing cloud function templates and plug-ins to the latest version. It is generally used when a certain cloud function requires subsequent development and maintenance.

The test cloud function command is our test box based on Node.js packaged in Jest, it supports three test modes: local test environment testing, remote test environment testing and remote formal environment testing.

The so-called remote testing refers to directly calling Tencent Cloud’s interface to trigger cloud functions to verify whether the returned results are correct. If it is a gateway type cloud function, it will directly request the gateway interface for testing.

Publishing cloud function instructions, in addition to publishing and updating cloud functions, can also publish and update the corresponding gateway interface and message queue at the same time.

The two batch functions are because during actual development, a new function usually has multiple business processes, and batch testing and release can save a lot of command lines.

Since FaaS itself provides such a good bottom layer, when will the framework level be perfected? The answer is not to wait a few more years, but this year :P

I am developing a new FaaS framework based on simple psychology’s FaaS practical experience. This framework will be based on Node.js and developed using TypeScript.

Integrated development environment: single application development experience + complete process management from local development to online troubleshooting.

This mainly solves two pain points: first, the development experience of trivial cloud functions is poor, and second, when an online fault involves multiple cloud functions, it is complicated to trace.

Progressive development experience: from hello world to complex enterprise business disassembly; rapid development while maintaining low technical debt and extremely low refactoring costs.

This is mainly so that you can build the project in a relatively simple way at the beginning. Later, you can use some auxiliary tools to rename these initial cloud functions so that the project can grow healthily.

Flexible plug-in mechanism: from public cloud service plug-ins to enterprise-customized private cloud services; from general business plug-ins to enterprise-customized business plug-ins.

Various functions will be provided in the new framework in the form of plug-ins, and the template mechanism will also be incorporated into the plug-in mechanism.

Designed for multi-person collaboration, multi-team collaboration and cloud development. The multi-person collaboration feature comes with FaaS, so I won’t go into details about it. I personally think that cloud development is the future trend. Just like I just demonstrated with Coding Studio, newcomers only need to register an account and assign permissions, and they can get started immediately. There is no need to configure the local environment or the operating system of the computer. It doesn’t matter, it doesn’t even matter if you use a computer or not.

Back to all posts