Hyperdrive does not work localhost when running 2 workers in the same process
What versions & operating system are you using?
wrangler v4.16, windows
Please provide a link to a minimal reproduction
No response
Describe the Bug
When using 2 wrangler in localhost mode: aka wrangler dev -c worker1/wrangler.jsonc -c worker2/wrangler.jsonc
then Wrangler only passes the Hyperdrive env vars to the primary worker, but not to secondaries - if the secondary has his own hyperdrive config, it just does not work. Making RPC development in localhost mode extremely painful.
you see that the hyperdrive string is wrong - compared to run it seperately.
Please provide any relevant error logs
No response
Hi @piffie currently Hyperdrive doesn't work on localhost while connected to the remote database. It's a complicated issue due to the way how integration between Hyperdrive and Workers Runtime has been done. There is no ETA when this could be resolved.
@piffie When using multi-worker mode locally each wrangler json configuration passed in spins up a separate worker and respects binding isolation just as it would in production which is why you can't just call the second hyperdrive binding directly in the primary worker. This is not a bug this is just how workers are designed.
However you use RPC here for your use case. You would need to set up the second worker as an RPC worker and expose a RPC method call that will call the second worker's inner hyperdrive binding. I've tested this myself locally and this works as expected today.
Here's a full example
Directory structure:
worker-a/
wrangler.jsonc
src/index.ts
worker-b/
wrangler.jsonc
src/index.ts
worker-b/wrangler.jsonc:
{
"name": "worker-b",
"main": "src/index.ts",
"compatibility_date": "2025-01-01",
"compatibility_flags": ["nodejs_compat"],
"hyperdrive": [
{
"binding": "DB",
"id": "<worker-b-hyperdrive-id>",
"localConnectionString": "postgresql://user:password@localhost:5432/db_b"
}
]
}
worker-b/src/index.ts:
import { WorkerEntrypoint } from "cloudflare:workers";
import { Client } from "pg";
interface Env {
DB: Hyperdrive;
}
export class DatabaseService extends WorkerEntrypoint<Env> {
async query(sql: string) {
const client = new Client({ connectionString: this.env.DB.connectionString });
await client.connect();
const result = await client.query(sql);
this.ctx.waitUntil(client.end());
return result.rows;
}
}
export default {
fetch() {
return new Response("worker-b");
},
} satisfies ExportedHandler<Env>;
worker-a/wrangler.jsonc:
{
"name": "worker-a",
"main": "src/index.ts",
"compatibility_date": "2025-01-01",
"compatibility_flags": ["nodejs_compat"],
"hyperdrive": [
{
"binding": "DB",
"id": "<worker-a-hyperdrive-id>",
"localConnectionString": "postgresql://user:password@localhost:5432/db_a"
}
],
"services": [
{
"binding": "WORKER_B",
"service": "worker-b",
"entrypoint": "DatabaseService"
}
]
}
worker-a/src/index.ts:
import { Client } from "pg";
import type { DatabaseService } from "../../worker-b/src/index";
interface Env {
DB: Hyperdrive;
WORKER_B: Service<DatabaseService>;
}
export default {
async fetch(request, env, ctx): Promise<Response> {
// Query worker-a's own Hyperdrive binding directly
const client = new Client({ connectionString: env.DB.connectionString });
await client.connect();
const localResult = await client.query("SELECT NOW() AS time, 'worker-a' AS source");
ctx.waitUntil(client.end());
// Query worker-b's Hyperdrive binding over RPC
const remoteResult = await env.WORKER_B.query("SELECT NOW() AS time, 'worker-b' AS source");
return Response.json({
workerA: localResult.rows,
workerB: remoteResult,
});
},
} satisfies ExportedHandler<Env>;
Run:
cd worker-a
npx wrangler dev -c wrangler.jsonc -c ../worker-b/wrangler.jsonc
Also, in case it helps, we now support RPC between Wrangler processes - so you might also be able to get the original setup working by running multiple wrangler dev instance...
Closing as this is working as expected.