Living on the edge

15 min read
January 2024
Living on the edge

Cloudflare workers, Vercel edge functions, Lambda@Edge, Fastly: all these have something common: They utilize edge computing and the edge network.

But first, let's solve the first and most important question:

What is edge computing ?

To stay in the pizza lovers environment, let's imagine this scenario: Picture yourself at a pizza restaurant, deliberating over your order. In the traditional scenario, the waiter takes your order, sends it to the chef in the back, and then delivers the finished pizza to your table.

Now imagine if the situation was this:

Pizza chef

The chef sets up shop right in front of you, preparing your pizza on the spot! Unless you're exceptionally hungry, the slight delay in the first scenario doesn't bother you much—it's just a pizza, after all. However, amplify that scenario to a restaurant as vast as the entire planet, and suddenly having your personal pizza artist right next to you makes a world of difference!

This is exactly what happens with edge computing, the data is processed as close as possible to the user.

Now that we have a clear vision about what edge computing is, let's dive deeper into what was our first question:

What Cloudflare workers, Vercel edge functions, Lambda@Edge etc... have to do with edge computing?

Closer to you

These services have a common thread—they leverage edge computing and the edge network.

Spiderman edge meme

They allow you to run your code as close as possible to the end user, thus reducing delays and improving overall speed.

They give you the possibility to deploy these so called edge functions which let you run code as close as possible to the client.

How? well they distribute your code across various CDNs so that the user can call the closest one.

In the case of Cloudflare workers, your functions are distributed across 300 data centers around the world!

Cloudlfare data center

This allows for instant response time across the whole globe, no matter where the client is.

They work similar to serverless functions but avoiding the cold starts problem because they never scale down to zero.

Although cold starts do occur, their impact is significantly mitigated as these functions operate without the need for a virtual machine to initiate execution.

How much does the blazingly fast world cost?

Indeed, it may sound peculiar, but the cost-effectiveness of services like Cloudflare Workers and Vercel is undeniably impressive.

blazingly fast

For instance, Vercel's hobby plan generously provides 500 thousand free invocations, while Cloudflare Workers, leveraging the edge network, offers a substantial 100 thousand free requests per day.

These unmatched prices are made possible by a combination of strategic factors:

  • Serverless architecture: both Cloudflare Workers and Vercel operate on a serverless architecture, capitalizing on a pay-as-you-use model. This eliminates the need to maintain dedicated servers constantly running, ensuring that users only pay for the resources consumed during actual code execution.
  • Efficient resource utilization: as emphasized earlier, the efficient use of resources is a key element in keeping costs down. The serverless nature of these platforms ensures that users are not billed for idle server time but only for the specific computing power and network resources required to fulfill requests
  • Size: your code size needs to be under a certain limit defined by each provider but which can be further increased at a higher pricing CPU Time: edge functions often have limitations on CPU time per invocation, typically around 10-20 milliseconds. This restriction ensures that the execution time remains minimal, contributing to cost efficiency

It's now clear how these providers can afford to offer such low prices, in fact, it's essential to acknowledge that, while cost-effective, edge functions may come with certain limitations, including code size and CPU time constraints. Nevertheless, for many use cases, the advantages of rapid response times and global scalability far outweigh these limitations, making services like Cloudflare Workers and Vercel exceptionally attractive for developers and businesses alike.

How can I deploy to the Edge?

It is fairly easy, new JS frontend frameworks such as NextJS, SvelteKit, Remix, Fresh etc. allow for an easy on the edge deploy.

You can follow each framework's docs to easily implement this.

On the backend, you can use NextJS app router to deploy vercel edge functions, vercel will treat your normal api routes as edge functions if you export a simple configuration variable for each route you want to be deployed to the edge:

export const runtime = "edge";

This also works for any Vercel compatible framework by exporting the same configuration:

export const config = { runtime: "edge" };

You can also use HonoJS:

HonoJS is a new multi-runtime framework (works on any JavaScript runtime) that allows you to create edge functions that can run on any edge provider, it has a very simple syntax and it's batteries included: Hono has built-in middleware, custom middleware, third-party middleware, and helpers.

However, the sweet taste of edge computing comes with a bitter truth—edge is not always faster, especially when it comes to the backend and database interaction.

Edge is not always faster

What??? After everything we talked about, the edge structure is not faster??!?

Let's take into account a normal application's structure, we have a frontend, a backend and ... A DATABASE!

web application architecture layers

While the frontend benefits from proximity to the user, the backend, and particularly the database, may not experience the same advantage.

You see, if the backend is served on the edge, it is closer to the user but not to the database, and in that case, the edge structure is not suitable, because the link between the database and the backend server needs to be really close as that's where the majority of data exchange is gonna happen (the user only sends one request to the backend while the backend sends a request to the database for each query)

This is EXACTLY why Vercel allows you to opt-into the edge per route, so that you only optimize the routes that do not connect to the database, and that's a very smart configuration.

But, as promised, there's a solution: Edge databases

Edge databases

Edge databases are exactly what you think they are: databases built for the edge.

They allow you to have multi-region instances and also are as cost effective as the edge can be.

Homelander

Here are some options:

  • Turso: An edge-hosted, distributed database based on libSQL, an open-source and open-contribution fork of SQLite. CockroachDB: Autoscales and distributes data across multiple nodes. Focused on high data consistency and integrity. Supports most of Postgres but not stored procedures and extensions
  • Neon: Postgres with separation of storage and compute. Uniquely designed for serverless and works with the native Postgres driver + supports database branching workflows.
  • Cassandra: It's used by big companies such as Discord and Netflix, Cassandra can (and usually does) have multiple nodes. A node represents a single instance of Cassandra (eg: the beginning of 2022, discord had 177 nodes with trillions of messages 🤯)

This website for example is hosted on vercel and is running as a serverless function from washington, you can check it by simply opening the devtools and looking for the X-Vercel-Execution-Region header:

vercel execution region

As you can see, the region is iad1, which refers to us-east-1, Washington D.C

I cannot opt-into edge functions because while remix supports it, my headless CMS (currently Contentful), only allows enterprise-level customers to access Multi-region delivery infrastructure.

You can check out this simple project which is a Remix website that has 4 routes, two of them running on the edge while the other two on a NodeJS runtime and you can see the computing region (on the edge it will be closer to you) and the proxy region (your region).

Conclusion

I hope this exploration of edge computing has been both enlightening and enjoyable for you. Navigating the intricacies of services like Cloudflare Workers, Vercel edge functions, and others reveals a landscape of technological innovation that is reshaping the way we approach data processing and application deployment.

Thank you for joining me on this journey around the edge, hope this was a good read and that you learnt something new and interesting.