Relational Databases Come to Workers

Cloudflare has spent this week focusing on its compute platform, and one of the biggest gaps for developers building on Workers has been the lack of first-class support for relational databases. While Workers KV, R2, and Durable Objects provide scalable storage options, and partnerships with the likes of Fauna, MongoDB, and Prisma cover some HTTP-based data platforms, traditional relational databases like Postgres and MySQL have remained out of reach.

That changes today with the announcement of database connectors for Workers. The initial release supports Postgres and MySQL, but it is an early step aimed at gathering developer feedback to tackle the substantial technical challenges involved in bridging the serverless runtime with traditional database infrastructure.

Why Database Connectors Are Difficult

The core problem is that relational databases expect long-lived TCP connections, a pattern that doesn’t map cleanly to the Workers runtime. Workers has historically only supported HTTP connections, which is why previous database solutions were either HTTP-based or required a proxy.

Even when a connection can be established, there is the issue of client libraries. Most database drivers are built for Node.js and rely on built-in modules that the Workers runtime does not fully support. Finally, database connections are expensive to establish, so any serious solution must also address connection pooling to avoid overwhelming the database with handshakes from short-lived serverless functions.

The Current Approach

Cloudflare is using a multi-part strategy to work around these limitations. The first piece is cloudflared, which creates a secure tunnel between a private network and Cloudflare. This existing tool can proxy HTTP traffic over WebSockets, and it is being repurposed to act as the bridge between a Worker and a database running in a customer’s infrastructure.

Introducing Relational Database Connectors

On the database side, a shim layer adapts the socket API of a popular runtime so that it connects directly to a database over a WebSocket. This means existing database drivers can be bundled into a Worker without forking them or making major modifications. Cloudflare has published a tutorial demonstrating how to connect to and query a Postgres database using this method, relying on a driver developed by the Deno community.

Leveraging Cloudflare Tunnels, we’re able to connect to a relational database

This design has trade-offs. The cloudflared tunnel gives Cloudflare little control over how and where the final connections are routed, which is a limitation the company acknowledges and plans to address. For now, the goal is to gather latency and performance data to inform future improvements.

What Comes Next

This announcement is positioned as a starting point. Cloudflare is looking for developers who want to build new applications or migrate existing ones to Workers while keeping their data in relational databases.

The longer-term roadmap is in three directions. Native TCP support in the Workers runtime is a priority, which would improve database connectivity and open the door to other data infrastructure. Cloudflare also wants to run its HTTP-to-TCP proxy service as a managed connection pooling service for developers. And finally, with connections to customer data in place, Cloudflare envisions caching that data on its network to provide low-latency access globally.

import { Client } from './driver/postgres/postgres'

export default {
  async fetch(request: Request, env, ctx: ExecutionContext) {
    try {
      const client = new Client({
        user: 'postgres',
        database: 'postgres',
        hostname: 'https://db.example.com',
        password: '',
        port: 5432,
      })
      await client.connect()
      const result = await client.queryArray('SELECT * FROM users WHERE uuid=1;')
      ctx.waitUntil(client.end())
      return new Response(JSON.stringify(result.rows[0]))
    } catch (e) {
      return new Response((e as Error).message)
    }
  },
}

Trying the Connectors

Getting started involves three steps:

  • Deploying cloudflared within your own infrastructure.
  • Deploying a database that connects to cloudflared.
  • Deploying a Worker with the database driver to submit queries.

The full Postgres tutorial is available on the Cloudflare developer docs. Developers who want to participate in early access or run into problems can sign up through the registration form or reach out on Discord.