Edge SQL Database Pooling, Query Caching, Private Access, and TLS
How Cloudflare Hyperdrive speeds PostgreSQL and MySQL from Workers: pooling, caching, VPC, limits, and how to verify connections.
This page summarizes Cloudflare documentation on Hyperdrive: a service that accelerates queries to existing PostgreSQL and MySQL databases from Cloudflare Workers. Use it when you already run a regional or hosted SQL database and want fast connection setup, global connection pooling, and optional read query caching—not when you only need Cloudflare’s own serverless SQL (D1), which is already optimized for Workers without Hyperdrive.
Hyperdrive vs D1 and direct TCP: when to use what
Section titled “Hyperdrive vs D1 and direct TCP: when to use what”Hyperdrive fits existing Postgres or MySQL (large databases, familiar drivers and ORMs, regional data). D1 fits lightweight, read-heavy serverless apps with global read replication. Direct TCP from a Worker works but pays full connection setup cost on every invocation.
Hyperdrive is designed to speed up connectivity to traditional, regional SQL databases such as PostgreSQL. These databases are typically accessed using database drivers that communicate over TCP/IP. Unlike D1, creating a secure database connection to a traditional SQL database involves multiple round trips between the client (your Worker) and your database server.
— Cloudflare, FAQ
Hyperdrive if you have an existing Postgres or MySQL database, require large (1TB, 100TB or more) single databases, and/or want to use your existing database tools.
— Cloudflare, Choose a data or storage product
Connecting over TCP without Hyperdrive adds latency because each Worker invocation must repeat handshake, TLS, and authentication. Hyperdrive pools connections globally and completes much of that setup at the edge. See How Hyperdrive works and Connect to databases.
How Hyperdrive speeds up database access
Section titled “How Hyperdrive speeds up database access”Hyperdrive combines edge connection setup, connection pooling near your origin database, and query caching for eligible reads.
Hyperdrive accelerates database queries by:
Performing the connection setup for new database connections near your Workers
Pooling existing connections near your database
Caching query results— Cloudflare, How Hyperdrive works
Connection lifecycle: edge setup and pooled origin connections
Section titled “Connection lifecycle: edge setup and pooled origin connections”Without Hyperdrive, Cloudflare describes roughly seven round-trips (TCP, TLS, DB auth) before the first query runs. Hyperdrive splits work into a fast Worker-to-Hyperdrive connection at the edge and reuse of pooled connections close to the database.
By maintaining a connection pool to your database within Cloudflare’s network, Hyperdrive reduces seven round-trips to your database before you can even send a query: the TCP handshake (1x), TLS negotiation (3x), and database authentication (3x).
— Cloudflare, Connection lifecycle
In a Cloudflare Worker, database client connections within the Worker are only kept alive for the duration of a single invocation. With Hyperdrive, creating a new client on each invocation is fast and recommended because Hyperdrive maintains the underlying database connections for you, pooled in an optimal location and shared across Workers to maximize scale.
— Cloudflare, Connection lifecycle
Regional pooling can sharply cut uncached query latency; egress uses Cloudflare’s standard IP ranges so you can allowlist Hyperdrive on database firewalls.
Smart Placement with Hyperdrive: multiple queries per request
Section titled “Smart Placement with Hyperdrive: multiple queries per request”For Workers that run several queries per request, placement near the database compounds with Hyperdrive’s pooling.
Yes, if your Worker makes multiple queries per request. Placement runs your Worker near your database, reducing per-query latency from 20-30ms to 1-3ms. Hyperdrive handles connection pooling and setup. Placement reduces the network distance for query execution.
— Cloudflare, FAQ
Pair Hyperdrive with Smart Placement when your database is regional; see 3rd Party Integrations for Hyperdrive and Smart Placement together.
Supported databases, drivers, and Hyperdrive limits
Section titled “Supported databases, drivers, and Hyperdrive limits”Hyperdrive supports PostgreSQL (9.0–18.x) and MySQL (5.7–8.x), plus many compatible providers (Neon, Supabase, PlanetScale, Aurora, Cloud SQL, and others listed in Supported databases and features). MongoDB, SQL Server, and D1 are not supported via Hyperdrive.
| Concern | PostgreSQL (recommended) | MySQL (recommended) |
|---|---|---|
| Driver | pg (node-postgres) ≥ 8.16.3 |
mysql2 ≥ 3.13.0 |
| Docs | Connect to PostgreSQL | Connect to MySQL |
| Python Workers | asyncpg, pg8000, psycopg — Python Workers |
aiomysql, pymysql |
MySQL from Workers uses the Hyperdrive binding fields (host, user, password, database, port) and disableEval: true on mysql2 for Workers compatibility—see MySQL support GA.
Hyperdrive does not support some session features (PostgreSQL LISTEN/NOTIFY, advisory locks, SQL-level PREPARE; MySQL USE, multi-statement queries, many auth plugins). For those, use a second direct client as recommended in Supported databases and features.
Account limits (Hyperdrive configurations)
Section titled “Account limits (Hyperdrive configurations)”| Limit | Free | Paid |
|---|---|---|
| Hyperdrive configs per account | 10 | 25 |
| Origin connections per config (typical) | ~20 | ~100 |
| Query duration | 60 s | 60 s |
| Max cached response size | 50 MB | 50 MB |
| Database queries (Hyperdrive) | 100,000 / day | Unlimited |
Details and footnotes (soft max on origin connections, failover pools) are in Limits and Pricing. Connection pooling and caching do not add separate Hyperdrive compute charges beyond Workers usage (FAQ).
Connect Hyperdrive to public and private databases
Section titled “Connect Hyperdrive to public and private databases”Public databases: configuration and credentials
Section titled “Public databases: configuration and credentials”Create a Hyperdrive configuration in the dashboard or with Wrangler Hyperdrive commands, bind it in your Worker, and connect with your existing driver using Hyperdrive’s connection details (Getting started).
For multiple databases, store separate connection strings as Worker secrets with descriptive names (for example DATABASE_URL_PROD and DATABASE_URL_STAGING per 3rd Party Integrations).
Popular hosted SQL integrations (Neon, PlanetScale, Supabase, Turso, Upstash, Xata, and others) are linked from Setting up new database connections and provider lists under PostgreSQL providers and MySQL providers.
You can also create PlanetScale Postgres and MySQL billed through your Cloudflare account; Hyperdrive still handles pooling and caching (PlanetScale on Cloudflare).
Private databases: Workers VPC (recommended) and Tunnels
Section titled “Private databases: Workers VPC (recommended) and Tunnels”For databases not on the public Internet, Workers VPC is the recommended path:
You can now connect Hyperdrive to a private database through a Workers VPC service. This is the recommended way to connect Hyperdrive to a private database that is not exposed to the public Internet.
— Cloudflare, Hyperdrive support for private databases with Workers VPC
In the dashboard, choose Connect to private database → Workers VPC, or use npx wrangler hyperdrive create with --service-id. VPC services are reusable across Hyperdrive configs and Workers (Connect to a private database using Workers VPC).
For tunnel-based setups, Hyperdrive can automatically configure Cloudflare Access, Service Token, and policies when you provide credentials and a Cloudflare Tunnel in the private network (Automatic configuration for private databases).
Query caching: what is cached, STABLE functions, and fresh reads
Section titled “Query caching: what is cached, STABLE functions, and fresh reads”Query caching is on by default (max_age 60 seconds, stale_while_revalidate 15 seconds; max max_age 1 hour). Hyperdrive caches non-mutating read queries and does not invalidate cache when your app writes.
Hyperdrive does not invalidate cached read query results when your application writes to your database. A matching read can return a cached result until the configured max_age expires, and Hyperdrive can serve that result during the stale_while_revalidate window while it refreshes the cache.
— Cloudflare, FAQ
STABLE and VOLATILE PostgreSQL functions in cached queries
Section titled “STABLE and VOLATILE PostgreSQL functions in cached queries”Hyperdrive now treats STABLE functions (for example NOW(), CURRENT_TIMESTAMP) as uncacheable, not only VOLATILE functions like RANDOM().
Because STABLE functions can return different results across different SQL statements within the same transaction, caching their results could serve stale or incorrect data. This change aligns Hyperdrive’s caching behavior with PostgreSQL’s function volatility semantics.
— Cloudflare, Hyperdrive no longer caches queries using STABLE PostgreSQL functions
Pass timestamps and similar values from your Worker as parameters instead of calling NOW() in SQL. Text-based matching also treats function names in SQL comments as uncacheable—avoid NOW() in comments (Query caching, Troubleshoot and debug).
Two Hyperdrive bindings for read-after-write consistency
Section titled “Two Hyperdrive bindings for read-after-write consistency”Use one binding with caching for tolerable staleness (catalogs, dashboards) and a second binding with --caching-disabled for auth, sessions, permissions, or post-write reads (FAQ, Query caching).
Edge caching of results can reduce cache-hit latency substantially (Up to 10x faster cached queries); that applies when caching is enabled on the configuration.
Connection pooling: size, metrics, restart, and transaction mode
Section titled “Connection pooling: size, metrics, restart, and transaction mode”Origin connection limit: minimum 5 and soft maximum
Section titled “Origin connection limit: minimum 5 and soft maximum”You can set how many connections Hyperdrive opens to your origin (minimum 5; maximum depends on Workers plan). Configure via dashboard, API, or:
npx wrangler hyperdrive update <HYPERDRIVE_ID> --origin-connection-limit=<MAX_CONNECTIONS>The max_size parameter acts as a soft limit - Hyperdrive may temporarily create additional connections during network issues or high traffic periods to ensure high availability and resiliency.
— Cloudflare, Connection pooling
Set Hyperdrive’s limit below your database’s own connection cap, especially with multiple Hyperdrive configs on one database (Tune connection pooling, Prisma Postgres example).
Hyperdrive pools in transaction mode: a connection is held for one transaction, then reset (session SET does not persist). Avoid long transactions and wrapping unrelated work inside BEGIN/COMMIT (Connection pooling, Connection lifecycle).
Pool metrics: waiting clients and open connections
Section titled “Pool metrics: waiting clients and open connections”The dashboard Metrics tab and GraphQL dataset hyperdrivePoolSizesAdaptiveGroups expose waitingClients, currentPoolSize, availablePoolSlots, and maxPoolSize (Hyperdrive pool size metrics, Metrics and analytics).
Connection contention shows up as spikes in waiting clients or open connections near pool size maximum. If that persists, request a higher limit via Cloudflare’s Limit Increase Request process.
Restart Hyperdrive configuration: break-glass pool drain
Section titled “Restart Hyperdrive configuration: break-glass pool drain”Manual Restart in the dashboard Danger zone drains the pool and forces new origin connections. Use only when you must cycle immediately; failovers usually recover automatically.
Restarting is a break-glass action. Hyperdrive automatically detects and recovers from most database failovers. Use a manual restart only when you need to force the pool to drain immediately.
— Cloudflare, Restart a Hyperdrive configuration from the dashboard
Restarting requires the Hyperdrive Admin role and can cause brief errors on in-flight queries (Connection pooling).
TLS and mTLS: SSL modes for PostgreSQL and MySQL
Section titled “TLS and mTLS: SSL modes for PostgreSQL and MySQL”Default modes encrypt traffic: PostgreSQL require, MySQL REQUIRED (WebPKI validation). Stricter verify-ca / VERIFY_CA and verify-full / VERIFY_IDENTITY need uploaded CA material via npx wrangler cert upload; MySQL custom certs are documented in Hyperdrive custom TLS for MySQL and SSL/TLS certificates.
Client mTLS lets Hyperdrive present a client certificate to the database in addition to username and password (Hyperdrive custom TLS/SSL certificates).
Local development with wrangler dev and remote databases
Section titled “Local development with wrangler dev and remote databases”localConnectionString in Wrangler config (or CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>) lets wrangler dev talk to remote TLS databases without wrangler dev --remote.
{ "hyperdrive": [ { "binding": "HYPERDRIVE", "id": "your-hyperdrive-id", "localConnectionString": "postgres://user:password@remote-host.example.com:5432/database?sslmode=require" } ]}When using a remote string locally, the Worker runs on your machine and Hyperdrive caching does not apply (Connect to remote databases during local development, Release notes).
Common mistakes when using Hyperdrive
Section titled “Common mistakes when using Hyperdrive”- Expecting instant fresh reads after writes while caching is enabled—use a cache-disabled configuration for those reads (FAQ).
NOW()orRANDOM()in SQL (or in comments) preventing cache hits or causing incorrect cache assumptions (Query caching).prepare: falseon Postgres.js under transaction pooling (Connect to PostgreSQL).- Origin connection limit above the database’s cap, or ignoring soft overshoot during outages (Tune connection pooling).
- Long-lived DB clients in Durable Objects exhausting the pool (Connection lifecycle).
- Using Hyperdrive for MongoDB or D1 (FAQ, Supported databases and features).
Verify Hyperdrive is connected to your database
Section titled “Verify Hyperdrive is connected to your database”On PostgreSQL, connections from Hyperdrive often appear as application_name = 'Cloudflare Hyperdrive' in pg_stat_activity unless the driver overrides it (Connect to PostgreSQL).
On MySQL, check performance_schema.threads for PROGRAM_NAME = 'Cloudflare Hyperdrive' (Connect to MySQL).
How to check this on your site
Section titled “How to check this on your site”-
Open the Hyperdrive page in the Cloudflare dashboard, select your configuration, and review the Metrics tab (query volume, latency, cache ratio, and Pool connections).
-
If you see elevated waiting clients, compare open connections to pool size maximum and adjust Origin connection limit per Tune connection pooling if needed.
-
On the database, run the identification queries from Connect to PostgreSQL or Connect to MySQL to confirm Hyperdrive origin connections.
-
For caching issues, follow Troubleshoot and debug (STABLE functions, comments, driver
preparesettings). -
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 sentence you are asking about.