Global serverless applications
A globally distributed Cloudflare Worker needs low-latency access to Neon Postgres.
Neon · Const
Cloudflare Hyperdrive connects Cloudflare Workers to Neon Postgres through a globally distributed connection pool. It lets Workers use standard PostgreSQL drivers while reducing connection setup latency.
Cloudflare Hyperdrive sits between a Cloudflare Worker and Neon Postgres. It maintains connection pools distributed across Cloudflare locations and routes Worker queries to a nearby pool, while standard PostgreSQL drivers such as node-postgres or postgres.js issue the queries.
npx wrangler hyperdrive create <name> --connection-string=<NEON_DATABASE_CONNECTION_STRING>| Name | Type | Description |
|---|---|---|
connection-string | string | Neon Postgres connection string passed to Wrangler when creating the Hyperdrive configuration. |
name | string | Name assigned to the Hyperdrive service created with Wrangler. |
A Hyperdrive configuration and Worker binding that provide connection details for querying the selected Neon Postgres database.
import { Client } from "pg";
export default {
async fetch(request, env) {
const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
await client.connect();
try {
const result = await client.query("select now() as current_time");
return Response.json(result.rows[0]);
} finally {
await client.end();
}
}
};Uses node-postgres with the connection string supplied by a Cloudflare Hyperdrive binding.
A globally distributed Cloudflare Worker needs low-latency access to Neon Postgres.
A Worker uses node-postgres or postgres.js and needs connection pooling without implementing it itself.
Many short-lived Worker requests would otherwise repeatedly establish database connections.
Hyperdrive is Cloudflare’s connection-pooling service for Workers. It keeps database connections in globally distributed pools and routes requests to the closest available connection, reducing connection-setup latency.
Follow-up: Why should the Neon connection string have pooling disabled? Because Hyperdrive itself manages pooling, so adding Neon’s pooled endpoint would introduce an unnecessary second pooling layer.
A Worker can use a regular PostgreSQL driver, including node-postgres or postgres.js, with Hyperdrive. The Worker reads the connection string from its Hyperdrive binding and passes it to the driver.
Follow-up: How does this differ from Neon’s serverless driver? The serverless driver connects over HTTP or WebSockets, whereas Hyperdrive is intended for native PostgreSQL drivers and handles pooling at Cloudflare’s network edge.
Create a Hyperdrive configuration with Wrangler using Neon’s direct connection string, add the resulting Hyperdrive binding to the Worker, and use that binding’s connection details in the Worker code. The Neon role and database selected for the connection determine the target database.
Follow-up: What should be checked if deployment fails? Verify that the binding name in the Worker matches the Wrangler configuration and that the source connection string is direct rather than pooled.
Cloudflare WorkersHyperdrive bindingWrangler CLInode-postgrespostgres.jsNeon serverless driverconnection pooling