Wire protocol
The frame layout and every request and reply of the Raft DB binary protocol, as implemented by the raftdb-proto crate.
The binary protocol is implemented by the raftdb-proto crate: a codec with no I/O (bytes to requests and replies and back). It depends only on raftdb-doc for varints, uses no unsafe, and never panics on damaged input.
Frame#
[len u32 LE][kind u8][id u32 LE][body]| Part | Meaning |
|---|---|
len | counts everything after itself: at least 5, at most MAX_FRAME (16 MiB) |
kind | request kinds are below 0x80, reply kinds 0x80 and above |
id | chosen by the client and echoed in the reply, so replies may arrive in any order |
| body | integers are varints; a byte string is [len varint][bytes] |
frame(buf) returns Ok(None) when more bytes are needed, otherwise the frame and the number of bytes it used, so pipelined frames are taken one after another.
Requests#
| Request | Kind | Body |
|---|---|---|
Hello | 0x01 | [version varint][token bytes] |
Ping | 0x02 | empty |
Get | 0x03 | [key bytes] |
GetBatch | 0x04 | [count][key bytes]* |
Put | 0x05 | [key bytes][document: the rest] |
Insert | 0x06 | same as Put; fails if the key exists |
Delete | 0x07 | [key bytes] |
Filter | 0x08 | [flags u8][limit varint][cursor varint][filter text: the rest] |
Cancel | 0x09 | [target id varint] |
FieldId | 0x0A | [field name: the rest] |
PutJson | 0x0B | [key bytes][JSON object text: the rest] |
CreateIndex | 0x0C | [ordered u8][field name: the rest] |
Use | 0x0D | [db bytes][table: the rest] |
Stream, Defer, Sync, Project | 0x13 to 0x16 | version 2 only, see Connections, errors and versions |
Filter flags: bit 0 count only, bit 1 documents as JSON text, bit 2 the text is SQL instead of a JSON filter. Flag bytes outside the defined bits are an error.
Replies#
| Reply | Kind | Body |
|---|---|---|
HelloOk | 0x81 | [version varint] |
Pong | 0x82 | empty |
Found | 0x83 | the document (the rest) |
NotFound | 0x84 | empty |
Docs | 0x85 | [count][doc bytes]*[next cursor varint]; an empty entry is a miss in a GetBatch; cursor 0 means no more pages |
Done | 0x86 | [existed u8]: a write that is applied and durable |
Count | 0x87 | [n varint] |
Error | 0x88 | [code varint][message: the rest] |
FieldId | 0x89 | [id varint] |
What each request does#
| Request | Result |
|---|---|
Ping | Pong |
Get | Found or NotFound |
Put | Done{existed} after the write is durable (an existing key is replaced) |
Insert | Done after it is durable, or error EXISTS |
Delete | Done{existed}; deleting a missing key is answered at once |
PutJson | like Put, the document given as JSON text that the server converts |
FieldId | the dictionary id of a field name, allocated if new |
CreateIndex | Done when every shard has built the index and committed its definition (idempotent) |
Filter | Count, or Docs with a cursor |
GetBatch, Cancel | the codec has them; the server answers NOT_SUPPORTED (pipeline Gets instead) |
Documents in a body#
A document travels in the stored binary form of raftdb-doc (Documents and schemas), so the server does not convert it; or as JSON text when asked (PutJson; Filter with the JSON flag). One document must fit in one frame (16 MiB): streaming of larger documents is not built.
Filter pages#
Filter carries limit (default 100 on the server, at most 10,000, also capped by about 8 MB of reply) and cursor (0 to start). The reply Docs ends with the next cursor; 0 means the last page. The client should treat the cursor as opaque. Pages are consistent only while the data does not change: there is no snapshot isolation yet. For a SQL query with ORDER BY the cursor is the number of rows already returned.
Parsing rules#
Parsing is zero-copy and every length is checked against the bytes present. A body must be consumed exactly (leftover bytes are an error), a list count larger than the bytes left is refused before any loop, flags must be 0 or 1, and text must be UTF-8. Errors of the codec: BadLength, BadKind, BadBody, BadUtf8. The server answers a malformed frame with an error reply and closes the connection.
Measured#
raftdb-bench proto on Windows 11, one core, one operation = encode, find the frame, parse: Get with a 13-byte key 49.9 M/s, Put with 13 + 111 bytes 37.9 M/s, Found with a 111-byte document 52.9 M/s, GetBatch of 32 keys 93 M keys/s.
Tests#
Six tests in raftdb-proto: every message round-trips; exact bytes are pinned; partial input asks for more and pipelined frames split at every cut; bad lengths, kinds, trailing bytes, bad UTF-8 and lying counts are errors; every single-bit flip of valid frames and 200,000 random inputs parse without a panic.
Not supported yet#
Compression, streaming of very large documents, a change feed (Subscribe), version negotiation beyond "equal or refuse" for version 1.
See also#
Connections, errors and versions, Pipelining and batching, Filter language