In recent years, serverless functions (sometimes referred to as "serverless" or "serverless computing") have become a popular technology. However, there is still a lot of confusion about this term. How to run code without a server? What are the advantages and disadvantages of this technology? Under what circumstances can it be used? In this article, I hope to answer these questions and provide you with a good overview of the technology.
Key Points
What is a serverless function?
The first time I heard the term "serverless" would surely arouse curiosity. "How to run code on the web without a server?" you might think. What it actually means is that as a developer, you don't have to worry about the server where the code runs. Hardware configuration, network configuration, software installation and extension are all abstracted by serverless providers.
From a development point of view, serverless functions are code packages that you upload to a serverless provider such as AWS or Google. This code can be configured to respond to requests via URL, run as scheduled (i.e. through cron jobs), or call from other services or serverless functions.
Serverless functions are ideal for adding backend functionality to front-end applications without the complexity and cost of running a full server.
At the other extreme, you can also build the entire application using serverless functions. Combined with other cloud services that provide file storage, database systems, and authentication, large, robust and scalable applications can be built without configuring a single server.
Advantages of serverless functions
The serverless function runs in a mini container that is started on demand. They are designed for fairly short runs, so billing is subject to this. Unlike full server instances that are usually billed by hour, serverless functions are usually billed in GB seconds. Since the shortest billing time is about milliseconds, low-frequency or sporadic workloads run as serverless functions much less expensive than traditional server instances. Lightweight workloads and prototyping may even fall under the free tier of some providers.
On-demand calls of serverless functions mean they can be scaled quickly and easily without the need for developers to do extra work. This makes them ideal for situations where traffic may proliferate unpredictably, as more function instances will be automatically provided to handle the load. After that, the function will scale down, meaning you don't have to pay for unused capacity.
A major advantage of the serverless model is that it does not require server processing. Running a web application requires a lot of time and server management expertise to keep the software up to date with security patches and ensure that the server is properly configured for security and high performance. For start-ups and small businesses, hiring people to handle server management is a huge extra overhead. With serverless, developers can focus on creating solutions.
Disadvantages of serverless functions
Of course, no technology is perfect, and serverless functions have their shortcomings. As I mentioned earlier, the design of the serverless model is short-lived. Because the maximum execution time is in minutes (for example, 15 minutes on AWS and 9 minutes on Google), it is not suitable for long-running jobs, such as processing large amounts of data.
Another widely discussed issue is cold start time. This is the time it takes for a provider to configure and initialize its container before the serverless function is ready to start running. After the function is run, the container will be kept for a while to be reused when the code is executed again. This "cold start" delay may add a delay of half a second to one second to a function's response time. There are some workarounds, including the WarmUp plugin for the Serverless framework, which pings your functions as planned to keep the container active.
While serverless functions allow you to avoid worrying about server configuration and maintenance, this does not mean there is no learning curve. Building applications using serverless requires a different mindset than using traditional monolithic code bases. You have to build your code in different ways, breaking the functionality into smaller, more independent services to accommodate the limitations of serverless functions. Deployment is also more complex, as each function is independently versioned and updated.
Sometimes there is also a reference to vendor lock-in issues, which is a disadvantage of serverless technology. As of now, the major providers in this field (AWS, Google, Azure) have their own different implementation and management tools. This can make it difficult to migrate serverless applications from one cloud provider to another. Projects such as Serverless Framework attempt to abstract underlying services so that applications can be ported between providers.
Serverless function use case
While serverless functions can be used to build entire applications, let's look at some less ambitious use cases where serverless can benefit the average developer.
Websites are usually completely static, except for the contact form that customers want to email to users when they click to send. The hosting provider of the website may or may not support server-side scripting, and even if it is supported, it may not be a language you are familiar with. Setting the serverless function as a form mailer allows you to add this feature to a statically hosted website.
Sometimes you may need to run scheduled tasks in the background. Typically, you have to pay to set up the server for the cron job, and this server is idle between jobs. With serverless functions, you only pay for the time it takes for the job to run (if it is within the free tier, you may not pay at all).
Suppose your React application allows users to upload photos to use as avatars throughout the application. You want to resize the uploaded image so that you don't waste bandwidth by providing a much larger image than you need. Serverless functions can be used to process upload requests, resize images to desired sizes and save them to services such as S3 or Google Storage.
Practical example of serverless function
To gain a deeper understanding of how serverless functions work, let's look at a real example. We will create a static page with a press release sign-up form that uses a serverless function to save the user's name and email address to Google Spreadsheets.
Depending on the provider, serverless functions can be written in multiple languages, but we will use JavaScript because Netlify supports Node.js functions. To continue learning, I assume you have the latest version of Node/npm installed on your local computer.
(The following steps are the same as the original example. In order to maintain consistency, no repeated translations will be made here.)
Serverless: Just a fashion, or a future for the backend?
Serverless is also denounced as a fashion and is hailed as the future of backend applications. Amazon's Lambda functions have been around since 2014 and are a key product for AWS. Of course, in many cases, the flexibility and functionality of a real server running 24/7 with full shell access is still required.
However, as we have seen, the low cost of serverless, scalability, and low maintenance costs make it a good choice for some types of workloads. With the advent of more and more books, courses, frameworks and services in the serverless ecosystem, it is safe to say that serverless functions will exist for a long time.
(The following FAQ part is the same as the original example. In order to maintain consistency, no repeated translations will be made here.)
The above is the detailed content of Serverless Functions: A Guide to Usage and Deployment. For more information, please follow other related articles on the PHP Chinese website!