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
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.
Before talking about
why it is hard. We'd better learn how the edge works.
There are two important parts of edge:
- 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).
Whether you can connect to the database depends on the edge runtime.
In other words, we can not use database drivers directly at the edge with Cloudflare Workers or Deno.
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.
|planetscale||serverless driver (HTTP)|
|neon||serverless driver (HTTP and Websocket )|
|tidb serverless||serverless driver (HTTP) and data service|
|supbase||serverless driver (HTTP)|
|mongo||Realm Web SDK|
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.
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, 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 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 also provide node compatibility 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, which means you can use this driver in Cloudflare Workers edge runtime directly.
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.