|
| 1 | +--- |
| 2 | +pcx_content_type: navigation |
| 3 | +title: Multi Workers development |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | +head: [] |
| 7 | +description: Learn how to develop with multiple Workers using different approaches and configurations. |
| 8 | +--- |
| 9 | + |
| 10 | +import { Aside, PackageManagers } from "~/components"; |
| 11 | + |
| 12 | +When building complex applications, you may need to work with multiple Workers during development. This guide covers the different approaches for running multiple Workers locally and when to use each approach. |
| 13 | + |
| 14 | +## Running in a single process |
| 15 | + |
| 16 | +Run multiple Workers in a single process by passing multiple configuration files to `wrangler dev`. Each configuration should point to a different Worker: |
| 17 | + |
| 18 | +<PackageManagers |
| 19 | + type="exec" |
| 20 | + pkg="wrangler" |
| 21 | + args="dev -c wrangler.toml -c ../other-worker/wrangler.jsonc" |
| 22 | +/> |
| 23 | + |
| 24 | +The first configuration will be treated as the **primary Worker**, exposed over HTTP at `http://localhost:8787`. Additional configurations become **secondary Workers**, accessible only via service bindings from the primary Worker. |
| 25 | + |
| 26 | +### Using Vite plugin |
| 27 | + |
| 28 | +If you are using the Vite plugin, configure `auxiliaryWorkers` in your Vite configuration: |
| 29 | + |
| 30 | +```js title="vite.config.js" |
| 31 | +import { defineConfig } from "vite"; |
| 32 | +import { cloudflare } from "@cloudflare/vite-plugin"; |
| 33 | + |
| 34 | +export default defineConfig({ |
| 35 | + plugins: [ |
| 36 | + cloudflare({ |
| 37 | + configPath: "./wrangler.toml", |
| 38 | + auxiliaryWorkers: [ |
| 39 | + { |
| 40 | + configPath: "../other-worker/wrangler.jsonc", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }), |
| 44 | + ], |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +<Aside type="note"> |
| 49 | +Auxiliary Workers are additional Workers used as part of your application. Use [service bindings](/workers/runtime-apis/bindings/service-bindings/) to call auxiliary Workers from your main Worker. All requests are routed through your primary Worker. |
| 50 | +</Aside> |
| 51 | + |
| 52 | +## Running in multiple processes |
| 53 | + |
| 54 | +Run separate development sessions for independent Workers. Each session runs in its own process with its own configuration: |
| 55 | + |
| 56 | +<PackageManagers |
| 57 | + type="exec" |
| 58 | + pkg="wrangler" |
| 59 | + args="dev" |
| 60 | + comment="Terminal 1 - First Worker" |
| 61 | +/> |
| 62 | + |
| 63 | +<PackageManagers |
| 64 | + type="exec" |
| 65 | + pkg="vite" |
| 66 | + args="dev" |
| 67 | + comment="Terminal 2 - Second Worker" |
| 68 | +/> |
| 69 | + |
| 70 | +Each development session runs independently, but you can still connect them using service bindings. |
| 71 | + |
| 72 | +## Hybrid approach |
| 73 | + |
| 74 | +Combine single and multiple process approaches. For example, run multiple Workers through Vite while connecting to a separate Worker running in its own `wrangler dev` session: |
| 75 | + |
| 76 | +<PackageManagers |
| 77 | + type="exec" |
| 78 | + pkg="vite dev" |
| 79 | + comment="Terminal 1 - Vite with auxiliary Workers" |
| 80 | +/> |
| 81 | + |
| 82 | +<PackageManagers |
| 83 | + type="exec" |
| 84 | + pkg="wrangler dev" |
| 85 | + comment="Terminal 2 - Separate Worker" |
| 86 | +/> |
| 87 | + |
| 88 | +This approach provides flexibility when working with a mix of related and independent Workers. |
| 89 | + |
| 90 | +## When to use each approach |
| 91 | + |
| 92 | +### Single process |
| 93 | +Use when: |
| 94 | +- Workers are closely related or part of the same application |
| 95 | +- You need full support for service bindings, Durable Objects, and Tail consumers |
| 96 | +- You want the simplest development setup |
| 97 | + |
| 98 | +### Multiple processes |
| 99 | +Use when: |
| 100 | +- Workers are independent projects with different build setups |
| 101 | +- Different teams maintain the Workers |
| 102 | +- You need flexibility to run Workers together only when needed |
| 103 | + |
| 104 | +<Aside type="caution"> |
| 105 | +The multiple processes approach has limitations: |
| 106 | +- Only `wrangler dev` sessions with a single configuration support cross-process Durable Objects namespaces |
| 107 | +- Cross-process communication is restricted to fetch requests only (no RPC calls) |
| 108 | +- `wrangler dev` with multiple configurations does not support hybrid mode bindings to Workers running in different processes |
| 109 | +</Aside> |
| 110 | + |
| 111 | +We are actively working to address these limitations in future releases. |
0 commit comments