Connect database from the edge
Tags: deno, database, cloudflareworkers, vercel
Published: 2023-08-28 05:17:56

# What is edge 

Cloud is not a new thing, you needn't worry about the infrastructure on the cloud. For example, if you want to deploy your code in us-west-2. You don't need to buy a machine and configure the network for it. Just pay for an EC2 in us-west-2 and deploy your code on that is all what you need to do. But wait, is this a real `Cloud`?

Why do I need to buy a EC2 first? Image that you can write and deploy your code directly without a server. Here comes the serverless.

Based on the serverless, why do I need to pay attention to where the codes are running? Image the code can not only run in us-west-2, but it also runs everywhere - closer to the code triggers. Here comes the edge.

Cloudflare Workers, Vercel Edge Function, and Netlify Edge Function all provide the edge, which allows you to implement low-latency HTTP API without any other infrastructure. And your code can run closer to your customer.

> Edge is actually edge computing, which is a networking philosophy focused on bringing computing as close to the source of data as possible in order to reduce latency and bandwidth use.

# Why it is hard to connect the database at the edge

Before talking about `why it is hard`. We'd better learn how the edge works.

There are two important parts of edge:

1. Edge network: Your code runs everywhere, which means your code will be copied into a bunch of nodes of a a global network (edge network).
2. Edge runtime: The runtime of your code. For example, Java runs on JVM, javascript runs on Node.

Whether you can connect to the database depends on the edge runtime. 

| provider   | edge runtime       |
| ---------- | ------------------ |
| Cloudflare | Cloudflare Workers |
| Vercel     | Cloudflare Workers |
| Netlify    | Deno               |
| Supbase    | Deno               |

Cloudflare Workers is written in JavaScript, and executed using the V8 JavaScript engine (from Google Chrome). Deno also provides edge runtime beyond V8 JavaScript engine. The main reason why they don't choose to build on top of node.js lies in the security —— node is not designed to be a sandbox.

Thus, Cloudflare Workers and Deno don't support all the node.js APIs that are used by the database JavaScript drivers. Instead, their APIs are based on the Web Platform APIs (the APIs generally follow the specifications and should match the implementation in Chrome and Firefox). There is also [WinterCG](https://wintercg.org/), which aims to use Web Platform APIs outside of browsers, namely on the server (Deno / Node.js) or edge runtimes (Cloudflare Workers / Deno).

In other words, we can not use database drivers directly at the edge with Cloudflare Workers or Deno.


# Efforts from the database providers

Despite insufficient TCP connect support, edge runtimes usually have better support for WebSocket and HTTP. Thus, many cloud database providers use serverless driver or data service to connect to the database.

| provider        | connection method                                        |
| --------------- | -------------------------------------------------------- |
| planetscale     | [serverless driver](https://github.com/planetscale/database-js) (HTTP)                                 |
| neon            | [serverless driver](https://github.com/neondatabase/serverless) (HTTP and Websocket )                  |
| tidb serverless | [serverless driver](https://github.com/tidbcloud/serverless-js) (HTTP) and data service                |
| supbase         | [serverless driver](https://github.com/supabase/supabase-js) (HTTP)                                 |
| mongo                | [Realm Web SDK](https://www.mongodb.com/docs/realm/web/) |



# Efforts from edge provider

The limitations of Data service or serverless driver is obvious. As a stateless protocol, HTTP performs differently from the traditional TCP way when connected to the database. For example, it is harder to provide `transactions` beyond HTTP. 

Thus, I think the better way is to optimize in edge runtime. Let's see what edge providers have done for it.

## Deno

Apart from the Web Platform APIs, Deno also provides global APIs to read from files, open TCP sockets, server HTTP, and execute subprocesses, etc. Based on these APIs, there are [deno drivers](https://github.com/denodrivers), make it easy to connect to different databases from deno edge runtime.

What's more, Deno paid a lot to the node compatibility. Deno supports import node and npm modules, you can import and use [node-mysql2](https://github.com/sidorares/node-mysql2) in deno, but node compatibility is not an easy thing. For example, errors occur when using TLS with node-mysql2. There is still a long way to go for it.

## Cloudflare Workers

Cloudflare Workers also provide [node compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) with the `compatibility_flags = [ "nodejs_compat" ]` configuration. But it doesn't mean you can use all the node APIs. Database drivers still can not be used in Cloudflare Workers.

Recently, Cloudflare Workers provided socket API for creating TCP sockets. Meanwhile, They add the socket API in [node-postgres](https://github.com/brianc/node-postgres), which means you can use this driver in Cloudflare Workers edge runtime directly.


# Conclusion

This article introduces what's edge and why it is hard to connect to the database at the edge. The article also shows you the efforts of database providers and edge providers. Believe that connecting to the database at the edge will no longer be a hassle in the near future.

# Reference

- [Introducing Cloudflare Workers: Run JavaScript Service Workers at the Edge](https://blog.cloudflare.com/introducing-cloudflare-workers/)
- [Everyone can now run JavaScript on Cloudflare with Workers](https://blog.cloudflare.com/cloudflare-workers-unleashed/)