Iaroslav Kozlov Русский

Inside databases MySQL

MySQL

A relational database server. Its InnoDB engine keeps each table’s rows in a clustered index, a B-tree of pages; replicas repeat the changes the source writes to its binary log. Here it is Percona Server for MySQL, a drop-in replacement for MySQL, behind ProxySQL, with Orchestrator for failover.

My work

A Percona Server for MySQL cluster (8.0, then 8.4) behind ProxySQL with Orchestrator: automatic failover, reads split from writes, lagging replicas taken out of reads, asynchronous GTID-based replication. The grants and the ProxySQL rules for writes and reads are in Salt; MySQL and ProxySQL are installed through Terraform.

How it is built

A generalised layout from the documentation of MySQL 8.4, ProxySQL and Orchestrator, with one source and one replica as an example. The cluster ran on Percona Server for MySQL, 8.0 and then 8.4, a drop-in replacement for MySQL: what is told here is MySQL’s, with the defaults of 8.4. ProxySQL, Orchestrator, Terraform and Salt are the tools of my work beside it, not parts of it.

data only on failure My part

After a successful failover

the route after the failure unavailable takes over

  • Orchestrator cannot reach the source, and the replicas confirm that they cannot see it either: the topology is broken, and a failover is justified.
  • With automated recovery on, Orchestrator can promote the replica; with ApplyMySQLPromotionAfterMasterFailover (true by default) it resets the promoted server’s replication and sets read_only=0 on it.
  • With the reader and writer hostgroups in mysql_replication_hostgroups, ProxySQL’s monitor finds read_only=0 on that server and copies or moves it to the writer hostgroup, where this layout sends the writes.
  • Replication was asynchronous: a transaction the old source had committed and no replica had received yet is missing on the new source. 10 12141516

Components

Press a component to read what it does.

Client application

An application that sends SQL. Here it connects to ProxySQL, not to a server.

ProxySQL hostgroups

A proxy in front of the servers. It sends statements to hostgroups of servers by its query rules, and its monitor watches the servers’ read_only flag and the replicas’ lag. A tool, not part of MySQL.

Terraform, Salt as code

MySQL and ProxySQL on GCP are installed through Terraform; the MySQL grants and the ProxySQL rules for writes and reads are kept in Salt. Tools, not parts of MySQL.

Server runs the SQL

The MySQL server. It runs a statement and, before a COMMIT is executed, writes the transaction to the binary log. Under it the InnoDB engine keeps the data.

Buffer pool in memory

The area in main memory where InnoDB caches table and index data as it is accessed.

Clustered index B-tree of pages

The index that stores a table’s row data, typically the primary key: a B-tree whose records are in its leaf pages, 16KB each by default.

Binary log events

Events that describe changes to the database: on a source, the record of the changes to be sent to its replicas. It is also what brings data up to date after a backup has been restored.

Redo log crash recovery

A disk-based structure used during crash recovery: it encodes requests to change table data, and changes that had not reached the data files are replayed from it.

I/O thread receiver

The replica’s receiver thread: it requests the source’s binary log and writes the transactions it reads to the relay log.

Relay log on the replica

The replica’s log of the transactions read from the source’s binary log.

Replica applier threads

A server of its own. Its applier applies the transactions of the relay log to its data, repeating the changes just as they were made on the source: in 8.4, by default, a coordinator thread reads them from the relay log and schedules them to four worker threads.

Orchestrator failover

