** This article is from Tencent Cloud Technology Salon. The theme of this salon is Serverless architecture development and SCF deployment practice **
Speaker: Huang Wenjun, used to be responsible for the architecture and development of enterprise-class storage, enterprise-class container platform and other products. Currently, he is mainly responsible for SCF Tencent serverless cloud function products. Has expertise in hot technology areas such as container platforms, microservices architecture, serverless architecture, and DevOps.
Hello, everyone. I would like to introduce myself. At present, I am the product leader of Tencent Cloud serverless cloud function. I did back-end development for many years. Today is a programmer’s view of how we use the Serverless architecture.
I will divide this presentation into several parts: first, introduction to Serverless architecture; Second, the introduction of cloud function products; Third, Serverless usage scenarios.
Before talking about Serverless architecture, we can take a look at the development process of the whole cloud. Before there was no cloud, we may all use physical servers. In the early days, we all use physical machine hosting, purchasing some servers to be hosted in the room. If there is a problem, please ask IDC staff to help you operate. The investment and operation cost of these equipment is still high.
After the advent of the cloud era, due to the application of virtualization technology, we used the cloud host. Cloud host is everyone directly on the cloud to do virtual machine purchase, open can be used. This is what we call IaaS(Infrastructure as a Service), where you don’t have to run a physical machine to do it, you do it directly in the cloud. And then as container technology evolved, we had container platforms, or PaaS (Platform as a Service). After the arrival of the container platform, there is actually a part of the infrastructure operation and maintenance problem, but at this time, the infrastructure gradually sinks to the operation and maintenance personnel to operate; From the perspective of application developers, they don’t have to care about virtual machines, or operating systems. In this case, application developers pay more attention to the usage of computing or storage resources required by the application. Moving forward, we arrived at FaaS (Function as a Service). At this time, operation and maintenance personnel do not need to pay attention to the underlying operation and maintenance, but the ability to run on demand. Business developers can further do business related things.
Let’s take a look at what the Serverless architecture is. Serverless is separated from the use of physical machines or VMS and focuses more on the running status of upper-layer services. The Serverless architecture consists of two parts: function as a service and back-end as a service. Function as a service provides computing power. The original computing power, whether container or virtual machine, is carried on a certain operating system. Function as service abstracts the computing power further, which we will continue to expand later. In addition, Serverless also has back-end as-a-service, such as object storage, database application, cache service, we can also call it Serverless, because these services can also provide openas-a-service, openas-a-use capability in the cloud. When using these products, we also do not need to pay attention to what kind of server it is and where its server is deployed. Instead, the service can be used when it is opened. The following operation and maintenance work is handed over to the cloud, so we do not need to perceive its bottom server, so we can also call it Serverless. This service is called Serverless back-end as a service. Together, these two can be called the Serverless architecture.
How does function as a service work? How is computing power provided on Serverless? You know, if you’re using containers or virtual machines, you upload code to a container or virtual machine, and then you start a process, and the code runs, and it accepts requests from outside, and it responds in real time. Different from the original containers or virtual machines, Serverless implements computing hosting services. The first thing the Serverless users need to do is to submit the code we call cloud functions to the platform for code hosting. The next thing to do is configure the trigger. Why do I need to configure triggers? Because cloud functions run as triggers, the code actually runs when triggers occur. So configuring a trigger means that we give it a trigger source, which defines the event at which the code will actually run. After the user code is hosted to the platform, it is just the code file and configuration store, and the code does not run until the event arrives. Under what conditions? It is when the event trigger actually arrives that the cloud function actually launches an instance, which means a cell. After the cell is pulled up, the event is passed to the cell for processing. If the trigger source has a large number of events and high concurrency, the platform will automatically pull up multiple instances of the same code and configuration for concurrent processing, depending on the accumulation of events or the speed at which events arrive. So you can see that Serverless runs on demand, meaning that the code will only be pulled up and run if an event arrives.
Automatic concurrency means that the cloud function platform will automatically carry out concurrency according to the event accumulation, and automatically pull up multiple instances for processing. However, if the original containers or virtual machines want to perform concurrency, they still need to be manually involved, such as starting more containers or adding more virtual machines to handle high concurrency requests. Functions as services run completely automatically.
Another feature of running on demand is that it doesn’t take up computing resources until the code is up and running. The function-as-a-service charge is also based on running on demand, that is, the function is billed while it is running; And there will be no billing when there is no use. In fact, most Internet services only peak during the day, or even after 6 o ‘clock when people leave work, and there are actually not many requests after midnight, so the function as a service can meet the peaks and troughs very well to cut the peak and fill the valley.
From the above principle, we can see some features of function as a service, such as code hosting, cloud function platform directly provides the running environment, that is, the environment supporting various development languages; For developers or function service consumers, there is no awareness of where the server below it is, but the function platform completes the scheduling of function execution. So you don’t actually need operations, operating system optimization, server maintenance, all of that is hosted by the platform.
Second deployment means that functions run only when they are actually requested. The request only runs meaning that the function will be pulled up and run in real time when the request arrives on the platform. The instance is also returned if there are no subsequent requests after the run is complete.
Since function execution is triggered by events, and there are many types of events, there are various triggers that can interconnect with cloud functions. The more triggers are connected, the more scenarios the cloud functions can provide.
For developers, under the condition of using the cloud function, he really should focus on the business, is to use the code to focus his business logic, such as what kind of logic is to get the incident after the operation, what kind of business is to store, and don’t need to focus on how to use business code to realize high concurrency, how to realize the bearing capacity of the high request. So here we see that functions as a service can bring some convenience to application developers, and automatic concurrency itself is a feature of functions as a service.
As for The serverless cloud function of Tencent Cloud, our goal is the same at the beginning of product development, that is, to host the computing. In the case of computing hosting, we use computing just like we use Tencent cloud object storage. We do not need to care about the operation and maintenance of the bottom layer, and we do not need to care about the security of virtual machines or physical machines. The comparison with object storage also shows that our calculation is based on actual usage. Of course, the cloud function is still in the free period, you can use it anytime.
In terms of usage, the cloud functions themselves, or the function-as-a-service product itself, are simple. We pay more attention to the writing of core code when developing. The core code actually means the real business logic. And there is no need to consider high concurrency in the business logic, because given the computing characteristics of the function as a service, high concurrency requests are handled by multiple instances, so the business code is written to focus on the processing of a single event. Therefore, the core of the first step is to write the core business code, which is what the code is to achieve business. The next step is to configure the trigger mode. Configuring the trigger method is to connect the function code to the trigger source. Docking with other products on the cloud platform, what kind of event is needed, what kind of event is processed, and what kind of logical processing is carried out. After doing such a triggering source docking, the function can run under the condition of event generation.
So, from the whole usage perspective, what you really need to do is two steps: first, write the code, and second, configure the trigger. For the underlying infrastructure, the environment configuration does not need to be worried about.
At present, Tencent cloud functions have supported Python, Nodejs, PHP, Golang, Java and other languages in terms of the running environment.
Next comes triggers, because the more triggers there are, the more scenarios the cloud function can actually use. The triggers we’ve implemented have timing triggers; Tencent cloud object storage service, including file upload, delete and other time; CMQ message queue service; API Gateway services, which is an important trigger for implementing API services through the Serverless architecture; In addition, there is Ckafka, which is the Kafka capability provided by Tencent Cloud. At present kafka is an open source product, we Tencent Cloud packaging it into the cloud, is also compatible with the standard Kafka protocol. Therefore, in many cases, no modifications are required to migrate directly to the Tencent cloud. Kafka itself acts as a messaging carrier, similar to Tencent’s original message queue, where messages execute cloud functions.
In what scenarios can Serverless land? First, API services are most commonly used in Serverless scenarios. You know that when you implement an API service, whether you give an API to a browser application, or to a mobile APP, or to an applet, when you give it to them, you implement it in an API. In order to achieve this have the WEB server receives the connection, docking the backend business code, if you want to be file storage, back-end structured storage, or some cache needs to read and write, you may be behind the application server docking corresponding file storage, structured database, later if you want to use the cache, to receive the corresponding server or corresponding products. How would an existing API service appear if it were to evolve to the Serverless architecture?
Its front-end browser applications, apps, applets, all work seamlessly without changing the API. The API gateway is used to accept the API request, and when the request comes to the API gateway, it is forwarded to the cloud function, triggering the cloud function to execute. The cloud functions run the business logic at execution time. In fact, cloud functions are stateless at runtime, so such state storage also requires some later storage, whether for caching or database use. Therefore, products provided on the cloud can also be connected. Things like file storage can be done with object storage. The same with databases, there are corresponding database products, both structured and unstructured databases have corresponding products to use. Similarly, caching also has corresponding products to do docking. Cloud functions are written in code to read and write directly to the database or cache.
From the point of view of the whole service architecture, we use the API gateway at the front to provide API capabilities, and even further directly provide SDK services, which is more convenient for development. The SDK provides a variety of development languages to make direct API calls. Cloud functions play the role of business logic processing in the middle, and the storage of state data or other business data depends on the file storage or database behind. API services are also the most common form of implementation for Serverless.
The scenarios introduced here are all the actual scenarios used by our customers. Processing of object files is also common in serverless landing scenarios. Object file processing refers to the callback processing after an operation is performed on an object file. Callbacks are typically events that occur after an object file creation or deletion operation. The cloud function can perform subsequent processing after retrieving the event. Here common processing logic is the following, such as image processing, for the image to generate thumbnails of various sizes or cutting, and then stored in the object storage data, and then according to the request of different clients to show different sizes of the picture to the front end.
Batch file packaging. Users can use cloud functions to filter and package files. After uploading a file, event handling can be used to select which files to package and generate compressed files for download.
Log archive analysis, as well as business system callbacks, are also business logic hosted by cloud functions. Such as log file analysis this usage, the user will upload every day on the front end of the application server log archive in the object storage, archive after the trigger function of cloud, the cloud function will pull down the log file for real-time analysis, it can extract these errors in the log number, or other business related content or user attention, And then it extracts the information or statistical information written back to the database, for the user follow-up investigation, use. For example, when some video files generated by users are uploaded to the object storage, the cloud function will be triggered to notify the information of uploaded files to the user’s transcoding system. The video transcoding is converted to different resolutions and then stored. Of course, transcoding is the business system realized by the user itself, which notifies its own business system through callback notification. These are the scenarios where cloud functions are used in conjunction with the Serverless architecture and object storage.
Then there is CKafka message processing. CKafka is mainly used for log storage and log collection. For example, when multiple application servers constantly generate logs, CKafka can write logs to CKafka for archiving and subsequent analysis. The connection between CKafka and the cloud function is triggered by the information CKafka receives. After collecting logs, archived logs are generally stored in the object storage. In this case, CKafka messages are pushed to the cloud function, which then writes these messages to the object store. Some users don’t write to the object store, they write to the database, archive as a database, and it’s the same thing. In some scenarios, messages need to be analyzed, and the keywords in the messages will be analyzed immediately after receiving the messages in real time. If the keywords are captured, the messages will be immediately pushed to another topic of CKafKA to timely send alarms to business and operation and maintenance personnel. This is also a use of serverless, which is the analysis and forwarding of messages.
Message queues are similar to CKafka, but message queues do not collect logs, but decoupled services. Message queue CMQ is a highly reliable financial message queue provided by Tencent Cloud. It usually forwards and processes some business-level messages. With this product, you are actually doing business decoupling. The cloud function here carries the logical processing of the message, and it can conduct business processing on the message as soon as it is received. This business process is the actual business logic, for example, I need to make a judgment according to a certain message, to determine whether it is appropriate, should be subsequent forward, or forward to another business system? This is the logic of business execution.
At the same time, we can also use cloud functions to dispatch messages again and do state transitions. This state transfer is the same as the later message forwarding. It identifies the content in the message and forwards the message according to the content. This scenario is similar to using cloud functions for logical processing, moving it to the appropriate message queue, and then processing. This is the way we’ve seen messages delivered directly with cloud functions.
The last form, which is now quite common, is the use of timer triggers. Originally, scheduled tasks were mostly used in O&M scenarios. However, when the crontab script was used, people usually needed to know whether the script ran successfully and whether the VM was still working. Cloud functions eliminate the reliability problem of using traditional virtual machines or physical machines to write crontab scripts. In the actual scenario where timer trigger is used, there are also several usages: One is service dial test. This is to periodically check whether the service is still working. If there is an exception, alarms can be sent in time, and emails or SMS messages can be sent to tell the operation and maintenance personnel or developers.
The other is scheduled backup. In this case, you can back up the database every day or every two days and export the exported data to a proper place, for example, the object storage. Periodic backup is performed.
Another is timing data calculation. Because some calculations are based on statistics over a period of time, calculated and presented. In the actual scenario, we have internal business of Tencent cloud, which is to conduct periodic data calculation, make statistics every two hours, and then write the statistical data into the database for subsequent business display and business analysis.
Summary: What does the Serverless architecture itself offer users? It actually allows us to focus more on the business code, so we can build the business faster and go live. The speed of development on the Internet is getting faster and faster now, so the expectation is to further accelerate the speed of development and business actually coming online, and improve the ability to iterate. Therefore, using Serverless allows us to bring the business online more quickly, allowing us to implement our ideas more quickly. On-demand use is our business after the online, in the real request, business will be triggered, there will be computing. And if your business to produce the explosive growth, but also do not need to worry about platform or expand with the bearing capacity, because platform provides automatic extension ability, reduced the appeal of the operations, everybody doesn’t have to care about is the underlying things, and operations staff can also be more emphasis on routing and business related operations. These are some of the benefits of the Serverless architecture. As the core of Serverless, the function-as-a-service product is the computational component presented in Serverless. As you can see, it is closely associated with the trigger source and various products or services at the back end. It can be more regarded as the script of the cloud era, similar to the glue. The former trigger source and the back-end of the various storage, data, services are bonded, the real implementation of architecture landing, is the real implementation of business logic landing ability.
Q&A
Q: Cloud functions have unlimited scalability, but the whole system may also have limitations, such as the database and storage behind it. Can I set such an expansion limit?
A: That can be capped. The appropriate upper limit of expectations can now be set by submitting work orders. Extensions can be set to a suitable value in the background, to which concurrent instances can be extended without further extension, to avoid a large number of instance connections causing the back-end database or storage to exceed the connection limit.
Q: What are the development approaches for implementing API services?
A: There are several ways to develop cloud functions to implement API services. One is to do it all in one function, and the path and method resolution are all carried out in the function. This is also a more traditional approach to development. The other approach is to disintegrate, with each function handling a request for an API path and method, which is microservice development.
Functions and calls between functions are also possible. One is to use the cloud API directly, and the other is to use the API gateway wrapped API. Cloud function is triggered to call, in addition to the introduction of many triggers, in the case of not using these triggers, through code or script can also be called through Tencent cloud API.
Q: Is there any guarantee that the function will be executed when the event is fired, that is, when the CMQ event is fired? Because unlike an API gateway, a call to a function can launch it, and the front end is aware of it. But does CMQ, which is thrown on a message queue, guarantee that this function will be executed?
A: Because unlike API gateways, they are synchronous calls. A synchronous call is a call that can be immediately sensed if something goes wrong during execution, whether it’s the platform, such as insufficient resources, insufficient concurrency, or, for example, time out. Things like CMQ or CKafka are asynchronous, which means you don’t know when the message is being executed or the result of the call. There are two ways to solve this problem: one is to output the result after the function is run, and then put the output of the message processing into another message queue, so that your external business system can sense it. Of course, this type of external notification is also asynchronous. Synchronous notification is another kind, that is, the function can call back to its own business API, you can know what the result of the current data processing is through the code, after processing can immediately call back to the API so that the business system receives the processing result.
Q: COS triggers, for example, for video transcoding, may not be processed in 300 seconds. Function Settings are now limited to a maximum of 300 seconds. Is there a solution to this?
A: Why do all cloud platforms set this time roughly within this range? They just don’t want to do too much calculation in cloud functions. Video transcoding is too heavy for computing, while cloud functions provide limited CPU capacity and memory size, which are not suitable for transcoding in cloud functions. In fact, some video services can be used to achieve transcoding, and the cloud function can be used to bridge the two. For example, after the event of object storage is triggered, the cloud function can get the event and transcode by calling the video transcoding service instead of transcoding in the cloud function. Tencent cloud has this service at present, you can try.
For more details, please click the following link:
Serverless architecture. PDF
Serverless: How do I delete a function?
reading
Make the business not aware of the existence of the server — serverless practice based on elastic computing
Use SCF serverless cloud function to back up database periodically
Cloud, college courses, special recommend | tencent technology test team leader, in combination with 8 years experience in detail for you hot and cold separation principle
** This article has been published by Tencent Cloud + community authorized by the author, please click ** for the original article
Search concern public number “cloud plus community”, the first time to obtain technical dry goods, after concern reply 1024 send you a technical course gift package!
Massive technical practice experience, all in the cloud plus community!