slotbus-hub
HTTP-to-shared-memory router for same-machine microservices. Workers register routes via HTTP. Clients send normal HTTP requests. The hub dispatches them through shared memory with sub-millisecond round trips. Multiple workers behind a single port, zero HTTP overhead on the hot path.
Built on slotbus, a lock-free shared memory IPC library for Rust.
Why slotbus-hub?
You have services running on the same machine that communicate over HTTP. Each request pays for TCP handshakes, socket copies, HTTP parsing, and JSON serialization — overhead that adds 5-20ms per round trip to what should be a local function call.
slotbus-hub replaces that hot path with shared memory. From the outside, it looks like a normal HTTP server. On the inside, requests are dispatched to workers through memory-mapped slots with 0.1-0.8ms round trips — 10-50x faster than localhost HTTP.
- Drop-in HTTP gateway — clients don't know they're using shared memory
- Dynamic route registration — workers register routes at startup, no config files
- Multiple workers — route different paths to different worker processes
- SSE event stream — workers push events, clients subscribe via
/events - Sub-millisecond dispatch — the entire SHM round trip takes 0.1-0.8ms
Installation
Quick Start
# Start the hub on the default port (3200)
# Custom port and timeout
# With latency instrumentation
CLI Flags
| Flag | Default | Description |
|---|---|---|
--port |
3200 |
HTTP listen port |
--timeout |
30 |
Request timeout in seconds. Returns 504 Gateway Timeout if a worker doesn't respond in time. |
--slots |
32 |
Number of SHM slots per worker. More slots = more concurrent in-flight requests. |
--region-size |
1048576 |
SHM region size per worker in bytes (default 1 MB). |
--instrumentation |
false |
Enable latency instrumentation logging via tracing. |
Logging is controlled by the RUST_LOG environment variable (default: info).
How It Works
Client Hub Worker
| | |
| HTTP GET /api/items/42 | |
| -----------------------> | |
| | match route, find worker |
| | write to SHM slot |
| | signal req event |
| | -------------------------> |
| | | claim slot
| | | read request
| | | process...
| | | write response
| | signal rsp event | to SHM slot
| | <------------------------- |
| | read response from slot |
| HTTP 200 OK | |
| <----------------------- | |
| | |
| total: 0.1-0.8ms | |
- Client sends a standard HTTP request to the hub.
- Hub matches the path against registered worker routes.
- Hub serializes the request metadata (path, headers, query params, path params) and body into a shared memory slot.
- Hub signals the worker via an OS-level named event (sub-microsecond wake).
- Worker claims the slot, processes the request, writes the response back to shared memory.
- Worker signals the hub. The hub reads the response and returns it to the client as a normal HTTP response.
The entire SHM round trip takes 0.1-0.8 ms. See the slotbus library for details on the shared memory transport, slot state machine, and overflow handling.
Worker Registration
Workers register their routes on startup via POST /internal/register:
Response:
The hub creates a shared memory region (hub-my-worker) and named events for the worker. The worker opens the region using the returned shm_name and starts its receive loop via the slotbus crate.
Path Parameters
Routes support {param} placeholders. When GET /api/items/42 matches the pattern /api/items/{id}, the hub extracts {"id": "42"} and includes it in the request metadata dispatched to the worker.
Stale Worker Cleanup
When a worker re-registers with the same name, the hub automatically removes the old registration and creates a fresh SHM region. This handles worker restarts without manual cleanup.
SSE Event Stream
The hub provides a unified Server-Sent Events stream at GET /events. Workers push events via POST /internal/emit, and the hub multiplexes them into a single stream tagged with the worker's source field.
# Subscribe to all worker events
Workers emit events like this:
The SSE stream uses keep-alive to maintain the connection. Events arrive as standard SSE with the event field set to event_type and the data field containing the full JSON payload including the source.
Health Check
API Reference
| Endpoint | Method | Description |
|---|---|---|
/internal/register |
POST |
Register a worker with name and routes. Returns worker_id and shm_name. |
/internal/emit |
POST |
Push an event from a worker for SSE broadcast. |
/events |
GET |
Unified SSE stream of all worker events. |
/health |
GET |
List connected workers and their route counts. |
* (all other paths) |
any | Proxied to the matching worker via SHM. Returns 502 if no route matches, 504 on timeout. |
Building Workers
The hub is just the router. To build a worker that connects to it, use the slotbus library crate:
[]
= "0.1"
A minimal worker: register routes with the hub via HTTP, open the SHM region using the returned shm_name, and start the receive loop. See the slotbus README for the worker-side API.
Platform Support
slotbus-hub inherits platform support from the slotbus library:
| Platform | Status |
|---|---|
| Windows | Supported |
| Linux | Supported |
| macOS | Supported |
Minimum Supported Rust Version
1.75
License
Licensed under the MIT License.