prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Cloudflare Workers: Use direct TCP `connect()` API to connect directly to the database

Open eliezedeck opened this issue 2 years ago • 3 comments

Problem

Currently, to deploy to Cloudflare Workers, we need to use Prisma Data Proxy. This is because Cloudflare Workers previously only supported HTTP/HTTPS connections, not TCP connections.

Suggested solution

Very recently, direct TCP connection has been announced for Cloudflare Workers, and is now a supported method of communication for Workers, with the help of the new connect() API: https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/

They've worked with the folks who created the pg library, and they have showcased the use of this new API in the announcement to connect to a PostgreSQL database directly, without any sort of proxy or adapter: https://blog.cloudflare.com/workers-tcp-socket-api-connect-databases/

I have no clue what driver does Prisma use, but I think this is really exciting and opens up a much anticipated direct connection on Serverless.

Additional context

There is another ticket open at this time about adding support for Neon's Serverless driver (here https://github.com/prisma/prisma/issues/19409) but this one is different, and I believe having this would also make the Serverless driver unnecessary, that's because Neon already has pooled connection string that prevents the typical Serverless issues caused by many connections to the DB.

eliezedeck avatar May 28 '23 15:05 eliezedeck

discussed already in https://github.com/prisma/prisma/issues/19344

morpig avatar Jun 01 '23 15:06 morpig

We are obviously looking into this. It is super interesting, and has the potential to be a game changer for Cloudflare Workers and Prisma (and other database clients).

Currently, adding support for this in Prisma Client is unfortunately not super straightforward, as our database connections are triggered by native code via Rust - and making the cloudflare:sockets available to that is not trivial. But we are looking at alternatives.


Another, more serious stumbling block, that is out of our control, is the following section from "Considerations" in the docs:

TCP sockets must be created within the fetch() handler of a Worker. TCP sockets cannot be created in global scope and shared across requests.

Or from the blog post:

If you’ve worked more closely with database scaling and performance, you might have noticed that in the example above, a new connection is created for every request. This is one of the biggest current challenges of connecting to databases from serverless functions, across all platforms. With typical client connection pooling, you maintain a local pool of database connections that remain open. This approach of storing a reference to a connection or connection pool in global scope will not work, and is a poor fit for serverless. Managing individual pools of client connections on a per-isolate basis creates other headaches — when and how should connections be terminated? How can you limit the total number of concurrent connections across many isolates and locations?

This effectively means that Prisma Client can not pool any database connections that are opened via connect(), and every request has to open its own database connection(s). That leads to a very dramatic hit in the latency of all requests, as they have to open one (or multiple) connection(s) to the database first before you can run any queries.

Fortunately, Cloudflare seems to be aware and to be working on solutions:

Instead, we’re already working on simpler approaches to connection pooling for the most popular databases. We see a path to a future where you don’t have to think about or manage client connection pooling on your own.

We'll watching that closely and take that into account.

janpio avatar Jun 01 '23 17:06 janpio

I believe having this would also make the Serverless driver unnecessary

For cloudflare workers that maybe true, although the Vercel Edge runtime does not provide any API that would allow to communicate to a DB (except for HTTP(S) using fetch and probably websockets which the serverless driver uses)

immjs avatar Jun 23 '23 11:06 immjs

Simple post for native TCP connection via pg library - works as expected.

Torbet avatar Jul 26 '23 16:07 Torbet

Hey, we at Prisma have been working on the foundations for Edge Functions support for the past few weeks - the basis that will be required for supporting Cloudflare's TCP connect() API. Soon, we'll have something to share and test! If you are interested in helping us, please respond to this super short survey and leave your email address so we can get in touch with instructions: https://pris.ly/survey/driver-adapter-cfwtcp Thanks.

janpio avatar Sep 13 '23 21:09 janpio

This just got more interesting: https://blog.cloudflare.com/hyperdrive-making-regional-databases-feel-distributed/

Also something to mention: The cloudflare:sockets are not only supported in pg, but also Postgres.js: https://github.com/porsager/postgres/pull/599

janpio avatar Sep 28 '23 18:09 janpio

Great to see there is being worked on! Would love to see this, I signed up for the beta! 😄

JustJoostNL avatar Oct 13 '23 21:10 JustJoostNL

Are there any updates on this?

JustJoostNL avatar Nov 01 '23 16:11 JustJoostNL

No, we are working on it. We will post a comment here (or reach out to relevant participants of the survey first) when we have something new.

janpio avatar Nov 01 '23 18:11 janpio

That's great, do you have any ETA though?

JustJoostNL avatar Nov 08 '23 20:11 JustJoostNL

Speaking for Jan here as I've been around prisma a while enough to know (so I hope this is okay)....

No, they do not provide ETAs. Best you can do is upvote this task so they prioritize this task even though they are already working on it.

uncvrd avatar Nov 10 '23 23:11 uncvrd

If you did sign up via the survey I posted above, you should already have received an email or will receive one in the coming days to be able to test pg on Cloudflare Workers and Pages Functions. If you haven't you can still fill it out.

janpio avatar Jan 11 '24 18:01 janpio

Hey everyone!

We just released Prisma ORM version 5.11.0 which includes a preview feature for Edge Functions support via Cloudflare Workers and Pages and Vercel Edge Functions and Middleware in Prisma ORM 🥳

  • The release notes give a quick overview: https://github.com/prisma/prisma/releases/tag/5.11.0
    • Prisma ORM when deployed to edge functions now supports drivers for Neon Database (and Vercel Postgres), PlanetScale and Turso via their serverless drivers on both Vercel and Cloudflare, and additionally PostgreSQL via pg on Cloudflare via connect() (which also includes Hyperdrive!)💥
  • We also have extensive documentation: https://www.prisma.io/docs/orm/prisma-client/deployment/edge/overview
  • And our blog post goes a bit more in depth: https://www.prisma.io/blog/prisma-orm-support-for-edge-functions-is-now-in-preview

Please give it a try, and let us know how it goes! If you encounter any problems, please create a new bug report issue, or if the problem is driver adapter specific, use the feedback discussions for @prisma/adapter-neon, @prisma/adapter-planetscale, @prisma/adapter-libsql / Turso or @prisma/adapter-pg 🙇

PS: We are also working on support for Cloudflare D1, and expect to share news in this feature request issue soon. (Early Access for that is already in progress)

janpio avatar Mar 12 '24 18:03 janpio