---
# System prepended metadata

title: Serverless vs Edge Functions

---

# Serverless vs Edge Functions

In this comparison article, we'll take a deep look into the strengths and weaknesses of serverless and edge functions for web development. For a long time, serverless functions have been the norm for developers to write programmatic functions on a server without managing anything related to the server it is running on. Then came Edge functions in 2019, a newer version of serverless functions run at the edge. When we say "run at the edge," we mean that these functions are run at a server closest to the function caller.

A serverless function is a type of cloud computing service that allows a developer to run code without managing or maintaining a server's underlying infrastructure. It is an alternative to a traditional server-side application and is typically invoked in response to an event, such as a user action.

An edge function is another type of serverless function, but its code is run closer to the end user. It operates on the edge of the cloud and utilizes the computing power of connected devices or services within the cloud. Edge functions are designed to provide faster and more efficient computing operations by allowing data processing and analysis to happen where the data is generated, rather than sending the data to the cloud for processing. 

Serverless and edge functions are both supported by cloud platforms such as [Amazon Web Services(AWS)](https://aws.amazon.com), [Microsoft Azure](https://azure.microsoft.com), [IBM Cloud](https://cloud.ibm.com), [Google Cloud](https://cloud.google.com), [Oracle Cloud](https://oracle.com), and more.

Edge functions claim to have lower latency and improved performance compared to traditional serverless functions. Today, we're going to find out if there is actually truth to these claims.

## Use Cases

Serverless and edge functions offer similar use cases, such as the IoT, image and video processing, real-time data analytics, backend for mobile and web apps, and chatbots. However, specific scenarios exist in which each technology excels against the other.

Serverless functions are best suited for event-driven applications that require rapid scaling and minimal infrastructure management. This makes it popular for applications such as image and video processing, IoT, and real-time data analytics.

On the other hand, edge functions are ideal for applications that require low latency and high bandwidth, such as gaming, augmented reality, and live streaming. They are also useful for applications that involve working with sensitive data, as they allow for data processing to take place closer to the source, reducing the need for data transfer.

## Scalability

Serverless functions take the lead in terms of scalability because most like AWS Lambda, Azure Functions, and Google Cloud Functions run on a cloud-based infrastructure with a large number of servers that allow them to automatically scale up and down based on their given workload.

Edge functions run on edge devices like gateways, routers, or IoT devices, which are limited in number and resources. Unlike serverless functions, which automatically scale up and down based on the workload, scaling edge functions requires manual intervention.

## Latency Speeds

In this section, we'll put both functions' latency speeds to the test using [Postman](https://postman.com) and [APImetrics](https://apimetrics.io). Postman is an API platform that can easily be used to build and test APIs. When testing with Postman, it will tell us the exact time it took for our call to get to the function and back to our local computer. Since your application will likely be used by people worldwide, we'll also make calls with APImetrics to better benchmark the latency speeds in other countries.

I have a [NextJS](https://nextjs.org) project created with two folders named “serverless” and “edge” in NEXT's default api folder. Each contains their respective function type.

```
latencytest
      ├── pages
      │   └── api 
      │       └── serverless
      │           └── hello.js
      │       └── edge
      │           └── hello.js
```

NEXT is a popular React framework for deploying server-side rendered applications. It is well known that files in its api folders are deployed as serverless functions. However, NEXT has added a new runtime feature that allows functions in the api folder to be deployed on the edge.

Inside the hello.js of the serverless folder is the following code:

export default function handler(req, res) {
      res.status(200).json({ message: "Hello Serverless!" });
    }
    

And inside the hello.js of the edge folder is:

```
import { NextResponse } from "next/server";
    export default async function handler() {
    return NextResponse.json({ message: "Hello Edge!" });
    }
    export const config = {
      runtime: "experimental-edge",
    };
```

After deploying both functions with `vercel deploy`, the latency speed of the serverless function was around 287 ms and the latency speed of the edge function was around 167 ms from my local computer.


![SERVERLESS: “latencytest.vercel.app/api/serverless/hello”](https://paper-attachments.dropboxusercontent.com/s_2486868F10C02FEFDFF4F192FE520D8967272EC4F5255FC1C64B496FE995E286_1674157507065_Screenshot+2023-01-19+175516.jpg)

![EDGE: “latencytest.vercel.app/api/edge/hello”](https://paper-attachments.dropboxusercontent.com/s_2486868F10C02FEFDFF4F192FE520D8967272EC4F5255FC1C64B496FE995E286_1674157370306_Screenshot+2023-01-19+180241.jpg)


Let's see how both functions perform when called from different countries. The results shown below were derived with [APImetrics](https://apimetrics.io):

| **Country**   | **Serverless** | **Edge** |
| ------------- | -------------- | -------- |
| United States | 111 ms         | 93 ms    |
| Japan         | 96 ms          | 86 ms    |
| Singapore     | 95 ms          | 90 ms    |
| Australia     | 101 ms         | 89 ms    |
| South Africa  | 119 ms         | 62 ms    |

Edge functions definitely have the advantage in terms of latency speed. Edge functions are also less likely to experience the “cold start” issue common with serverless functions that lead to delayed api responses.

## Integration with other Tech Stacks

When it comes to integrating with other tech stacks, both serverless and edge functions have their own set of advantages and limitations. 

Edge functions are designed to run on specific edge devices like gateways for example. As a result, edge functions are tightly integrated with the underlying hardware and are challenging to integrate with other technologies. However, they can be integrated with CDNs, DNS, and Firewalls as long as they running on the same edge devices.

Serverless functions are designed to be platform-agnostic and can easily integrate with a wider range of other technologies, such as databases, messaging queues, and other cloud services. This makes them a good fit for organizations with various tech stacks that need to integrate with multiple systems. Overall, serverless functions are more flexible when integrating with other technologies.

## Development and Deployment

Serverless functions offer a more streamlined development process. This is because they abstract away more of the underlying server infrastructure.

When working with edge functions, developers need to consider the constraint and limitations of the edge devices running the function because edge functions often require more manual intervention and configuration for deployment.

## Final Verdict

While edge functions do, in fact, offer lower latency and improved performance, serverless functions provide greater scalability, easier integration, and development experience than edge functions. Edge functions are best suited for applications that require low latency and high bandwidth.

If you need a more scalable solution with more out-of-the-box integrations, serverless functions might be the way to go. On the other hand, if you need low latency and high bandwidth for your application, edge functions are a better option. You can have the best of both worlds by creating a hybrid serverless application. For example, both functions can be used for an e-commerce site. Serverless functions can handle the payment processing while edge functions can handle real-time inventory management and delivery tracking. Another example is an online game that uses edge functions for its game logic and physics simulations while utilizing serverless functions for user account management and leaderboard functionality.

