Skip to content
Back to Cloud Services

Cloudflare Hyperdrive DB Connection Pooling

Connection pooling and query caching for Postgres/MySQL from Workers — reuse connections across isolates.

CloudflareIntermediate

Why

Workers can't keep DB connections open (each request is a new isolate). Hyperdrive pools connections at the edge, caches query results, and reduces connection overhead 10x.

Free, works with any Postgres.

When

Use Hyperdrive when Workers query Postgres/MySQL — eliminates connection exhaustion and adds query caching. Avoid for D1 (native edge) or single-query apps (overhead).

How

1. Create — wrangler hyperdrive create my-pool --connection-string="postgres://..." 2. Bind — [[hyperdrive]] binding="HYPER" id="..." 3. Connect — const client = new Client({ connection: env.HYPER.connectionString }) 4. Query — normal pg client, Hyperdrive pools + caches transparently 5. Cache — Hyperdrive caches SELECT results, invalidate via max_age config 6. Read replica — point Hyperdrive at read replica for read-heavy workloads 7. Local dev — wrangler dev uses local connection string automatically

Example

// wrangler.toml [[hyperdrive]] binding = "HYPER" id = "abc123..." // src/index.ts — Postgres from Worker via Hyperdrive import { Client } from "pg"; export default { async fetch(request: Request, env: Env): Promise<Response> { const client = new Client({ connection: env.HYPER.connectionString }); await client.connect(); const { rows } = await client.query("SELECT * FROM users WHERE active = $1", [true]); await client.end(); return Response.json(rows); }, };

Common Problems

  • ⚠Writes bypass cache — Hyperdrive caches SELECT only, INSERT/UPDATE not cached
  • ⚠Connection limit on origin DB — Hyperdrive pools but origin still has max connections
  • ⚠SSL required — Hyperdrive enforces TLS, self-signed certs need ca config

Best Practices

  • ✓Point at read replica — Hyperdrive + replica = edge-cached reads, primary for writes
  • ✓Set query cache max_age — 60s cache cuts DB load dramatically for read-heavy APIs
  • ✓Use prepared statements — Hyperdrive caches them, faster repeated queries

FAQ

What is Cloudflare Hyperdrive DB Connection Pooling and what problem does it solve?

Connection pooling and query caching for Postgres/MySQL from Workers — reuse connections across isolates. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with Cloudflare Hyperdrive DB Connection Pooling in 5 minutes?

Two steps to run it: (1) Create — wrangler hyperdrive create my-pool --connection-string="postgres://..."; (2) Bind — [[hyperdrive]] binding="HYPER" id="...". Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare Hyperdrive DB Connection Pooling?

The most frequent issue is: Writes bypass cache — Hyperdrive caches SELECT only, INSERT/UPDATE not cached. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare Hyperdrive DB Connection Pooling?

Best practices for Cloudflare Hyperdrive DB Connection Pooling include: Point at read replica — Hyperdrive + replica = edge-cached reads, primary for writes; Set query cache max_age — 60s cache cuts DB load dramatically for read-heavy APIs; Use prepared statements — Hyperdrive caches them, faster repeated queries. The first one — "Point at read replica — Hyperdrive + replica = edge-cached reads, primary for writes" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare Hyperdrive DB Connection Pooling?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most Cloudflare Hyperdrive DB Connection Pooling options offer a free tier — validate within free quota before scaling.