Muxio RPC Framework
Relevant source files
- Cargo.lock
- experiments/simd-r-drive-muxio-service-definition/Cargo.toml
- experiments/simd-r-drive-ws-client/Cargo.toml
- experiments/simd-r-drive-ws-server/Cargo.toml
Purpose and Scope
This document describes the Muxio RPC (Remote Procedure Call) framework as implemented in SIMD R Drive for remote storage access over WebSocket connections. The framework provides a type-safe, multiplexed communication protocol using bitcode serialization for efficient binary data transfer.
For information about the WebSocket server implementation, see WebSocket Server. For the native Rust client implementation, see Native Rust Client. For Python client integration, see Python WebSocket Client API.
Sources: experiments/simd-r-drive-ws-server/Cargo.toml:1-23 experiments/simd-r-drive-ws-client/Cargo.toml:1-22 experiments/simd-r-drive-muxio-service-definition/Cargo.toml:1-17
Architecture Overview
The Muxio RPC framework consists of multiple layers that work together to provide remote procedure calls over WebSocket connections:
Muxio RPC Framework Layer Architecture
graph TB
subgraph "Client Application Layer"
App["Application Code"]
end
subgraph "Client RPC Stack"
Caller["muxio-rpc-service-caller\nMethod Invocation"]
ClientRuntime["muxio-tokio-rpc-client\nWebSocket Client Runtime"]
end
subgraph "Shared Contract"
ServiceDef["simd-r-drive-muxio-service-definition\nService Interface Contract\nMethod Signatures"]
Bitcode["bitcode\nBinary Serialization"]
end
subgraph "Server RPC Stack"
ServerRuntime["muxio-tokio-rpc-server\nWebSocket Server Runtime"]
Endpoint["muxio-rpc-service-endpoint\nRequest Router"]
end
subgraph "Server Application Layer"
Impl["DataStore Implementation"]
end
subgraph "Core Framework"
Core["muxio-rpc-service\nBase RPC Traits & Types"]
end
App --> Caller
Caller --> ClientRuntime
ClientRuntime --> ServiceDef
ClientRuntime --> Bitcode
ClientRuntime --> Core
ServiceDef --> Bitcode
ServiceDef --> Core
ServerRuntime --> ServiceDef
ServerRuntime --> Bitcode
ServerRuntime --> Core
ServerRuntime --> Endpoint
Endpoint --> Impl
style ServiceDef fill:#f9f9f9,stroke:#333,stroke-width:2px
The framework is organized into distinct layers:
| Layer | Crates | Responsibility |
|---|---|---|
| Core Framework | muxio-rpc-service | Base traits, types, and RPC protocol definitions |
| Service Definition | simd-r-drive-muxio-service-definition | Shared interface contract between client and server |
| Serialization | bitcode | Efficient binary encoding/decoding of messages |
| Client Runtime | muxio-tokio-rpc-client, muxio-rpc-service-caller | WebSocket client, method invocation, request management |
| Server Runtime | muxio-tokio-rpc-server, muxio-rpc-service-endpoint | WebSocket server, request routing, response handling |
Sources: Cargo.lock:1250-1336 experiments/simd-r-drive-ws-server/Cargo.toml:14-17 experiments/simd-r-drive-ws-client/Cargo.toml:14-21
Core Framework Components
muxio-rpc-service
The muxio-rpc-service crate provides the foundational abstractions for the RPC system:
Core RPC Framework Types
graph LR
subgraph "muxio-rpc-service Core Types"
RpcService["RpcService Trait\nService Interface"]
Request["Request Type\nmethod_id + payload"]
Response["Response Type\nresult or error"]
Error["RpcError\nError Handling"]
end
RpcService -->|defines| Request
RpcService -->|defines| Response
Response -->|contains| Error
Key components in muxio-rpc-service:
| Component | Type | Purpose |
|---|---|---|
| RpcService | Trait | Defines the service interface with method dispatch |
| Request | Struct | Contains method ID and serialized payload |
| Response | Enum | Success with payload or error variant |
| method_id | Hash | XXH3 hash of method signature for routing |
Sources: Cargo.lock:1261-1272
Service Definition Layer
simd-r-drive-muxio-service-definition
The service definition crate serves as the shared contract between clients and servers. It defines the exact interface of the DataStore RPC service:
DataStore RPC Service Methods
graph TB
subgraph "Service Definition Structure"
ServiceDef["DataStoreService\nService Definition"]
Methods["Method Definitions"]
Types["Request/Response Types"]
Write["write\n(key, payload) → offset"]
Read["read\n(key) → Option<bytes>"]
Delete["delete\n(key) → bool"]
Exists["exists\n(key) → bool"]
BatchWrite["batch_write\n(entries) → Vec<offset>"]
BatchRead["batch_read\n(keys) → Vec<Option<bytes>>"]
end
ServiceDef --> Methods
ServiceDef --> Types
Methods --> Write
Methods --> Read
Methods --> Delete
Methods --> Exists
Methods --> BatchWrite
Methods --> BatchRead
Types -->|uses| Bitcode["bitcode Serialization"]
The service definition establishes a type-safe contract for all operations:
| Method | Request Type | Response Type | Description |
|---|---|---|---|
write | (Vec<u8>, Vec<u8>) | Result<u64, Error> | Write key-value pair, return offset |
read | Vec<u8> | Result<Option<Vec<u8>>, Error> | Read value by key |
delete | Vec<u8> | Result<bool, Error> | Delete entry, return success |
exists | Vec<u8> | Result<bool, Error> | Check key existence |
batch_write | Vec<(Vec<u8>, Vec<u8>)> | Result<Vec<u64>, Error> | Write multiple entries |
batch_read | Vec<Vec<u8>> | Result<Vec<Option<Vec<u8>>>, Error> | Read multiple entries |
flowchart LR
Signature["Method Signature\n'write(key,payload)'"]
Hash["XXH3 Hash"]
MethodID["method_id: u64"]
Signature -->|hash| Hash
Hash --> MethodID
MethodID -->|used for routing| Router["Request Router"]
Method ID Generation
Each method is identified by an XXH3 hash of its signature, enabling fast routing without string comparisons:
Method Identification Flow
Sources: experiments/simd-r-drive-muxio-service-definition/Cargo.toml:1-17 Cargo.lock:1261-1272
graph LR
subgraph "Bitcode Serialization Pipeline"
RustType["Rust Type\n(Request/Response)"]
Encode["bitcode::encode"]
Binary["Binary Payload\nCompact Format"]
Decode["bitcode::decode"]
RustType2["Rust Type\n(Reconstructed)"]
end
RustType -->|serialize| Encode
Encode --> Binary
Binary -->|deserialize| Decode
Decode --> RustType2
Bitcode Serialization
The framework uses the bitcode crate for efficient binary serialization with the following characteristics:
Serialization Features
Bitcode Encoding/Decoding Pipeline
| Feature | Description | Benefit |
|---|---|---|
| Zero-copy deserialization | References data without copying when possible | Minimal overhead for large payloads |
| Compact encoding | Space-efficient binary format | Reduced network bandwidth |
| Type safety | Compile-time type checking | Prevents serialization errors |
| Performance | Faster than JSON/MessagePack | Lower CPU overhead |
Integration with RPC
The serialization is integrated at multiple points:
- Request serialization : Client encodes method arguments to binary
- Wire transfer : Binary payload sent over WebSocket
- Response serialization : Server encodes return values to binary
- Deserialization : Both sides decode binary to Rust types
Sources: Cargo.lock:392-414 experiments/simd-r-drive-muxio-service-definition/Cargo.toml14
Client-Side Components
muxio-rpc-service-caller
The caller component provides the client-side method invocation interface:
Client Method Invocation Flow
Key responsibilities:
- Method call marshalling
- Request ID generation
- Response awaiting and matching
- Error propagation
Sources: Cargo.lock:1274-1285 experiments/simd-r-drive-ws-client/Cargo.toml18
muxio-tokio-rpc-client
The client runtime handles WebSocket communication and multiplexing:
Client Runtime Request/Response Multiplexing
The client runtime provides:
| Feature | Implementation | Purpose |
|---|---|---|
| Connection management | Single WebSocket connection | Persistent connection |
| Request multiplexing | Multiple concurrent requests | High throughput |
| Response routing | Map of pending requests | Match responses to callers |
| Async/await support | Tokio-based futures | Non-blocking I/O |
Sources: Cargo.lock:1302-1318 experiments/simd-r-drive-ws-client/Cargo.toml16
flowchart TB
subgraph "Server Request Processing"
Receive["Receive Request\nWebSocket"]
Deserialize["Deserialize\nbitcode::decode"]
ExtractID["Extract method_id"]
Route["Route to Handler"]
Execute["Execute Method"]
Serialize["Serialize Response\nbitcode::encode"]
Send["Send Response\nWebSocket"]
end
Receive --> Deserialize
Deserialize --> ExtractID
ExtractID --> Route
Route --> Execute
Execute --> Serialize
Serialize --> Send
Server-Side Components
muxio-rpc-service-endpoint
The endpoint component routes incoming RPC requests to service implementations:
Server Request Processing Pipeline
Endpoint responsibilities:
| Responsibility | Description |
|---|---|
| Request routing | Maps method_id to handler function |
| Execution | Invokes the appropriate service method |
| Error handling | Catches and serializes errors |
| Response formatting | Wraps results in response envelope |
Sources: Cargo.lock:1287-1300 experiments/simd-r-drive-ws-server/Cargo.toml17
muxio-tokio-rpc-server
The server runtime manages WebSocket connections and request dispatching:
Server Runtime Connection Management
Key features:
| Feature | Implementation | Purpose |
|---|---|---|
| Multi-client support | One task pair per connection | Concurrent clients |
| Backpressure handling | Bounded channels | Prevent memory exhaustion |
| Graceful shutdown | Signal-based termination | Clean connection closure |
| Error isolation | Per-connection error handling | Fault tolerance |
Sources: Cargo.lock:1320-1336 experiments/simd-r-drive-ws-server/Cargo.toml16
Request/Response Flow
Complete RPC Call Sequence
End-to-End RPC Call Flow
Message Format
The wire protocol uses a structured binary format:
| Field | Type | Size | Description |
|---|---|---|---|
request_id | u64 | 8 bytes | Unique request identifier |
method_id | u64 | 8 bytes | XXH3 hash of method signature |
payload_len | u32 | 4 bytes | Length of payload |
payload | [u8] | Variable | Bitcode-encoded arguments/result |
Sources: All component sources above
Error Handling
The framework provides comprehensive error handling across the RPC boundary:
RPC Error Classification and Propagation
Error Categories
| Category | Origin | Handling |
|---|---|---|
| Serialization errors | Bitcode encoding/decoding failure | Logged and returned as RpcError |
| Network errors | WebSocket connection issues | Automatic reconnect or error propagation |
| Application errors | DataStore operation failures | Serialized and returned to client |
| Timeout errors | Request took too long | Client-side timeout with error result |
Error Recovery
The framework implements several recovery strategies:
- Connection loss : Client automatically attempts reconnection
- Request timeout : Client cancels pending request after configured duration
- Serialization failure : Error logged and generic error returned
- Invalid method ID : Server returns "method not found" error
Sources: Cargo.lock:1261-1336
Performance Characteristics
The Muxio RPC framework is optimized for high-performance remote storage access:
| Metric | Characteristic | Impact |
|---|---|---|
| Serialization overhead | ~50-100 ns for typical payloads | Minimal CPU impact |
| Request multiplexing | Thousands of concurrent requests | High throughput |
| Binary protocol | Compact wire format | Reduced bandwidth usage |
| Zero-copy deserialization | Direct memory references | Lower latency for large payloads |
The use of bitcode serialization and WebSocket binary frames minimizes overhead compared to text-based protocols like JSON over HTTP. The multiplexed architecture allows clients to issue multiple concurrent requests without blocking, essential for high-performance batch operations.
Sources: Cargo.lock:392-414 Cargo.lock:1250-1336