Internals

MVCC and transactions

What the raftdb-mvcc crate implements (version header, snapshot visibility, purge horizon), what is not connected to the server, and how transactions work in the MySQL front-end.

What exists: raftdb-mvcc#

raftdb-mvcc (standard library only, no unsafe) implements the building blocks of multi-version concurrency control in the InnoDB style (decision D15):

  • Header, 12 bytes in front of every row version: txn (48 bits, the transaction that wrote this version), undo (47 bits, where the previous version is in the undo log, 0 = none) and a delete mark (the top bit of the second 6-byte field). It is stored as 6 bytes little-endian txn, then 6 bytes little-endian undo | delete bit. Every 12-byte pattern decodes, so there are no invalid states to check. For comparison the crate document gives 13 bytes for InnoDB and 23 for PostgreSQL.
  • ReadView, a snapshot: up_limit (everything below has finished), low_limit (everything at or above started later), creator (sees its own writes) and the sorted list of transactions running in between. visible(txn) is true if txn < up_limit or txn == creator, false if txn >= low_limit, and otherwise true unless txn is in the running list (binary search).
  • purge_horizon(views, next): the smallest up_limit of all open snapshots, or next when there are none. Undo for versions replaced below the horizon cannot be reached and can be freed.

Limits: MAX_TXN is 2^48 - 1 and MAX_UNDO is 2^47 - 1; encode asserts them in debug builds.

Measured: 495 M visibility checks per second typically and 183 M per second in the worst case.

The PostgreSQL way (a full new row per version, 23 + 4 bytes each, needing VACUUM) was rejected in D15.

What is not connected#

In the workspace, raftdb-mvcc is used by the benchmark harness only. The server and the engines do not yet carry row versions, an undo log, write-write conflict rules, isolation levels or a commit record. Consequences stated elsewhere in the crate documents:

  • Pages of a filter are consistent only while the data does not change; a scan delays that shard's point requests while it runs.
  • The log of raftdb-db has no transactions across keys.
  • The LSM engine has no snapshots across keys.

Transactions in the MySQL front-end#

The front-end (D57) provides START TRANSACTION, COMMIT, ROLLBACK, savepoints and autocommit, without row versions in the core:

  • Every write of a session already leaves the undo records its statement keeps so that a failing statement is all or nothing. Inside a transaction they are also kept in the session's log. ROLLBACK replays the log backwards, ROLLBACK TO replays down to the savepoint, COMMIT forgets it, and a connection that ends with a transaction open rolls it back.
  • Isolation is by table claims, not by versions. A session that writes a table inside a transaction holds it until the end and other sessions wait (txnlock.rs); @@transaction_isolation reports READ-COMMITTED. There are no snapshot reads. FOR UPDATE, LOCK IN SHARE MODE and LOCK TABLES are accepted and lock nothing. If the front-end process dies in the middle of a transaction, what had been written stays.
  • In one-process mode the writes of a transaction are answered when applied and the front-end waits for the disk once, at COMMIT (Defer and Sync, see Life of a write).

D57 states that real isolation needs row versions carried through the shards and the log, a commit record and a read view per transaction, and calls it a separate project (level 4b) that is not built.

Not supported yet#

Row versions in the engines, an undo log, isolation levels above read-uncommitted in the MySQL front-end, deadlock and conflict detection, snapshot age limits.