rivven-protocol
Wire protocol types for the Rivven distributed event streaming platform.
Overview
This crate defines canonical wire protocol types for client-server communication. Both rivven-client and rivvend depend on this crate to ensure wire compatibility.
Features
| Category | Types |
|---|---|
| Requests | Produce, Consume, CreateTopic, DeleteTopic, Metadata, etc. |
| Responses | ProduceResponse, ConsumeResponse, MetadataResponse, etc. |
| Messages | MessageData with key, value, headers, timestamp |
| Metadata | BrokerInfo, TopicMetadata, PartitionMetadata |
Protocol Constants
| Constant | Value | Description |
|---|---|---|
PROTOCOL_VERSION |
1 | Current wire protocol version |
MAX_MESSAGE_SIZE |
64 MB | Maximum serialized message size |
Usage
use ;
// Create a produce request
let request = Publish ;
// Serialize with wire format byte prefix (recommended)
let wire_bytes = request.to_wire?;
// Deserialize with auto-detection
let = from_wire?;
assert_eq!;
Wire Format
Messages use a format-prefixed wire protocol:
┌─────────────────┬──────────────────┬─────────────────────────┐
│ Length (4 bytes)│ Format (1 byte) │ Payload (N bytes) │
│ Big-endian u32 │ 0x00 = postcard │ Serialized Request/Resp │
│ │ 0x01 = protobuf │ │
└─────────────────┴──────────────────┴─────────────────────────┘
Format Byte Values
| Byte | Format | Description |
|---|---|---|
0x00 |
postcard | Rust-native, fastest (~50ns serialize) |
0x01 |
protobuf | Cross-language compatible |
Serialization Methods
| Method | Description |
|---|---|
to_wire(format) |
Serialize with format byte prefix |
from_wire(data) |
Deserialize with auto-format detection |
to_bytes() |
Postcard only, no format prefix (legacy) |
from_bytes(data) |
Postcard only, no format prefix (legacy) |
Server Behavior
The server auto-detects the wire format from the first byte and responds in the same format the client used.
Protobuf Feature
Enable protobuf serialization with the protobuf feature:
[]
= { = "0.0.11", = ["protobuf"] }
use ;
// Serialize request as protobuf
let request = Ping;
let wire_bytes = request.to_wire?;
// First byte is 0x01 (protobuf)
assert_eq!;
Cross-Language Support
Architecture
The Rivven broker uses postcard for maximum performance with Rust clients. For other languages, the protocol is documented via Protocol Buffers:
┌─────────────────┐ ┌─────────────────┐
│ Rust Client │───── postcard ────►│ │
│ (rivven-client) │◄──── postcard ─────│ rivvend │
└─────────────────┘ │ (broker) │
└─────────────────┘
Multi-Language Clients
For Go, Java, Python, and other languages, use the proto file as the wire format specification:
| Format | Use Case | Performance |
|---|---|---|
| postcard | Rust clients (native) | Fastest (~50ns serialize) |
| protobuf | Reference spec for other languages | Fast (~200ns serialize) |
Protobuf Schema
The proto file at proto/rivven.proto documents the wire format. Client implementers specify their own package names:
package rivven.protocol.v1;
message PublishRequest {
string topic = 1;
optional uint32 partition = 2;
Record record = 3;
}
message Record {
bytes key = 1;
bytes value = 2;
repeated RecordHeader headers = 3;
int64 timestamp = 4;
}
Generate Client Stubs
# Go
# Java
# Python
# TypeScript (ts-proto)
Implementing a Client
To implement a client in another language:
- Generate code from
proto/rivven.proto - Connect to broker on port 9092
- Use length-prefixed framing:
[4-byte length][1-byte format][payload] - Use format byte
0x01for protobuf - Serialize requests using protobuf
- Parse responses using protobuf (server responds in same format)
Proto Message Types
The protobuf schema defines 31+ message types covering all major protocol operations:
| Category | Message Types |
|---|---|
| Core | Request, Response, Record, ErrorCode |
| Publish/Consume | PublishRequest, BatchPublishRequest, ConsumeResponse |
| Metadata | GetMetadataResponse, GetClusterMetadataResponse, GetOffsetBoundsResponse |
| Consumer Groups | ListGroupsResponse, DescribeGroupResponse, DeleteGroupResponse |
| Transactions | BeginTransactionResponse, CommitTransactionResponse, AbortTransactionResponse |
| Idempotent | InitProducerIdResponse, IdempotentPublishResponse |
| Admin | AlterTopicConfigRequest, DescribeTopicConfigsResponse, CreatePartitionsRequest, DeleteRecordsRequest |
| Time Queries | GetOffsetForTimestampRequest / Response |
License
Apache-2.0. See LICENSE.