Architecture
The programs and crates of Raft DB, the threads inside the server, how the SQL front-end sits on the engine, and where the control plane lives.
Raft DB is made of small crates. Engine crates have no third-party dependencies. The programs are four binaries.
The programs#
| Program | Crate | Role |
|---|---|---|
raftdb-server | raftdb-server | The database server: binary protocol and HTTP on one port, shards, WAL, replication, the Studio's static files. |
raftdb-mysql | raftdb-mysql | The SQL front-end. Listens on a MySQL port, answers as MySQL 8 or MariaDB 11 (dialect), runs the SQL on Raft DB. Can run the server inside its own process (embed). |
raftdb-watch | raftdb-watch | The watcher: health checks, restarts, reports, and the control plane (failover quorum, resource control). |
raftdb-bench | raftdb-bench | The benchmark harness with targets. A developer tool. |
The crates#
| Layer | Crates |
|---|---|
| Data structures | raftdb-table (slot hash table), raftdb-doc (document format), raftdb-bloom (bloom filter), raftdb-spsc (cross-core ring), raftdb-index (secondary index), raftdb-mvcc (row-version header and visibility) |
| Query | raftdb-exec (plan IR, JSON filter compiler, batch executor, SQL subset parser) |
| Storage | raftdb-wal (write-ahead log with group commit), raftdb-store (key to document collection), raftdb-schema (strict schema), raftdb-db (durable table: schema, log, memory, indexes), raftdb-lsm (disk-resident engine) |
| Network | raftdb-proto (wire codec), raftdb-client (blocking client), raftdb-server |
| Cluster | raftdb-raft (Raft state machine, used by the watcher's quorum), raftdb-watch, raftdb-sim (deterministic crash simulation) |
| Front-end | raftdb-mysql |
| Support | raftdb-core (numeric targets), raftdb-report (fatal-error reports), raftdb-bench |
Inside the server#
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 fsync, then release the parked replies- A connection has a reader and a writer thread (a poller with
epoll,kqueueorIOCPis future work; this limits the number of idle connections). - A shard is one thread that owns one part of a table: its log (
shard-N.log) and its in-memory rows. The shard of a key is computed from the key's hash; every table of a server has the same number of shards (shards, default from the number of cores, at most 4). - A flusher thread per shard makes the log durable with one
fsyncfor everything that arrived meanwhile (group commit) and then releases the parked replies. A write is acknowledged only after that. - On the in-memory engine a connection's reader thread answers key reads and index-backed filters itself under the table's read lock, without a hop to the shard thread (D66).
The SQL front-end#
raftdb-mysql is a separate program so that a bug in SQL parsing cannot corrupt the engine, and so that other dialects can be added beside it (D52). It contains the MySQL protocol (handshake, authentication plugins, text and binary protocols), the lexer and parser, the expression evaluator with MySQL's rules, the executor for joins, grouping, window functions and the rest, the catalog (information_schema, SHOW), accounts and privileges, views, routines and triggers.
A MySQL table t in database d is the Raft DB table t in the Raft DB database d. Its definition (columns, types, keys, defaults, checks) is one document in a hidden table of the database; the table's rows are documents with one field per column. Unique keys other than the primary key are kept with hidden "claim" tables that use the engine's atomic insert-if-absent. Names that start with __rdb_, __tmp or __u_ are the front-end's own tables and are never shown.
The front-end can reach the server in two ways:
| Mode | Setting | What happens |
|---|---|---|
| One process (the documented default, D67) | embed = /path/raftdb.conf | The server of that settings file runs inside the front-end's process; sessions call it as a library, so a row read costs no network round trip. |
| Two processes | raftdb = host:port | The front-end connects to a running raftdb-server over the binary protocol (protocol version 2 with streams that sessions can share). Use it to put the front-end on another machine than the data. |
The control plane#
Elections, failover, handover and resource limits are the watcher's job, not the server's (D51). The server keeps only what it must decide alone in bounded time: an epoch and a lease (a fence), so that a node that cannot reach anybody stops accepting writes by itself. See The fence and Failover with a watcher quorum.