From 6.0.15, a site's pages can read from a replica close to the web server while every write still goes to the primary. The same rule applies to PostgreSQL, MySQL and MariaDB, and Oracle through the ezoracle extension. SQLite has no replica. Nothing changes until you set UseSlaveServer=enabled; the shipped value is disabled.
When a statement reads from the replica
Only when all of these hold. Anything else runs on the primary.
- It is a plain SELECT that writes nothing and takes no lock.
- No transaction is open.
- The request has written nothing yet. The first write pins the rest of the request to the primary, so a request always reads what it wrote.
- It is a web request with the method GET or HEAD. Cronjobs, scripts, console commands and every POST use the primary.
- The visitor is not signed in, so editors always see their own work at once.
- The replica is current. If it does not answer, or is behind, the whole request reads the primary.
Any error on the replica runs the same statement on the primary, without an error for the caller, and turns the replica off for the rest of the request. The connection opens on first use, with a 2-second timeout. A node where nothing listens on the replica's address simply reads the primary, so one set of settings can serve every node of a cluster.
Settings
[DatabaseSettings] UseSlaveServer=enabled SlaveServerArray[] SlaveServerArray[]=127.0.0.1 SlaveServerPort[] SlaveServerPort[]=5413 SlaverServerUser[] SlaverServerUser[]=<site login> SlaverServerPassword[] SlaverServerPassword[]=<its password> SlaverServerDatabase[] SlaverServerDatabase[]=<site database>
The keys really are spelled Slaver.... With several SlaveServerArray[] entries, each request picks one at random. Then clear the INI cache and deploy:
php bin/php/ezcache.php --clear-tag=ini ./console exp:velocity deploy
How current the replica must be
- PostgreSQL: the first statement asks the replica whether it is in recovery and whether its stream is up. The site's database login needs pg_read_all_stats to see that; without it the replica is never used.
- MySQL, MariaDB and Oracle: SlaveServerStatusCheck (enabled), SlaveServerMaxLag (0 seconds), SlaveServerCatchUp (enabled) and, on MariaDB, SlaveServerCatchUpWait (100 ms).
Note: MySQL and MariaDB sites that already used UseSlaveServer=enabled read the replica less often from 6.0.15 on, and never stale. The DFS cluster backend keeps its own connection to the primary.