Introduction

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) -> acknowledgement

1. Connection and login#

  1. The client opens a TCP connection to listen. The front-end sends the server handshake (version string 8.0.36-Raft DB for the MySQL dialect, 11.4.13-MariaDB-Raft DB for MariaDB) and a random nonce.
  2. The client answers with its user name and a scramble computed with caching_sha2_password or mysql_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 by CREATE 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.
  3. 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#

  1. The text arrives as COM_QUERY (or COM_STMT_EXECUTE for a prepared statement, whose parameters are written into the statement as literals at the place of each ?, never changing its structure).
  2. The parser turns the text into a tree. Valid MySQL that is not supported yet is refused with error 1235.
  3. 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.
  4. Views and common table expressions are expanded into derived tables.
  5. 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.
  6. 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.
  7. Joins, grouping, window functions, sorting and LIMIT run in the front-end over those rows.
  8. The result goes back as a text result set, or as binary rows for a prepared statement.

3. An INSERT#

  1. Parse and privilege check as above. Triggers BEFORE INSERT run on each row and may change it with SET NEW.col = ....
  2. 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; CHECK constraints and foreign keys are checked.
  3. 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.
  4. 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.
  5. The shard's flusher makes the log durable with one fsync that covers every write that arrived during the previous one. With sync-replicas greater than zero it also waits until that many replicas confirmed the position.
  6. The parked replies are released; the front-end sends the OK packet. Inside a transaction (START TRANSACTION or autocommit = 0), writes are answered when applied and the front-end waits for the disk once, at COMMIT.
  7. AFTER INSERT triggers 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.

See also#