Serverless at the Edge: A Quick Overview
Tags: serverless, webdev, cloud, performance
Published: 2022-07-26 15:17:30

One of the most exciting topics in frontend development recently is edge computing. Being able to run code at the edge is a new capability that will radically change how we write applications. This article aims to provide a brief overview of serverless edge computing, why it is beneficial, and how you can use it.

## What is Serverless Edge Computing?

Until recently, when we talked about the edge, we usually thought about content delivery networks (CDNs), which cache static assets such as HTML, CSS files, images, videos, etc. across hundreds of servers around the world. Because the contents are close to users, access is very fast. Serverless edge computing extends this idea by allowing developers to run code at the edge without having to manage any server. Edge computing gives developers a new location for their code along with the two traditional options, namely on the backend and on the device.

## What Are the Most Notable Edge Computing Platforms?

The two platforms that are most talked about these days are [Cloudflare Workers](https://workers.cloudflare.com) and [Deno Deploy](https://deno.com/deploy). At the high level, they have many similarities:

* Both are JavaScript runtimes that use the V8 engine. Deno has a built-in compiler that allows it to treat TypeScript as a first language. With Cloudflare Workers, you can also write code in TypeScript or any other [language that compiles to JavaScript](https://developers.cloudflare.com/workers/platform/languages) such as Python, PHP, Dart, or Scala.

* Both [support](https://deno.land/manual/webassembly#using-webassembly) [WebAssembly](https://blog.cloudflare.com/announcing-wasi-on-workers) which means you can run code written in compiled languages such as C, C++, or Rust at near-native speed.

* Both runtimes rely on [V8](https://developers.cloudflare.com/workers/learning/how-workers-works#isolates) [isolates](https://deno.com/blog/deploy-beta4) for multi-tenancy.

* Both implement [web](https://developers.cloudflare.com/workers/runtime-apis) [standards](https://deno.land/manual/runtime/web_platform_apis) such as Fetch API, Streams API and so on.

* Similar API in which you need to write a [handler](https://developers.cloudflare.com/workers#playground) [function](https://deno.com/deploy/docs/hello-world) that takes a request and returns a response asynchronously.

* [Both](https://developers.cloudflare.com/workers/learning/using-websockets) [support](https://deno.com/blog/deploy-streams) streaming and WebSockets.

On the other hand, the most apparent difference between the two platforms is that Deno Deploy doesn’t offer any storage solution while Cloudflare Workers has [Durable Objects](https://developers.cloudflare.com/workers/learning/using-durable-objects) for consistent storage and [Workers KV](https://developers.cloudflare.com/workers/learning/how-kv-works) for low-latency storage with eventual consistency.

## Why Serverless Edge Computing?

Edge computing is most often used for implementing middleware which is simple scripts that rewrite URLs, redirect requests, or manipulate cookies and headers. For example, you can perform A/B testing by serving different versions of a page depending on a cookie’s value. You can support localization, which means users from different countries get to see different content. You can also grant or block access to certain pages based on user authentication status.

While the above use cases could be handled by the backend and/or the client, using edge computing is much more optimal in terms of performance and simplicity. If you implement A/B testing or localization on the client, you might have a large-sized bundle and a flickering UI. On the other hand, implementing this functionality at the backend usually requires server configuration and results in slow responses.

Edge computing is so much more than middleware though. In fact, you can even use edge computing to replace your backend altogether. Many full-stack frameworks, for example, Remix and Fresh, now allow you to write functions that access databases directly and render HTML at the edge. By doing so, you probably won’t need to develop an API like you usually did in the past. (But should you want to serve a RESTful API or even a GraphQL API at the edge, you could do that as well.)

But why should you consider replacing your backend with serverless edge computing? There are several reasons for that. First, because it’s serverless, there is no server maintenance nor autoscaling to worry about. Second, it’s much faster than traditional backends because it runs very close to users and, compared to normal Function-as-a-Service platforms, edge computing has [little](https://deno.com/blog/deploy-beta1#fast-deployments) or [no cold starts](https://blog.cloudflare.com/eliminating-cold-starts-with-cloudflare-workers). And third, it is cheaper than conventional serverless options because edge computing platforms use lighter-weight technologies, such as [V8 isolates](https://developers.cloudflare.com/workers/learning/how-workers-works#isolates) (as opposed to virtual machines or containers) for multi-tenancy, which allows them to utilize resources more efficiently and handle a lot more requests.

## How to Use Edge Computing?

If you are using a platform such as Netlify or Vercel to deploy web applications, it’s very simple to opt-in to edge computing. For example, you can use Netlify’s [Edge Functions](https://docs.netlify.com/netlify-labs/experimental-features/edge-functions) (which is built on Deno Deploy) or Vercel’s [Edge Middleware](https://vercel.com/docs/concepts/functions/edge-middleware) (which probably runs atop Cloudflare Workers). When you deploy a Next.js application (since version 12.2) to either Vercel or Netlify, you can configure some or all pages to use the edge runtime for server rendering (this capability is called [Edge SSR](https://nextjs.org/blog/next-12-2#edge-server-rendering-experimental)).

If you don’t deploy to Netlify, Vercel, or a similar platform, it is advisable that you use an edge-native full stack framework and deploy your application directly to the edge computing service of choice. For example, you can develop your app with the [Fresh framework](https://fresh.deno.dev) and publish it to Deno Deploy. Another example is the [Remix framework](https://remix.run) which provides adapters for both Cloudflare Workers and Deno Deploy (together with many other deployment targets).

If you don’t want to use a full stack framework, you can develop your frontend and backend separately. With Cloudflare, you can serve your frontend and static assets with [Pages](https://pages.cloudflare.com) or [Workers Sites](https://developers.cloudflare.com/workers/platform/sites). With Deno Deploy, you can [serve static assets from the filesystem](https://deno.com/blog/deploy-static-files). As for the backend, you probably want to use an HTTP framework such as [Sunder](https://sunderjs.com) (if you target Cloudflare Workers), [oak](https://oakserver.github.io/oak), [Router](https://crux.land/router@0.0.5), or [Sift](https://github.com/satyarohith/sift) (if you target Deno Deploy).

## What Are the Drawbacks of Edge Computing?

While edge computing is perfect for implementing middleware scripts, if you want to implement the whole backend with it, you should be aware of the target platform’s limits on CPU runtime, memory, package size and so on. You can find detailed information on Cloudflare Workers’ limits [here](https://developers.cloudflare.com/workers/platform/limits), and Deno Deploy [here](https://deno.com/deploy/docs/pricing-and-limits).

## Conclusion

In summary, with edge computing platforms such as Cloudflare Workers and Deno Deploy, developers can write middleware and backends that are both fast and cheap. Edge computing is a game changer which has enabled a new generation of full-stack frameworks such as Remix and Fresh. You can use these frameworks to develop and deploy applications directly to the edge. Additionally, edge computing can be used indirectly through platforms like Netlify and Vercel.

It's just the beginning. In the near future, we can expect a lot more frameworks, libraries, and tools to be developed for the edge. Hopefully, this article has piqued your interest and given you some high-level information on edge computing so you can get started exploring its potential.