How a statement flows through Raft DB
The path of an SQL statement and of a write from the client to the write-ahead log and back, step by step.
This page follows two statements: a SELECT and an INSERT, sent by a MySQL client to a one-process installation (raftdb-mysql with embed).
mysql client
| MySQL protocol (TCP, port of `listen`)
v
raftdb-mysql: handshake and login -> session -> parser -> privilege check -> executor
| in one process: raftdb_server::Local (no socket) in two processes: raftdb-client over the binary protocol
v
raftdb-server: shard threads -> table (memory or lsm) -> WAL / lsm log -> flusher (fsync) -> acknowledgement1. Connection and login#
- The client opens a TCP connection to
listen. The front-end sends the server handshake (version string8.0.36-Raft DBfor the MySQL dialect,11.4.13-MariaDB-Raft DBfor MariaDB) and a random nonce. - The client answers with its user name and a scramble computed with
caching_sha2_passwordormysql_native_password. The front-end checks it against the verifier it derived from the password: the users named in its configuration, or an account stored byCREATE USER. Without a configured certificate there is no TLS exchange; with one, a client that asks for TLS sends a short SSL request first and the rest of the login travels encrypted. A client has 10 seconds to log in. - The session is created with the account's privileges. When
max_connections(default 500) are open, a new client gets error 1040.
2. A SELECT#
- The text arrives as
COM_QUERY(orCOM_STMT_EXECUTEfor a prepared statement, whose parameters are written into the statement as literals at the place of each?, never changing its structure). - The parser turns the text into a tree. Valid MySQL that is not supported yet is refused with error 1235.
- The privilege check works out, from the tree, what the statement needs (which privilege, on which database, table and columns) and compares it with the account's grants.
- Views and common table expressions are expanded into derived tables.
- The executor plans the statement. Conditions that Raft DB compares exactly as MySQL does (and index-usable ranges on primary keys and indexed columns) are compiled into a Raft DB filter plan and handed to the server; everything else is evaluated in the front-end with MySQL's rules (case-insensitive text, exact decimals, three-valued logic). Only the columns the statement names are read.
- In one process the filter runs on the session's thread under the table's read lock and each row is decoded where it is stored. In two processes the filter and the field list go over the binary protocol and rows come back in pages.
- Joins, grouping, window functions, sorting and
LIMITrun in the front-end over those rows. - The result goes back as a text result set, or as binary rows for a prepared statement.
3. An INSERT#
- Parse and privilege check as above. Triggers
BEFORE INSERTrun on each row and may change it withSET NEW.col = .... - Every row is converted to the column's type with MySQL's strict rules (error 1366, 1264, 1406, 1048, 1364 and the others when a value does not fit). Generated columns are filled;
CHECKconstraints and foreign keys are checked. - Rows are written. The primary key's uniqueness is the engine's atomic insert if absent. Each unique index claims its value in a hidden claim table before the row is written. If a later step fails, the statement takes back what it did (newest first): a statement is all or nothing.
- The server routes each write to the shard of the key. The shard applies it and appends a record to its log; the reply is parked.
- The shard's flusher makes the log durable with one
fsyncthat covers every write that arrived during the previous one. Withsync-replicasgreater than zero it also waits until that many replicas confirmed the position. - The parked replies are released; the front-end sends the OK packet. Inside a transaction (
START TRANSACTIONorautocommit = 0), writes are answered when applied and the front-end waits for the disk once, atCOMMIT. AFTER INSERTtriggers run when the statement has written all its rows.
4. What is cached#
Table definitions, field ids, foreign keys, views, routines and triggers are cached by the front-end for a short time (2 seconds for definitions of views and similar) and are read again after a change made through the same front-end. Several front-ends in front of one server see each other's definition changes after that interval; account changes are seen at the next login.