A tool that watches the replication topology: the source and its replicas. With automated recovery on, it can promote a replica once it has diagnosed a dead source. Percona Distribution for MySQL ships it as the replication topology manager for Percona Server; the original repository is archived and points to Percona’s fork. A tool, not part of MySQL.

  1. Routing

    An application connects to ProxySQL, not to a server. ProxySQL sends statements to hostgroups of servers by its query rules; in this layout the writes go to the source.

    My part MySQL and ProxySQL on GCP are installed through Terraform. The MySQL grants and the ProxySQL rules for writes and reads are kept in Salt.

    In detail

    ProxySQL’s guide to a read/write split assumes a primary server in hostgroup 1 and the replicas in hostgroup 2. A query rule names the hostgroup that a statement matching it goes to (destination_hostgroup); a user has a default hostgroup of its own. Rules are put into effect with LOAD MYSQL QUERY RULES TO RUNTIME and kept with SAVE MYSQL QUERY RULES TO DISK. 1

    Where it breaks Generic rules that send every SELECT to the replicas cause problems with transactions, session state and consistency: the ProxySQL guide marks them not for production and asks for targeted rules.

  2. Finding the row

    InnoDB keeps a table’s rows in its clustered index, normally the primary key. A search through it leads directly to the page that holds the row, cached in the buffer pool.

    In detail

    Each InnoDB table has a special index, the clustered index, that stores row data; typically it is the primary key. Except for spatial indexes, InnoDB indexes are B-trees, and index records are stored in their leaf pages, 16KB each by default. The buffer pool is the area of main memory where InnoDB caches table and index data as it is accessed. 2 34

    Where it breaks Each record of a secondary index holds the primary key columns, by which InnoDB finds the row in the clustered index. So a long primary key makes every secondary index larger.

  3. The redo log

    A change is encoded in the redo log. If the server stops before the change has reached the data files, it is replayed from that log at the next start, before connections are accepted.

    In detail

    The redo log is a disk-based data structure used during crash recovery to correct data written by incomplete transactions. During normal operations it encodes requests to change table data that result from SQL statements or low-level API calls. Modifications that did not finish updating data files before an unexpected shutdown are replayed automatically during initialisation, before connections are accepted. 5

    Where it breaks The replay comes before connections are accepted: after a crash the server is not available until it is done.

  4. Commit

    A transaction’s changes to InnoDB tables go to the binary log before its COMMIT is executed. The source then answers the client without waiting for any replica: replication is asynchronous.

    My part Replication on GTID, asynchronous.

    In detail

    Within an uncommitted transaction, all updates that change transactional tables such as InnoDB tables are cached until a COMMIT statement is received by the server; at that point the server writes the entire transaction to the binary log before the COMMIT is executed. Replication is asynchronous by default: the source writes events to its binary log and replicas request them when they are ready; the source does not know whether or when a replica has retrieved and processed the transactions. The binary log is not the redo log: it is the record of changes for the replicas, and the redo log is InnoDB’s own, for its crash recovery. 5 6712

    Where it breaks If the source fails, a transaction it has already committed may not have reached any replica: a failover then goes to a server that is missing it.

  5. Replication

    Each replica requests a copy of the source’s binary log: it pulls, the source does not push. Its receiver thread writes the relay log, and its applier threads apply it.

    In detail

    Each replica that connects to the source requests a copy of the binary log; it pulls the data from the source rather than the source pushing it. The replica’s relay log, written by the replication I/O (receiver) thread, contains the transactions read from the source’s binary log; the applier applies them on the replica, repeating the original changes just as they were made on the source. In 8.4 replicas are multithreaded by default (replica_parallel_workers is 4): a coordinator thread reads the transactions from the relay log and schedules them to four worker threads. With GTID auto-positioning, a replica tells the source which transactions it already has, and receives only those it has not. 7 89101113

    Where it breaks Replication is asynchronous by default, so a replica can be behind its source. ProxySQL’s monitor checks the lag of every server whose max_replication_lag is above 0, and shuns a server while its lag is over that limit.

  6. Reads from a replica

    Reads can go to the replicas. ProxySQL’s monitor checks read_only on the servers: one with read_only=1 is put in the reader hostgroup, one with read_only=0 in the writer hostgroup.

    My part Reads are split from writes, and a replica that lags too far is taken out of reads.

    In detail

    The monitor checks read_only for all hosts in the hostgroups listed in mysql_replication_hostgroups and logs each check in mysql_server_read_only_log. With read_only=1 a host is copied or moved to the reader hostgroup; with read_only=0, to the writer hostgroup. 7 10

    Where it breaks A read sent to a replica can miss a change the source has already committed: by default the source does not wait for its replicas.

  7. Failover

    With automated recovery on, Orchestrator can promote a replica once the replicas confirm they cannot see the source either, and set read_only=0 on it; ProxySQL’s monitor then puts it in the writer hostgroup.

    My part Automatic failover by Orchestrator.

    In detail

    Orchestrator observes not only the server itself but also its replicas: to diagnose a dead source it must fail to contact it and confirm with its replicas that they cannot see it either. Automated recovery is opt in. It attempts to promote a replica that will retain the most serving capacity, not always the most up-to-date one, and with ApplyMySQLPromotionAfterMasterFailover (true by default) runs reset slave all and set read_only=0 on the promoted server. To fail over at all, the topology must support GTID, Pseudo-GTID or binlog servers. ProxySQL’s monitor checks read_only on the hosts of the hostgroups listed in mysql_replication_hostgroups, and copies or moves a host with read_only=0 to the writer hostgroup. 10 14151618

    Where it breaks If all replicas were lagging when the source failed, even the promoted one may still have unapplied relay logs, which resetting its replication would lose. With DelayMasterPromotionIfSQLThreadNotUpToDate set to true, Orchestrator waits for the SQL thread to catch up before it promotes the new source.

The whole diagram

MySQL

Every component and step of the figure, drawn at once; the page lists them in text.

data only on failure My part

Next engine

PostgreSQL

A database server that logs changes in its write-ahead log before its data files change, and keeps old row versions until VACUUM removes them. Here with Patroni, etcd and HAProxy for failover.

Inside PostgreSQL Inside databases