Worker Database Pooling, Query Caching, Private Access and Troubleshooting
How Cloudflare Hyperdrive connects Workers to Postgres and MySQL: pooling, caching, VPC, limits, and fixing connection errors.
This page summarizes Cloudflare Hyperdrive documentation for teams that already run PostgreSQL or MySQL elsewhere and want Cloudflare Workers to query that data with less connection overhead. Hyperdrive sits between your Worker and the origin database: it pools connections to the database and can cache read query results. The same docs also describe when to pick other Worker storage products instead of an external SQL database.
Hyperdrive vs other Worker storage: when to use what
Section titled “Hyperdrive vs other Worker storage: when to use what”Workers can use several data products. The storage options guide describes them with recommended use cases and best practices:
This guide describes the storage & database products available as part of Cloudflare Workers, including recommended use-cases and best practices.
— Cloudflare, Choose a data or storage product
Use Hyperdrive when the system of record is an existing Postgres or MySQL host (managed cloud, PlanetScale, Neon, Supabase, self-hosted, and others documented under Connect to PostgreSQL and Connect to MySQL). Use Cloudflare D1 when you want a SQL database operated on Cloudflare; the connecting to databases overview points new projects there. Use Durable Objects, Vectorize, or object storage where the docs position those products—for example vector search or strongly consistent per-object state—not as a drop-in replacement for a regional Postgres cluster.
MongoDB and other non-SQL systems are covered outside Hyperdrive (for example the MongoDB integration and connect to MongoDB tutorial).
How Hyperdrive works: connection pooling and query caching
Section titled “How Hyperdrive works: connection pooling and query caching”Hyperdrive’s role is to make edge Workers practical against databases that enforce connection limits and round-trip latency. The Concepts page covers architecture; connection pooling and query caching are the two mechanisms most applications feel day to day.
The PlanetScale Postgres & MySQL overview states plainly that Hyperdrive pools connections and caches queries—the same model applies regardless of vendor. Your Worker typically opens a driver connection to Hyperdrive; Hyperdrive maintains a smaller set of connections to the origin and may serve repeated read queries from cache. Pool exhaustion and cache staleness are operational trade-offs documented in limits and troubleshooting, not surprises you discover only in production.
Cloudflare Agents session storage defaults to Durable Object SQLite, but external Postgres is supported for cross-agent queries and analytics:
These providers work with Postgres-compatible databases through Hyperdrive for connection pooling.
— Cloudflare, Sessions
Query caching and stale reads after writes
Section titled “Query caching and stale reads after writes”If a write is followed by a read that returns older data, query caching may be serving a cached result. Cloudflare advises checking Hyperdrive metrics by cacheStatus (hit, miss, disabled, uncacheable) via Metrics and analytics, then following query caching guidance to separate fresh reads from cacheable reads—including a cache-disabled Hyperdrive configuration when reads must be current.
Connect to a public database vs a private database with Workers VPC
Section titled “Connect to a public database vs a private database with Workers VPC”Public hostname and TLS requirements
Section titled “Public hostname and TLS requirements”For databases reachable on the public Internet, you create a Hyperdrive configuration with hostname, port, credentials, and TLS settings. When you create or update a configuration, Hyperdrive tests connectivity in the background and runs a minimal validation query (for PostgreSQL, an empty ; query) before saving. Failure surfaces structured error codes—documented in Troubleshoot and debug—such as:
| Situation | What Hyperdrive reports | Typical fix |
|---|---|---|
| DNS or hostname | Error 2008 bad hostname; 2010 cannot connect to host:port |
Confirm public DNS and spelling |
| Private IP only | Error 2009 hostname does not resolve to a public IP |
Use a public endpoint or private access via VPC |
| Firewall / ACL | Error 2011 connection refused; runtime connection_refused |
Allow connections from the public Internet to the database |
| TLS | Error 2012 TLS not supported; handshake / cert errors |
Enable TLS on the database; configure CA or client certs as required |
| Credentials / DB name | 2013 invalid credentials; 2014 database does not exist |
Verify user, password (case-sensitive), and database name |
| Permissions | 2016 test query failed |
Grant read/write for the Hyperdrive user |
Hyperdrive can only connect to public IP addresses. Private IP addresses, like 10.1.5.0 or 192.168.2.1, are not currently supported.
— Cloudflare, Troubleshoot and debug
Private database with Hyperdrive and Workers VPC (recommended)
Section titled “Private database with Hyperdrive and Workers VPC (recommended)”When the database has only a private IP, use Connect to a private database using Workers VPC (Recommended) rather than exposing the instance publicly. The private database example and configuration guide point to query caching, VPC Service configuration including TLS certificate verification, high-availability tunnels for production, and Hyperdrive-specific troubleshooting.
Drivers, ORMs, and supported Postgres/MySQL providers
Section titled “Drivers, ORMs, and supported Postgres/MySQL providers”Entry paths:
- Getting started with Hyperdrive
- Tutorials: PostgreSQL with Workers, MySQL with Workers
- Driver examples: Drizzle ORM and Prisma ORM for Postgres; MySQL variants under the same trees
- Python Workers — see Hyperdrive from Python Workers for supported drivers
Provider-specific pages (AWS RDS/Aurora, Azure, Google Cloud SQL, Neon, Supabase, CockroachDB, Timescale, and others) follow the same pattern: wire Hyperdrive to the vendor connection string, then use Troubleshoot and debug if setup fails. For DigitalOcean Postgres, DNS propagation delays can cause transient DNS errors—waiting about ten minutes before retrying is documented on that page.
Hyperdrive also documents direct socket use and supported protocols via Connect to PostgreSQL / Connect to MySQL “next steps” (Socket API and supported protocols).
Local development: wrangler dev vs wrangler dev --remote
Section titled “Local development: wrangler dev vs wrangler dev --remote”Hyperdrive works locally through Wrangler. From Local development:
wrangler dev (default): Runs your Worker code locally on your machine. You configure a localConnectionString to connect directly to a database (either local or remote). Hyperdrive query caching does not take effect in this mode. wrangler dev –remote: Runs your Worker on Cloudflare’s using your deployed Hyperdrive configuration. This is useful for testing with Hyperdrive’s connection pooling and query caching enabled.
— Cloudflare, Local development
Use default wrangler dev for fast iteration against a localConnectionString; use --remote when you need behaviour that matches production pooling and caching. Management commands are listed under Wrangler commands for Hyperdrive.
Limits, pricing, and runtime connection errors
Section titled “Limits, pricing, and runtime connection errors”Review Hyperdrive limits for your Workers plan alongside pricing (which links to Hyperdrive documentation). At runtime, when Hyperdrive cannot obtain a connection from the pool, you may see:
Failed to acquire a connection from the pool. The connection pool is exhausted because connections are held open too long. Long-running queries or transactions are a common cause.
— Cloudflare, Limits
Another common runtime message is Server connection attempt failed: connection_refused when the origin rejects connections—firewall, ACL, or the provider’s connection limit. The FAQ collects other frequent questions; Demos and architectures show how Hyperdrive fits serverless API designs.
Common mistakes in Worker database code
Section titled “Common mistakes in Worker database code”Do not pool or reuse clients in the Worker global scope
Section titled “Do not pool or reuse clients in the Worker global scope”Hyperdrive already pools to the origin. The troubleshooting guide documents repeated failures when reusing node-postgres (pg) or mysql2 clients across requests—for example:
Cannot use a pool after calling end on the pool — Do not use new Pool() in the global scope. Create a new Client() inside your handler — Hyperdrive handles connection pooling for you.
— Cloudflare, Troubleshoot and debug
The same page recommends creating a new client or connection inside the request handler for errors such as “Connection terminated”, “Client has already been connected”, and mysql2 “Pool is closed” or PROTOCOL_CONNECTION_LOST.
MySQL prepared statements
Section titled “MySQL prepared statements”Hyperdrive does not support MySQL COM_STMT_PREPARE messages:
Hyperdrive does not currently support MySQL COM_STMT_PREPARE messages — Remove prepared statements from your MySQL queries.
— Cloudflare, Troubleshoot and debug
Use non-prepared query patterns or drivers configured accordingly.
Assuming cache-disabled behaviour in local default dev
Section titled “Assuming cache-disabled behaviour in local default dev”Default local wrangler dev does not apply Hyperdrive query caching. Stale-read debugging belongs in deployed or --remote environments with metrics enabled.
How to check this on your site
Section titled “How to check this on your site”- Create or update a Hyperdrive configuration in the dashboard or via Wrangler and confirm the background test connection succeeds; if it fails, map the error code using Troubleshoot and debug.
- Run the Worker with
wrangler dev --remoteusing the deployed Hyperdrive binding to verify pooling and query caching match production, as described in Local development. - After deploy, use Metrics and analytics (including
cacheStatus) to confirm whether reads are hits, misses, disabled, or uncacheable—especially if you see stale data after writes. - Compare observed connection errors against Limits and the Hyperdrive-specific error tables in Troubleshoot and debug.
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.