All You Need to Know About Serverless Functions and the Edge
nefejames
Posted on November 25, 2022
The technological advancements and digital revolutions of the last century have completely transformed customers' appetites. They expect digital services and platforms to provide them with blazing-fast services and a seamless user experience. Hence, it is essential to build software applications in a way that allows us to meet the expectations and requirements of today’s fast-paced society.
It is vital to utilize today's innovations to build products for the people of today. Serverless functions and the Edge are powerful technologies we can harness to build speed and scale into our applications and software products. They enable us to deliver content and services to people in a fast and cost-effective manner.
In this article, we will learn more about serverless functions and the Edge, their benefits, and how to work with them.
What are Serverless Functions?
Serverless functions are specific and modular pieces of logic that run on the cloud and are designed to perform a single action. With serverless functions, we can use global data centers to run backend code without needing to configure or maintain servers ourselves, hence the term ‘serverless’ functions.
Having said that, technically speaking, servers are still involved, just not ours. Some businesses provide the cloud computing infrastructure that makes this possible, but more on that later. We can deploy serverless functions to multiple regions across the globe to improve latency and availability.
With serverless functions, we can add all kinds of functionality to applications, such as database queries, form submissions, authentication, and more.
What are the Edge and Edge Functions?
The Edge refers to the CDN server or data center closest to the user. We can take serverless functions to the next step by combining them with the Edge to create “Edge functions,” which are serverless functions that run at the Edge.
Edge functions allow us to modify network requests and serve content from the CDN server that is closest to the users who made the requests, no matter where they are around the globe. We can leverage edge functions to bring unique and personalized experiences to users, like showing localized content, displaying relevant ads, and more.
Edge functions enable us to deliver functions not within a broad area but in the location closest to the user. By running at the Edge, edge functions boost the performance of applications by providing blazingly fast execution times and low latency.
Benefits of Serverless Functions
Here are some benefits of serverless functions:
No infrastructure to manage:
Serverless functions eliminate the complexity behind setting up and managing server infrastructure since we don’t need to do that ourselves.Greater productivity
Serverless functions enable us to focus on what matters, writing code and building awesome products. They eliminate the hassle of setting up servers ourselves, meaning we don’t have to spend time picking the right operating system, configuring networks, packing dependencies, handling hardware upgrades, provisioning capacity to run at scale, or any form of DevOps activity. The cloud takes care of that for us behind the scenes, meaning we are free to spend time on priority activities.Easier to scale
As traffic increases or decreases, serverless functions automatically scale up and demand to adjust to the ever-changing rate of usage and avoid downtime.Lower cost
A server-based approach requires tons of manpower, specialized knowledge, and servers to set up, which are expensive, particularly when making provision for high-traffic usage. Serverless functions cost less to operate because we are charged per request platforms charge customers per request and only pay for the resources we consume to run the functions. It also eliminates the costs of idle CPU time or unused storage space because we don’t own any of that infrastructure.Less Code
Since we don’t need to set up the servers ourselves, we also don’t need to write any server-side or backend code. This makes the barrier of entry low for developers because we can add full-stack functionality to applications without needing to know the backend or server side of things.Faster time to market
The serverless approach means we can develop applications and add functionalities faster. This translates to fewer errors, bug fixes, legacy code, and a faster time to market.
Platforms that Support Serverless Functions
As we saw earlier, serverless functions are not truly serverless. They do need servers to function, just not ours, and there are platforms that provide the infrastructure where we can deploy these functions.
These are some frameworks that support serverless functions.
- Vercel exports Next.js API routes as serverless functions. Beyond those, Vercel also supports edge functions.
- With Netlify functions, we can deploy server-side code as API endpoints. It supports functions created with JavaScript, TypeScript, and Go. Like Vercel, Netlify also supports edge functions.
- AWS Lambda is Amazon Web Services’ serverless functions offering, and it is widely adopted by companies. It also has its Edge offering tagged AWS Lambda@Edge. - Microsoft Azure Functions is a serverless environment on Azure. It supports programming languages like C#, JavaScript, Java, Python, TypeScript, etc.
- Google Cloud Functions is Google’s serverless environment. It supports programming languages like JavaScript, Python, and Go.
- IBM Cloud Functions is a Functions-as-a-Service (FaaS) platform for developing lightweight code that executes at scale and on demand.
- Supabase functions can be distributed to 29 geographic region globally, and it is written in TypeScript.
- CloudFlare Workers is Cloudflare’s serverless offering, and it runs on Chrome’s V8 engine under the hood.
- Oracle Cloud Functions supports Python, Go, Java, Node, and other languages.
Frameworks That Support Serverless Functions
These are some frameworks that support serverless functions.
- Next.js provides API routes that we can deploy as serverless functions on Vercel and other platforms. Also, it’s built by Vercel, so it fully supports serverless functions.
- SvelteKit comes with an Endpoints feature for creating serverless functions, and it also has Netlify adapter. With Rich Harris, the creator of Svelte and SvelteKit, being part of the Vercel team, we can hope that SvelteKit will begin to support Vercel functions.
- Remix was recently made an open-source project, and it supports edge and serverless functions.
- Deno deploy enables the deployment of JavaScript, TypeScript, and WebAssembly code at the Edge. In fact, Supabase Functions and Netlify Edge are built with Deno. Also, Deno supports Azure functions.
The Anatomy of a Serverless Function
We’ve looked at a lot of the theoretical aspects surrounding serverless functions. However, let’s also take a brief glimpse of how an edge function looks.
We’ll create a function that adds a record to a Xata database.
import { getXataClient } from "@utils/xata";
const xata = getXataClient();
const handler = async (req, res) => {
const { name, price, imgUrl } = req.body;
await xata.db.Products.create({
name: name,
price: price,
img: imgUrl,
});
res.end();
};
export default handler;
Serverless functions make HTTP requests and then send a response back. They have two arguments, a request (req
), and a response (res
). Basically, we write a function, deploy it to a serverless provider, then we can call that function just like any API using HTTP methods, the fetch API, or our preferred method of fetching data.
Note that the structure of edge functions may look slightly different depending on the platform and provider we use. However, they will generally have little differences and similar functions and behaviors.
Conclusion
Serverless functions and the Edge have had a positive impact on builders and consumers alike. Builders can build faster, more effectively, and cheaper, and consumers can get the best experience no matter where they live.
However, ingenuity is never-ending, and progress is not static, so who knows what next technological innovation will transform the experience of the web and how we build with it
However, ingenuity is never-ending, and progress is not static, so who knows what next technological innovation will transformation the experience of the web and how we build with it.
Posted on November 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.