Skip to content

Commit b337df7

Browse files
thomasgauvinOxyjun
authored andcommitted
add hyperdrive connection lifecycle docs (#26425)
* add hyperdrive connection lifecycle docs * PCX Review * Fixing missed link updates * add nit to connection lifecycle garbage collection --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 6914081 commit b337df7

27 files changed

+14881
-37
lines changed

public/__redirects

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,9 +2045,12 @@
20452045
/version-management/reference/available-settings/ /version-management/reference/available-configurations/ 301
20462046

20472047
# hyperdrive
2048-
/hyperdrive/learning/how-hyperdrive-works/ /hyperdrive/configuration/how-hyperdrive-works/ 301
2048+
/hyperdrive/configuration/how-hyperdrive-works/ /hyperdrive/concepts/how-hyperdrive-works/ 301
2049+
/hyperdrive/configuration/query-caching/ /hyperdrive/concepts/query-caching/ 301
2050+
/hyperdrive/configuration/connection-pooling/ /hyperdrive/concepts/connection-pooling/ 301
2051+
/hyperdrive/learning/how-hyperdrive-works/ /hyperdrive/concepts/how-hyperdrive-works/ 301
20492052
/hyperdrive/learning/connect-to-postgres/ /hyperdrive/examples/connect-to-postgres/ 301
2050-
/hyperdrive/learning/query-caching/ /hyperdrive/configuration/query-caching/ 301
2053+
/hyperdrive/learning/query-caching/ /hyperdrive/concepts/query-caching/ 301
20512054
/hyperdrive/learning/troubleshooting/ /hyperdrive/observability/troubleshooting/ 301
20522055
/hyperdrive/changelog/ /hyperdrive/platform/release-notes/ 301
20532056
/hyperdrive/platform/changelog/ /hyperdrive/platform/release-notes/ 301

src/assets/images/hyperdrive/configuration/hyperdrive-connection-lifecycle.svg

Lines changed: 14740 additions & 0 deletions
Loading

src/content/changelog/hyperdrive/2024-12-11-hyperdrive-caching-at-edge.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ _P50, P75, and P90 Hyperdrive session latency for all client connection sessions
1818

1919
This performance improvement is applied to all new and existing Hyperdrive configurations that have caching enabled.
2020

21-
For more details on how Hyperdrive performs query caching, refer to the [Hyperdrive documentation](/hyperdrive/configuration/how-hyperdrive-works/#query-caching).
21+
For more details on how Hyperdrive performs query caching, refer to the [Hyperdrive documentation](/hyperdrive/concepts/how-hyperdrive-works/#query-caching).

src/content/changelog/hyperdrive/2025-03-04-hyperdrive-pooling-near-database-and-ip-range-egress.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ By improving placement of Hyperdrive database connection pools, Workers' Smart P
1414

1515
With this update, Hyperdrive also uses [Cloudflare's standard IP address ranges](https://www.cloudflare.com/ips/) to connect to your database. This enables you to configure the firewall policies (IP access control lists) of your database to only allow access from Cloudflare and Hyperdrive.
1616

17-
Refer to [documentation on how Hyperdrive makes connecting to regional databases from Cloudflare Workers fast](/hyperdrive/configuration/how-hyperdrive-works/).
17+
Refer to [documentation on how Hyperdrive makes connecting to regional databases from Cloudflare Workers fast](/hyperdrive/concepts/how-hyperdrive-works/).
1818

1919
This improvement is enabled on all Hyperdrive configurations.

src/content/changelog/hyperdrive/2025-04-08-hyperdrive-mysql-support.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ export default {
4646
} satisfies ExportedHandler<Env>;
4747
```
4848

49-
Learn more about [how Hyperdrive works](/hyperdrive/configuration/how-hyperdrive-works/) and [get started building Workers that connect to MySQL with Hyperdrive](/hyperdrive/get-started/).
49+
Learn more about [how Hyperdrive works](/hyperdrive/concepts/how-hyperdrive-works/) and [get started building Workers that connect to MySQL with Hyperdrive](/hyperdrive/get-started/).

src/content/changelog/hyperdrive/2025-07-02-hyperdrive-configurable-connection-count.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ All configurations have a minimum of 5 connections. The maximum connection count
1212

1313
This feature allows you to right-size your connection pool based on your database capacity and application requirements. You can configure connection counts through the Cloudflare dashboard or API.
1414

15-
Refer to the [Hyperdrive configuration documentation](/hyperdrive/configuration/connection-pooling/) for more information.
15+
Refer to the [Hyperdrive configuration documentation](/hyperdrive/concepts/connection-pooling/) for more information.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
pcx_content_type: concept
3+
title: Connection lifecycle
4+
sidebar:
5+
order: 2
6+
---
7+
8+
Understanding how connections work between Workers, Hyperdrive, and your origin database is essential for building efficient applications with Hyperdrive.
9+
10+
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).
11+
12+
## How connections are managed
13+
14+
When you use a database client in a Cloudflare Worker, the connection lifecycle works differently than in traditional server environments. Here's what happens:
15+
16+
![Hyperdrive connection](~/assets/images/hyperdrive/configuration/hyperdrive-connection-lifecycle.svg)
17+
18+
Without Hyperdrive, every Worker invocation would need to establish a new connection directly to your origin database. This connection setup process requires multiple roundtrips across the Internet to complete the TCP handshake, TLS negotiation, and database authentication — that's 7x round trips and added latency before your query can even execute.
19+
20+
Hyperdrive solves this by splitting the connection setup into two parts: a fast edge connection and an optimized path to your database.
21+
22+
1. **Connection setup on the edge**: The database driver in your Worker code establishes a connection to the Hyperdrive instance. This happens at the edge, colocated with your Worker, making it extremely fast to create connections. This is why you use Hyperdrive's special connection string.
23+
24+
2. **Single roundtrip across regions**: Since authentication has already been completed at the edge, Hyperdrive only needs a single round trip across regions to your database, instead of the multiple roundtrips that would be incurred during connection setup.
25+
26+
3. **Get existing connection from pool**: Hyperdrive uses an existing connection from the pool that is colocated close to your database, minimizing latency.
27+
28+
4. **If no available connections, create new**: When needed, new connections are created from a region close to your database to reduce the latency of establishing new connections.
29+
30+
5. **Run query**: Your query is executed against the database and results are returned to your Worker through Hyperdrive.
31+
32+
6. **Connection teardown**: When your Worker finishes processing the request, the database client connection in your Worker is automatically garbage collected. However, Hyperdrive keeps the connection to your origin database open in the pool, ready to be reused by the next Worker invocation. This means subsequent requests will still perform the fast edge connection setup, but will reuse one of the existing connections from Hyperdrive's pool near your database.
33+
34+
:::note
35+
36+
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.
37+
38+
:::
39+
40+
## Connection lifecycle considerations
41+
42+
### Durable Objects and persistent connections
43+
44+
Unlike regular Workers, [Durable Objects](/durable-objects/) can maintain state across multiple requests. If you keep a database client open in a Durable Object, the connection will remain allocated from Hyperdrive's connection pool. Long-lived Durable Objects can exhaust available connections if many objects keep connections open simultaneously.
45+
46+
:::caution
47+
48+
Be careful when maintaining persistent database connections in Durable Objects. Each open connection consumes resources from Hyperdrive's connection pool, which could impact other parts of your application. Close connections when not actively in use, use connection timeouts, and limit the number of Durable Objects that maintain database connections.
49+
50+
:::
51+
52+
### Long-running transactions
53+
54+
Hyperdrive operates in [transaction pooling mode](/hyperdrive/concepts/how-hyperdrive-works/#pooling-mode), where a connection is held for the duration of a transaction. Long-running transactions that contain multiple queries can exhaust Hyperdrive's available connections more quickly because each transaction holds a connection from the pool until it completes.
55+
56+
:::tip
57+
58+
Keep transactions as short as possible. Perform only the essential queries within a transaction, and avoid including non-database operations (like external API calls or complex computations) inside transaction blocks.
59+
60+
:::
61+
62+
Refer to [Limits](/hyperdrive/platform/limits/) to understand how many connections are available for your Hyperdrive configuration based on your Workers plan.
63+
64+
## Related resources
65+
66+
- [How Hyperdrive works](/hyperdrive/concepts/how-hyperdrive-works/)
67+
- [Connection pooling](/hyperdrive/concepts/connection-pooling/)
68+
- [Limits](/hyperdrive/platform/limits/)
69+
- [Durable Objects](/durable-objects/)

src/content/docs/hyperdrive/configuration/connection-pooling.mdx renamed to src/content/docs/hyperdrive/concepts/connection-pooling.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
pcx_content_type: concept
33
title: Connection pooling
44
sidebar:
5-
order: 5
5+
order: 4
66
---
77

8+
import { Render } from "~/components";
9+
810
Hyperdrive maintains a pool of connections to your database. These are optimally placed to minimize the latency for your applications. You can configure
911
the amount of connections your Hyperdrive configuration uses to connect to your origin database. This enables you to right-size your connection pool based on your database capacity and application requirements.
1012

@@ -19,6 +21,10 @@ Hyperdrive will automatically scale the amount of database connections held open
1921

2022
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.
2123

24+
## Pooling mode
25+
26+
<Render file="pooling-mode" product="hyperdrive" />
27+
2228
## Best practices
2329

2430
You can configure connection counts using the Cloudflare dashboard or the Cloudflare API. Consider the following best practices to determine the right limit for your use-case:
@@ -30,6 +36,6 @@ You can configure connection counts using the Cloudflare dashboard or the Cloudf
3036

3137
## Next steps
3238

33-
- Learn more about [How Hyperdrive works](/hyperdrive/configuration/how-hyperdrive-works/).
39+
- Learn more about [How Hyperdrive works](/hyperdrive/concepts/how-hyperdrive-works/).
3440
- Review [Hyperdrive limits](/hyperdrive/platform/limits/) for your Workers plan.
3541
- Learn how to [Connect to PostgreSQL](/hyperdrive/examples/connect-to-postgres/) from Hyperdrive.

src/content/docs/hyperdrive/configuration/how-hyperdrive-works.mdx renamed to src/content/docs/hyperdrive/concepts/how-hyperdrive-works.mdx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
pcx_content_type: concept
33
title: How Hyperdrive works
44
sidebar:
5-
order: 2
5+
order: 1
66
---
77

8+
import { Render } from "~/components";
9+
810
Connecting to traditional centralized databases from Cloudflare's global network which consists of over [300 data center locations](https://www.cloudflare.com/network/) presents a few challenges as queries can originate from any of these locations.
911

10-
If your database is centrally located, queries can take a long time to get to the database and back. Queries can take even longer in situations where you have to establish a connection and make multiple round trips.
12+
If your database is centrally located, queries can take a long time to get to the database and back. Queries can take even longer in situations where you have to establish new connections from stateless environments like Workers, requiring multiple round trips for each Worker invocation.
1113

1214
Traditional databases usually handle a maximum number of connections. With any reasonably large amount of distributed traffic, it becomes easy to exhaust these connections.
1315

@@ -31,6 +33,8 @@ When a database driver connects to a database from a Cloudflare Worker **directl
3133

3234
**With Hyperdrive**, this connection setup occurs between your Cloudflare Worker and Hyperdrive on the edge, as close to your Worker as possible (see diagram, label _1. Connection setup_). This incurs significantly less latency, since the connection setup is completed within the same location.
3335

36+
Learn more about how connections work between Workers and Hyperdrive in [Connection lifecycle](/hyperdrive/concepts/connection-lifecycle/).
37+
3438
### 2. Connection Pooling
3539

3640
Hyperdrive creates a pool of connections to your database that can be reused as your application executes queries against your database.
@@ -45,6 +49,8 @@ If the connection pool does not have pre-existing connections, it will establish
4549
Hyperdrive automatically manages the connection pool properties for you, including limiting the total number of connections to your origin database. Refer to [Limits](/hyperdrive/platform/limits/) to learn more.
4650
:::
4751

52+
Learn more about connection pooling behavior and configuration in [Connection pooling](/hyperdrive/concepts/connection-pooling/).
53+
4854
### 3. Query Caching
4955

5056
Hyperdrive supports caching of non-mutating (read) queries to your database.
@@ -55,16 +61,14 @@ For non-mutating queries, Hyperdrive will cache the response for the configured
5561

5662
Caching reduces the burden on your origin database and accelerates the response times for your queries.
5763

58-
## Pooling mode
64+
Learn more about query caching behavior and configuration in [Query caching](/hyperdrive/concepts/query-caching/).
5965

60-
The Hyperdrive connection pooler operates in transaction mode, where the client that executes the query communicates through a single connection for the duration of a transaction. When that transaction has completed, the connection is returned to the pool.
61-
62-
Hyperdrive supports [`SET` statements](https://www.postgresql.org/docs/current/sql-set.html) for the duration of a transaction or a query. For instance, if you manually create a transaction with `BEGIN`/`COMMIT`, `SET` statements within the transaction will take effect. Moreover, a query that includes a `SET` command (`SET X; SELECT foo FROM bar;`) will also apply the `SET` command. When a connection is returned to the pool, the connection is `RESET` such that the `SET` commands will not take effect on subsequent queries.
63-
64-
This implies that a single Worker invocation may obtain multiple connections to perform its database operations and may need to `SET` any configurations for every query or transaction. It is not recommended to wrap multiple database operations with a single transaction to maintain the `SET` state. Doing so will affect the performance and scaling of Hyperdrive as the connection cannot be reused by other Worker isolates for the duration of the transaction.
66+
## Pooling mode
6567

66-
Hyperdrive supports named prepared statements as implemented in the `postgres.js` and `node-postgres` drivers. Named prepared statements in other drivers may have worse performance or may not be supported.
68+
<Render file="pooling-mode" product="hyperdrive" />
6769

6870
## Related resources
6971

70-
- [Query caching](/hyperdrive/configuration/query-caching/)
72+
- [Connection lifecycle](/hyperdrive/concepts/connection-lifecycle/)
73+
- [Query caching](/hyperdrive/concepts/query-caching/)
74+
- [Connection pooling](/hyperdrive/concepts/connection-pooling/)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Concepts
4+
sidebar:
5+
order: 3
6+
group:
7+
hideIndex: true
8+
---
9+
10+
Learn about the core concepts and architecture behind Hyperdrive.

0 commit comments

Comments
 (0)