Core components
Server layer
The server layer orchestrates the entire runtime and is defined insrc/workerd/server/:
- CliMain: Entry point that dispatches subcommands (
serve,compile,test) - Server class: Central orchestrator that parses Cap’n Proto configuration, constructs services, and manages the event loop
- WorkerdApi: Registers all JavaScript API types and compiles worker modules
Worker lifecycle
Workers progress through several stages managed by classes insrc/workerd/io/:
- Isolate: V8 isolate wrapper, shared across workers with identical configuration
- Script: Compiled JavaScript/WebAssembly bound to an isolate
- Worker: Worker instance that can handle multiple requests
- Actor: Durable Object instance with persistent storage
I/O subsystem
The I/O layer bridges JavaScript promises with the KJ async framework:- IoContext: Per-request context object (“god object”) accessible via
IoContext::current() - InputGate/OutputGate: Consistency primitives for Durable Object request ordering
- ActorCache: Write-back LRU cache over storage operations
- ActorSqlite: SQLite-backed storage implementation
JavaScript engine integration
The JSG (JavaScript Glue) layer insrc/workerd/jsg/ provides:
- Type wrappers between C++ and JavaScript
- Promise handling and memory management
- Module system implementation
- Automatic garbage collection integration
Request flow
When a request arrives at workerd:- Socket receives request: A configured socket (HTTP/HTTPS) receives the incoming request
- Service routing: The request is routed to the named service specified in the socket configuration
- Worker acquisition: An available worker instance is selected or created
- IoContext creation: A new per-request IoContext is established
- JavaScript execution: The worker’s fetch handler executes in V8
- I/O operations: Fetch calls, KV operations, and Durable Object calls flow through the I/O subsystem
- Response delivery: The Response object is serialized and sent back through the socket
Service types
workerd supports multiple service types beyond workers:Worker service
Executes JavaScript/WebAssembly code in response to requests. See Workers.Network service
Provides access to network resources with configurable allow/deny lists:External server
Forwards requests to a specific remote server:Disk directory
Exposes a local directory through an HTTP interface:Configuration system
workerd uses Cap’n Proto text format for configuration. The schema is defined insrc/workerd/server/workerd.capnp.
A minimal configuration requires:
- At least one service definition
- At least one socket to expose services
- For workers: compatibility date and source code
Thread model
workerd currently runs in a single-threaded event loop:- All JavaScript execution is single-threaded
- I/O operations are asynchronous and non-blocking
- Multiple worker instances cannot execute simultaneously on the same isolate
- To utilize multiple cores, run multiple workerd instances with load balancing
- No race conditions in JavaScript code
- Predictable performance characteristics
- Simple debugging and reasoning about execution
Dependencies
workerd builds on several core dependencies:| Dependency | Purpose |
|---|---|
| V8 | JavaScript engine |
| Cap’n Proto | Serialization, RPC, and KJ base library |
| BoringSSL | TLS and cryptography |
| SQLite | Durable Object storage backend |
| ICU | Internationalization |
Memory management
workerd uses tcmalloc for memory allocation, providing:- Fast allocation for small objects
- Reduced lock contention
- Better memory utilization
- V8’s garbage collector for JavaScript heap
- KJ’s reference counting for C++ objects
- IoOwn/IoPtr smart pointers for cross-heap references