Native API

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]
PartMeaning
lencounts everything after itself: at least 5, at most MAX_FRAME (16 MiB)
kindrequest kinds are below 0x80, reply kinds 0x80 and above
idchosen by the client and echoed in the reply, so replies may arrive in any order
bodyintegers 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#

RequestKindBody
Hello0x01[version varint][token bytes]
Ping0x02empty
Get0x03[key bytes]
GetBatch0x04[count][key bytes]*
Put0x05[key bytes][document: the rest]
Insert0x06same as Put; fails if the key exists
Delete0x07[key bytes]
Filter0x08[flags u8][limit varint][cursor varint][filter text: the rest]
Cancel0x09[target id varint]
FieldId0x0A[field name: the rest]
PutJson0x0B[key bytes][JSON object text: the rest]
CreateIndex0x0C[ordered u8][field name: the rest]
Use0x0D[db bytes][table: the rest]
Stream, Defer, Sync, Project0x13 to 0x16version 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#

ReplyKindBody
HelloOk0x81[version varint]
Pong0x82empty
Found0x83the document (the rest)
NotFound0x84empty
Docs0x85[count][doc bytes]*[next cursor varint]; an empty entry is a miss in a GetBatch; cursor 0 means no more pages
Done0x86[existed u8]: a write that is applied and durable
Count0x87[n varint]
Error0x88[code varint][message: the rest]
FieldId0x89[id varint]

What each request does#

RequestResult
PingPong
GetFound or NotFound
PutDone{existed} after the write is durable (an existing key is replaced)
InsertDone after it is durable, or error EXISTS
DeleteDone{existed}; deleting a missing key is answered at once
PutJsonlike Put, the document given as JSON text that the server converts
FieldIdthe dictionary id of a field name, allocated if new
CreateIndexDone when every shard has built the index and committed its definition (idempotent)
FilterCount, or Docs with a cursor
GetBatch, Cancelthe 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