Skip to content

Commit e56ec35

Browse files
committed
fix(api): 🐛 update database connection settings for improved performance
1 parent d858e91 commit e56ec35

4 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎apps/web/content/docs/dev/deployments/cloud/cloudflare.mdx‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ import { coreRelations } from '@vitnode/core/database/relations'
110110
import { drizzle } from 'drizzle-orm/postgres-js'
111111

112112
export const vitNodeApiConfig = buildApiConfig({
113-
// [!code ++:9]
113+
// [!code ++:10]
114114
dbProvider: scopedDatabase(() =>
115115
drizzle({
116116
connection: {
117117
url: process.env.POSTGRES_URL,
118-
max: 1,
118+
max: 5,
119+
max_pipeline: 1,
119120
},
120121
relations: coreRelations,
121122
}),
@@ -124,6 +125,8 @@ export const vitNodeApiConfig = buildApiConfig({
124125
})
125126
```
126127

128+
`max_pipeline: 1` sends each connection's next query only after the previous one answers. Drizzle runs every query as an unnamed statement, and [Hyperdrive](#connect-through-hyperdrive) never answers three or more of those queued on one connection, so a request that runs queries in parallel, such as the signed-in session, hangs without it. `max: 5` keeps parallel queries parallel, on separate connections.
129+
127130
On Node (`vite dev`, `db:migrate`) `scopedDatabase` keeps one client per process, so the same config serves both. It closes a Drizzle client on postgres.js or node-postgres; pass `scopedDatabase(create, { dispose })` for another driver.
128131

129132
</Step>
@@ -225,7 +228,8 @@ dbProvider: scopedDatabase(({ env }) =>
225228
drizzle({
226229
connection: {
227230
url: hyperdriveConnectionString(env) ?? process.env.POSTGRES_URL,
228-
max: 1,
231+
max: 5,
232+
max_pipeline: 1,
229233
},
230234
relations: coreRelations,
231235
}),
@@ -262,16 +266,17 @@ With `npx wrangler dev` running, `http://localhost:8787/api/health/ready` answer
262266

263267
## Troubleshooting
264268

265-
| Symptom | Cause and fix |
266-
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
267-
| The Worker ignores `.dev.vars`, and the bindings table lists none of its variables | Wrangler ran with `--config .output/server/wrangler.json`. Run `wrangler dev` from the app's folder without it. |
268-
| A Worker-rendered page fails with `n is not defined` in the browser | The Worker is minified with `keep_names` on. Set `"keep_names": false`. |
269-
| `CONNECTION_CLOSED` or `Cannot perform I/O on behalf of a different request` from the database | The client was created once at module load. Use `scopedDatabase`. |
270-
| `TLS_HANDSHAKE_FAILED` from the database, and `/api/health/ready` answers `503` | The database's certificate is not signed by a public CA, as with Supabase. [Connect through Hyperdrive](#connect-through-hyperdrive). |
271-
| Image uploads fail with "The image optimization library (sharp) failed to load" | Remove `storage.image` from the Worker's API config. |
272-
| Deploy fails with `total sourcemap size is too large` (code `10021`) | Set `"upload_source_maps": false` in `wrangler.jsonc`. |
273-
| Pages answer `503` and the logs show "Worker exceeded CPU time limit" after about 10 ms of CPU | The account is on Workers Free, which allows 10 ms of CPU per request. Upgrade to Workers Paid (30 s by default). |
274-
| Deploy fails because the script is too large | [Shrink the Worker](#shrink-the-worker), and check that you are on Workers Paid. |
269+
| Symptom | Cause and fix |
270+
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
271+
| The Worker ignores `.dev.vars`, and the bindings table lists none of its variables | Wrangler ran with `--config .output/server/wrangler.json`. Run `wrangler dev` from the app's folder without it. |
272+
| A Worker-rendered page fails with `n is not defined` in the browser | The Worker is minified with `keep_names` on. Set `"keep_names": false`. |
273+
| `CONNECTION_CLOSED` or `Cannot perform I/O on behalf of a different request` from the database | The client was created once at module load. Use `scopedDatabase`. |
274+
| `TLS_HANDSHAKE_FAILED` from the database, and `/api/health/ready` answers `503` | The database's certificate is not signed by a public CA, as with Supabase. [Connect through Hyperdrive](#connect-through-hyperdrive). |
275+
| Image uploads fail with "The image optimization library (sharp) failed to load" | Remove `storage.image` from the Worker's API config. |
276+
| Deploy fails with `total sourcemap size is too large` (code `10021`) | Set `"upload_source_maps": false` in `wrangler.jsonc`. |
277+
| Signed-in requests, such as `/api/@vitnode/core/users/session`, never answer while signed-out ones work | Queries pipelined through Hyperdrive. Set `max_pipeline: 1` on the Postgres connection, see [Give each request its own database client](#give-each-request-its-own-database-client). |
278+
| Pages answer `503` and the logs show "Worker exceeded CPU time limit" after about 10 ms of CPU | The account is on Workers Free, which allows 10 ms of CPU per request. Upgrade to Workers Paid (30 s by default). |
279+
| Deploy fails because the script is too large | [Shrink the Worker](#shrink-the-worker), and check that you are on Workers Paid. |
275280

276281
## Related
277282

‎apps/web/src/vitnode.api.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const vitNodeApiConfig = buildApiConfig({
9595
drizzle({
9696
connection: {
9797
url: hyperdriveConnectionString(env) ?? POSTGRES_URL,
98-
...(ON_WORKERS ? { max: 1 } : {}),
98+
...(ON_WORKERS ? { max: 5, max_pipeline: 1 } : {}),
9999
onnotice: () => undefined,
100100
},
101101
relations: coreRelations,

‎packages/create-vitnode-app/copy-of-vitnode-app/cloudflare/src/vitnode.api.config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const vitNodeApiConfig = buildApiConfig({
3030
drizzle({
3131
connection: {
3232
url: hyperdriveConnectionString(env) ?? POSTGRES_URL,
33-
max: 1,
33+
max: 5,
34+
max_pipeline: 1,
3435
onnotice: () => undefined,
3536
},
3637
relations: coreRelations,

‎packages/vitnode/src/cloudflare/database.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export const hyperdriveConnectionString = (
8686
* drizzle({
8787
* connection: {
8888
* url: hyperdriveConnectionString(env) ?? process.env.POSTGRES_URL,
89-
* max: 1,
89+
* max: 5,
90+
* max_pipeline: 1,
9091
* },
9192
* relations: coreRelations,
9293
* }),

0 commit comments

Comments
 (0)