Internals

Life of a write

What happens to an insert from the client's frame to the acknowledgement: shards, schema check, log, group commit, flusher, replicas and the lsm variant.

This page follows one Put or Insert on the binary protocol through the in-memory engine. The sources are raftdb-server, raftdb-db and raftdb-wal.

1. The frame is read and routed#

The connection's reader thread reads whatever bytes are available, which may be many pipelined requests, and splits them into frames (raftdb-proto). A Put carries [key][document]; PutJson carries JSON text that the server converts with its field dictionary, allocating ids for new field names (a new name is queued to every shard before any document that uses it).

The shard of the key is ((hash >> 16) & 0xFFFF) * shards >> 16, so a key always goes to the same shard thread. The reader copies each data frame into a per-shard batch and sends one batch per shard per read through a bounded channel (256 batches). If a shard falls behind, the channel fills, the reader stops, and TCP flow control slows the client.

2. The shard checks, logs and applies#

The shard thread owns the Table and is the only thread that changes its data. For each request it follows the order of raftdb-db:

  1. Check. The schema, if the table has one (raftdb-schema); for Insert that the key is absent. A refusal (SCHEMA, EXISTS) is answered at once and nothing is logged.
  2. Log. One record is appended to the log buffer (PUT, DELETE, DICT, SCHEMA or INDEX; layouts in raftdb-db). Framing adds 8 bytes: length and CRC-32C. The call returns the log position (Lsn).
  3. Apply. The in-memory collection (raftdb-store) is changed, and any secondary index is updated (Secondary indexes). A same-length document is overwritten in place; otherwise the new version is appended and the old record becomes garbage, which compaction later reclaims.

The success reply is not sent yet. It is parked together with its log position.

3. The flusher makes it durable (group commit)#

Each shard has a flusher thread. It takes all parked replies, asks the log to commit the highest position, and then releases them. In the log, the committing thread that wins a compare-and-swap becomes the leader: it swaps the buffer, writes it, flushes once, publishes the durable position and wakes the waiters. Every record appended before the swap is covered by that one flush. While a flush runs, new writes pile up and share the next one.

On Windows the log file is opened write-through, so the write is durable when it returns and the separate flush is skipped (D37).

If the write or the flush fails, the log is poisoned, every later write fails, and the parked requests get an IO error. Nothing is acknowledged that is not on disk.

4. Replicas (optional)#

  • A replica follows each shard's log byte for byte and applies it as it arrives. In the asynchronous default the primary acknowledges after its own flush.
  • With --sync-replicas N the flusher also waits until N replicas have sent an Ack{offset} at or beyond the commit position. If they do not within --sync-timeout-ms the write is acknowledged anyway and counted as a degraded commit.
  • A node managed by a watcher accepts writes only while its lease is valid; see Raft and the control plane.

5. The acknowledgement#

The reply (Done{existed}) goes back with the request id. Replies of one connection may arrive in any order, since writes wait for their shard's flush while reads do not.

A client that asks for deferred answers (protocol version 2, Defer) gets the reply when the write is applied and calls Sync to wait once for durability of all of them. The MySQL front-end uses this to wait for the disk once per COMMIT.

The lsm variant#

On an engine = lsm table the shard writes to the LSM engine instead: the record goes to the engine's own log segment and the memtable, and commit is the same group commit. If the server replicates, a replication log is also written and the engine's own log is not synced, because the replication log is then the durable record. Every row is written twice in that mode.

What bounds the speed#

Measured on the development PC: a blocking commit per 64 inserts reached 134 k/s because each caller waits for a whole flush; with a shard thread and a flusher thread the same table reached 3.43 M/s, and the server over loopback TCP reached 1.21 M/s of acknowledged durable inserts with 4 clients and 4 shards. The p99 acknowledgement latency target of 2 ms was not met on that PC (see raftdb-db).