Go to lists of articles

A Beginner's Guide to Edge and Serverless Functions

As a junior developer, you may have heard of edge functions and serverless functions, but do you know the differences between them? In this article, we'll explore the key characteristics of each type of function and their pros and cons.

What are Serverless Functions?

Serverless functions are pieces of code that run on a serverless computing platform (cloud), such as Azure Functions or AWS Lambda. They are small and self-contained and are executed in response to specific events. If you're using Next.js, the service you use to deploy them is Vercel. When you deploy the application, all the functions from api routes will be used as serverless functions. They can be used for a wide range of tasks, from simple data processing to complex business logic.

One of the main advantages of serverless functions is that they can scale automatically, without the need for manual intervention. This means that if your application experiences a sudden spike in traffic, the serverless function will automatically scale to handle the additional load. Another advantage is pricing. You only pay when the functions are executed, not for upkeep.

However, a disadvantage of serverless functions is the occurrence of cold starts. If you're not calling your function frequently, the service you use will need to create a new instance of the function, which takes time. After a cold start, the function will run faster for some time. High latency is also a common occurrence. Since they are mostly deployed on one server, if, for example, you're using a server from Oregon and your user tries to connect from India, the distance between the server and the client is quite long. That's why you probably shouldn't use them for tasks that require low latency.

What are Edge Functions?

Edge functions, also known as CDN functions, are pieces of code that run on the edge of a content delivery network. What is a CDN you may ask. A CDN is a distributed network of servers that helps deliver content (such as images, videos, and static files) to users faster and more reliably. Edge functions are not deployed only on one server like serverless functions but on many. This means that the function code is executed closer to the end-user, resulting in faster response times and reduced latency. Edge functions can be used to modify or augment content as it is being delivered to the end-user, without the need for additional server infrastructure. We can think of them as serverless functions living a layer closer to the user.

For example, imagine that you're building a web application that displays images. You want to ensure that the images are compressed before they are sent to the user, in order to reduce page load times. With an edge function, you can apply compression to the images as they are being delivered, without the need for additional server infrastructure.

A big disadvantage of edge functions is that, in contrast to serverless functions, which can use different runtimes, they useGoogle's V8 JavaScript engine. This means that they can't do as much as their serverless counterparts. For example, V8 does not support some popular Node.js modules and language features. Additionally, some developers may prefer other languages or runtimes for their functions, which limits their options when using edge functions.

Which One to Choose?

So, which type of function should you choose for your project? It depends on your specific use case and requirements. Edge functions are ideal for simple content manipulation tasks, such as image compression or code stripping. They are executed on the edge of the network, which makes them faster and more responsive.

Serverless functions, on the other hand, are more flexible and can be used for a wider range of tasks. They are executed in response to specific events, which means that they are well-suited for complex business logic or data processing tasks. They also offer automatic scaling, which can save time and resources.

Conclusion

Edge functions and serverless functions are both powerful tools for developers. Edge functions are great for improving performance and reducing latency, while serverless functions are better suited for more complex business logic. By understanding the differences between the two, you can choose the right tool for the job and build better applications.

Bonus

If you read to this point I think you are quite interested in this topic. Here are some resources that go on this topic more deeply.

Lee Robinson (Vercel) and Kelsey Hightower (Google) talking about Edge Functions

Theo (ping.gg) Going on one of his rants about Edge Functions and if they are future of the web dev

How to deliver personalized content at the edge?