Native API

Pipelining and batching

How to keep many requests in flight on one connection, match replies by id, and make writes cheaper with group commit and deferred answers.

Pipelining#

Every request carries an id chosen by the client, and the reply echoes it. A client may therefore write many requests before it reads any reply. The server reads whatever is available (many pipelined requests per read), splits the frames, and hands one batch of frames per shard per read to the shard threads.

Replies may come back in any order: a read answered at once can overtake a write that waits for the disk, and requests for different shards finish independently. The id identifies the reply.

The Rust client exposes this as send (returns the id), flush, and recv. The test pipelined_requests_get_the_right_replies_even_out_of_order writes 5,000 documents 500 at a time and then reads them back:

Rust
for chunk in (0..n).collect::<Vec<_>>().chunks(500) {
    for &i in chunk {
        c.send(&Request::Put { key: &i.to_le_bytes(), doc: &doc_n(i) });
    }
    c.flush()?;
    for _ in chunk {
        c.recv(|_, r| assert!(matches!(r, Reply::Done { existed: false })))?;
    }
}
// reads: remember which id asked for which key
let mut want = HashMap::new();
for i in 0..n {
    let id = c.send(&Request::Get { key: &i.to_le_bytes() });
    want.insert(id, i);
}
c.flush()?;
for _ in 0..n {
    let (id, got) = c.recv(|id, r| match r { Reply::Found { doc } => (id, doc.to_vec()), o => panic!("{o:?}") })?;
    assert_eq!(got, doc_n(want.remove(&id).unwrap()));
}

Ordering rules#

  • A filter sees the writes pipelined before it on the same connection: the reader first hands the shards every request it has already read, then asks them to scan.
  • A read on a connection sees the writes sent before it on that connection (test a_read_sees_the_writes_sent_before_it_on_its_connection).
  • Order between different connections is not defined.

Back-pressure#

The channel from a connection to a shard holds 256 batches. When it is full the reader stops reading that connection and TCP flow control slows the client. A client that pipelines far more than the buffers hold is not stalled: what does not fit in the socket buffer goes to a writer thread, and the reader thread never waits for a slow client (test a_client_that_pipelines_far_more_than_the_buffers_hold_is_not_stalled).

Group commit#

A write is answered after it is durable. Each shard has a flusher thread: it takes all parked replies, commits the highest log position with one fsync, then releases them. Writes that arrive during an fsync share the next one. Pipelining writes therefore costs far less than one fsync per write.

Measured with raftdb-bench server (400 k documents, loopback, 4 clients x 4 shards, windows of 512, Windows 11): durable inserts over TCP 1.21 M/s, point reads 5.56 M/s. These are pipelined throughput figures and say nothing about the latency of a single request.

Deferred writes (version 2)#

On a connection that said version 2, Defer{on: 1} makes the server answer the stream's writes when they are applied, not when durable; Sync then answers Done once all of those writes are durable (one wait, however many shards were touched). In the Rust client: set_defer(true), any number of writes, then sync(). set_defer returns false against a server without version 2. Until Sync is answered, an acknowledged write may still be lost in a crash. The lsm engine answers only durable writes, so sync has nothing to wait for there. See Connections, errors and versions.

Batching reads#

GetBatch exists in the codec but the server answers NOT_SUPPORTED; pipeline Gets instead. To read only some fields of many documents, use Project on a version 2 connection (filter_each_fields in the Rust client): each document is cut down to the named field ids on the server.

Batching over HTTP#

POST /v1/databases/{db}/tables/{t}/import takes up to 20,000 NDJSON lines (under 1 MiB) in one request and answers when they are durable. See HTTP: documents and import.

See also#

Wire protocol, HTTP API