raftdb-server, the server
The threads of the server (connection, shard, flusher), databases and tables on disk, replication, roles, control-plane hooks and the engine choices, with measured throughput.
raftdb-server is the server program and library. It depends on raftdb-db, raftdb-proto, raftdb-store, raftdb-wal and raftdb-lsm, uses safe Rust and standard sockets (the exceptions are listed in Portability). Source files include lib.rs, admin.rs (HTTP), auth.rs, fence.rs, limits.rs, local.rs, lsmshard.rs, pool.rs, replica.rs, replication.rs, settings.rs, udp.rs and upstream.rs.
Threads#
client --TCP--> connection reader --one batch of frames per shard per read--> shard thread (owns a Table)
^ | reads, refusals: reply at once
connection writer <------------------- reply bytes <-------------------+ writes: parked until durable
v
flusher thread: one flush, then release the parked replies- Connection (two threads): the reader reads what is available (many pipelined requests per
read), splits frames, answersHelloandPing, and copies each data request into a per-shard batch. One batch per shard per read goes through a bounded channel of 256 batches; a full channel stops the reader and TCP flow control does the rest. The writer sends the reply buffers it receives. - Shard (one thread each,
Config.shards): the shard of a key is((hash >> 16) & 0xFFFF) * shards >> 16(decision D12). It ownsshard-N.logas aTableand is the only thread that changes its data.Getanswers at once;Put,InsertandDeleteare applied and logged and the success reply is parked with its log position. Refusals (EXISTS,SCHEMA,IO) answer at once. - Flusher (one thread per shard, D21): takes all parked replies, commits the highest log position with one flush, then releases them. Writes that arrive during a flush share the next one (group commit). If the flush fails the parked requests get
IOerrors.
Replies of one connection may arrive in any order; the request id identifies them. See Life of a write and Life of a read.
Databases and tables on disk#
dir/<database>/<table>/shard-N.log: a database is a directory, a table is a directory of shard logs with its own field-name registry, indexes and shard threads. All tables use the same number of shards. The server starts with main.documents. Names are 1 to 64 characters of letters, digits, _ and -. Each table owns its shard and flusher threads, so drop and rename stop those threads first (safe on Windows, which cannot delete open files); the body of a drop must repeat the exact name.
One registry of field names#
Documents store ids, so every shard must agree on them. The server keeps one dictionary; a new name gets the next id and is queued to every shard in the same order while a lock is held, and each shard queue is first-in first-out, so a document that uses a new name always arrives after the name. At start the registry is rebuilt from the longest shard dictionary and a conflict refuses to start.
Sorted queries and paging#
ORDER BY runs in two steps. Each shard selects every match, finds its first start + page rows with a partial sort and returns them with the number of matches; the connection merges the lists and cuts the page. Ties are ordered by primary key. The cursor of a sorted query is the number of rows already returned, so any row can be jumped to. Cost: a sorted query reads every match unless an index narrows it; measured 40 ms for the top 100 of 1 M rows and about 0.9 s for OFFSET 500000. For unsorted filters the cursor is shard << 48 | matches already returned from that shard, and pages are consistent only while the data does not change (there is no snapshot isolation; see MVCC).
Replication#
- Asynchronous (D38):
--replica-ofstarts a read-only replica that follows every shard's log byte for byte (Follow{db, table, shard, offset, tail_crc}thenLog{offset, bytes}, with an emptyLogevery 5 s as a heartbeat). A failover can lose the newest acknowledged writes.POST /v1/promotemakes a replica writable. - Synchronous (D39):
--sync-replicas Nmakes a write wait until N replicas have confirmed it durable; after--sync-timeout-ms(default 1000) the write is acknowledged anyway and counted insync.degradedCommits. Measured, one replica, loopback, 400 sequential writes: p50 143 to 304 us, p99 962 to 1,401 us. - A checkpoint on the primary makes every replica of that table fetch it again.
Managed nodes and table groups#
control = true makes the server a node a watcher manages (D51): read-only at start, no election inside it. A control account commands the node with POST /v1/control/role (epoch, role, lease). The node is writable only while it is primary and its lease has not run out; epochs only grow, and the epoch and the dirty flag are stored in control.state. The election that once lived in the server (D42) was removed in D51 step C4. See Raft and the control plane.
With groups = N the tables are split into N groups by group_of(db, table, N), each with its own primary under the watcher; requests for a group led elsewhere are forwarded (upstream.rs). Partitioned tables (T~p0, T~p1, ...) place partition k in group (hash(db, T) + k) mod groups.
Engines#
The default engine keeps each shard's table in memory with a log (raftdb-db). engine = lsm creates new tables on the LSM engine; existing tables keep their engine. LSM tables support the same requests, indexes, sorted SQL, replication and checkpoint; a schema through on_open is refused. When the server replicates, an LSM shard keeps a replication log repl-N.log in the same format as shard-N.log, so LSM and in-memory servers can follow each other.
Performance helpers (D66 to D69)#
- Reads on the connection's thread: the table of an in-memory shard is
Arc<RwLock<Option<Table>>>; the shard thread takes the write lock for one message and a connection's reader answersGetand index-backed filters under the read lock, but only when all its earlier batches are finished, so a read sees the writes sent before it on its connection. Server::local(credential)returnsLocal, the in-process client used by the MySQL front-end in one-process mode.pool.rsis the program's allocator: the system allocator with a per-thread cache of freed blocks up to 64 KiB. Measured on Windows through the in-process client, a key read went from 0.83 to 0.15 us.spin-us(default 150) is the bounded look of raftdb-client on the connection reader.inline_pagewrites a first page of binary documents under the shards' read locks when an index narrows the filter and at most 4096 candidates match.- Streams: a protocol-2 connection keeps a place per stream; requests that need a shard's thread go to helper threads (at most 32 per connection) so no stream holds up another. Limit: 65,536 streams per connection.
Settings#
raftdb-server with no arguments listens on loopback 127.0.0.1:7480, keeps data in /var/lib/raftdb or %ProgramData%\RaftDB (else ./raftdb-data) and, with no token or user, creates an admin account with a random password in <dir>/admin.password. One name per setting works as a flag, an environment variable (RAFTDB_SYNC_REPLICAS) and a file key, in the order command line, environment, file, profile. Profiles: single, primary, replica. Accounts keep only salted PBKDF2-HMAC-SHA-256 hashes (100,000 iterations); roles are readonly, readwrite and admin, optionally limited to one database. A successful login is remembered for 10 minutes as a keyed digest so reconnecting clients skip the hash. For use of these settings see the administration and security chapters.
Measured#
raftdb-bench server, 400 k documents, loopback, 4 clients x 4 shards, Windows 11:
| Metric | Result | Target |
|---|---|---|
| Durable inserts over TCP (acknowledged after the flush) | 1.21 M/s | at least 300 k/s |
| Point reads over TCP | 5.56 M/s | at least 2 M/s |
| Filter count scan, 1 M documents, 4 shards x 3 scan threads | 57.2 M docs/s | at least 50 M/s |
| Page of 100 matches as JSON | 2,178 pages/s | n/a |
These are pipelined throughput figures. They say nothing about the latency of a single request.
Limits#
Two threads per connection, so it cannot hold a million idle connections; no TLS; no GetBatch or Cancel over the server's request loop (clients pipeline Get); a Server dropped without shutdown() keeps its threads running.