ubuntips

The MariaDB query cache earned its place back, and 14,000 lines of migration code left

Draft posted on · 8 min read

On this page
Draft commented on Nov 30, 2026

My setup script spent the last two weeks carrying a one-time migration: the code that moved a 1.21 server from Angie to a compiled nginx, from Sury’s PHP packages to a source build, and from a packaged Redis or Valkey to the compiled Valkey (how that went). Every server I run has now been through it. This cycle removed it, and used the same test runs to look for new speed in nginx, PHP, Valkey, MariaDB and Elasticsearch.

Deleting code that live servers might still need#

Migration code is easy to write and hard to delete: the day you delete it, any server that did not finish the migration is stranded with a script that no longer understands it. So the removal came in two parts.

A guard first. Before any mode runs, setup.sh now checks for a 1.21 state: an unfinished migration record, the temporary policy-rc.d the migration installs, a 1.21 build trigger, Angie still serving, or a packaged PHP-FPM, Valkey or Redis still serving on a host that shows signs of 1.21 (Sury packages, an Angie directory) and never started the migration. Any of these stops the run with one message: check out v1.22.35, the last release that carries the migration, and run --optimize with it. --purge and --diagnostics still run on such a host.

The first draft of that guard was wrong in two instructive ways, and an independent review caught both. It refused hosts that had finished the migration on purpose with a package still serving (the migration leaves a small host on its packaged Redis below the 4 GB class). And its warning about packages left installed but stopped suggested apt-get purge, which on these hosts deletes configuration and data directories the source builds still read. A “helpful” cleanup hint in a refusal message is still a command someone will copy. A final review before the release caught a third: every setup.sh run creates its state directory, so the first draft would also have refused, from its second run on, a host that was never on 1.21 but happened to run Ubuntu’s own php-fpm. The guard now asks for evidence of 1.21, not just for a package.

Then a read-only look at every server. Part of the old code did not belong to the migration itself but to leftovers it might have left behind: an Anubis bot-filter unit, Angie’s ACME client stanza, a Brotli/zstd load_module line, a PHP update-alternatives group, a Sury diversion of /usr/bin/php, an old allocator recorded in a PHP build. Each of those clean-ups can only go once no server has the leftover. So instead of guessing, a script ran the new guard’s own function plus one check per leftover on all 22 servers, as root but writing nothing.

Every server passed the guard. Eight of the ten leftover checks were clean everywhere, so that code went. Two were not: seven migrated servers never recorded that a PHP extension clean-up had finished, and two still carry a list of extensions to drop. Those two helpers stay for another release. The check also turned up one server that had only ever had a 1.20 --basic run and one that setup.sh never managed at all, both worth knowing about before pointing a new release at them.

In total about 14,200 lines left the repository and 2,300 came in (mostly the guard, its tests and the query-cache work below). The proof that nothing else changed is a parity test on the test box: a fresh install of the new code against a fresh install of 1.22.35, and --optimize with the new code on a 1.22.35 server. Both compared /etc, the systemd units, nginx -T, php -i, the PHP-FPM pools, every Valkey setting, every MariaDB variable, sysctl and the crontab. The only differences were the query cache, a timestamp Elasticsearch’s package writes into its own configuration, and counters.

Nine ideas, one winner#

The test box is a Xeon E-2236 (6 cores, 12 threads, 62 GB, NVMe RAID0) on Ubuntu 26.04 with a XenForo 2.3.13 forum seeded with 20,000 members, 10,000 threads and 100,000 posts: PHP 8.5 from source, MariaDB 11.8, sessions and cache in Valkey, search on Elasticsearch. Every idea ran as an interleaved A/B: the two arms alternate inside each round, six rounds, CPU clock pinned at 3.6 GHz so turbo cannot reward whichever arm runs after an idle gap. The bar for a new default is the same as always: at least 2 % faster on some page type and slower on none.

IdeaUncached and member pagesVerdict
MariaDB query cache, 64 MB+9.6 to +11.2 %new default
…with a writer posting 2 replies a second+9.2 to +11.9 %holds
…with 48 PHP workers, 200 connections and the writer+7.5 to +10.3 %holds
InnoDB adaptive hash index on+0.3 to +0.6 %under the bar
jemalloc for MariaDB (on top of the query cache)0.0 to +0.3 %nothing
multi-size transparent huge pages (always)+0.5 to +0.7 %under the bar
PHP-FPM workers on SCHED_BATCH-0.3 to +0.7 %noise
PHP garbage collector off (zend.enable_gc=0)-0.3 to -0.9 %slower
MariaDB one-thread-per-connection instead of the thread pool-2.5 to -5.2 %slower

Why the query cache wins here#

The query cache has a bad reputation, and mostly an earned one: one global lock, and every write to a table throws away every cached result that read it. MySQL removed it in 8.0. MariaDB kept it, and the setup script had it switched off since long before 1.22.

On this forum it comes out well ahead, and the reason is visible in the cache itself. XenForo asks the same small set of questions on every page: options, style and phrase lookups, permission caches, node lists, the online-user count. About 35 distinct queries end up cached, in less than 1 MB, and they are hit about 245,000 times per benchmark round. Those queries read tables that change rarely. Posting a reply writes to the post, thread and user tables, which drops the handful of cached results that read them, and leaves the rest alone.

That is why the gain survived the two tests designed to break it. A background writer posting two replies a second (1,859 posts during the run) kept it at 9-12 %. Quadrupling the PHP workers to 48 with 200 concurrent connections, to put pressure on the global lock, still left 7.5-10 %.

The cache is sized from RAM, not set to a fixed 64 MB: RAM/1024, rounded down to 8 MB steps and kept between 16 and 64 MB (56 MB on this box). A bigger cache only makes invalidation slower. --optimize switches it on with SET GLOBAL, so no MariaDB restart is needed. SETUP_MARIADB_QUERY_CACHE=0 keeps it off for a workload where it does not fit, such as a write-heavy site that keeps a few hot tables changing.

Before and after#

The release against 1.22.35, same box, same forum data, median of five rounds:

Page1.22.351.22.36p99 latency
guest home, cached by nginx116,271 req/s115,673 (noise)1.02 -> 1.00 ms
guest home, uncached368407 (+10.6 %)344 -> 326 ms
guest thread, uncached385419 (+8.8 %)352 -> 298 ms
member home329364 (+10.6 %)410 -> 380 ms
member thread298325 (+9.1 %)471 -> 400 ms

The cached guest page never reaches PHP or MariaDB, so it does not move. Everything PHP renders got about a tenth faster, with the slowest requests improving most.

Why the losers lost#

  • One thread per connection gave back everything the thread pool already won: with PHP holding a persistent connection per worker, the pool keeps the number of threads running inside InnoDB near the core count.
  • The adaptive hash index helps repeated lookups into hot B-tree pages, but the buffer pool already holds this whole forum and the query cache now answers the most repeated reads, which most likely left it little to do.
  • Turning PHP’s garbage collector off was slightly slower, not faster. My reading: XenForo builds few reference cycles per request, so the collector rarely runs anyway, and switching it off saves nothing measurable.
  • SCHED_BATCH and multi-size huge pages measured inside the noise. Both are cheap to apply, which is exactly why they need the 2 % bar: a default that changes nothing still costs a line of code and a line in every report.

What this does not prove#

All of it ran on one Intel box; the AMD test machine is gone. Most of my database servers run MariaDB 10.11 (Ubuntu 24.04), and the gain was measured on 11.8. The query cache code is old and stable in both, so I expect the gain to carry over, but I have not measured it there.

What I could check is the switch itself. Update: the first live server to get the release runs MariaDB 10.11.14, started with the cache off. --optimize switched it on with SET GLOBAL (OFF to 56 MB) in a 21-second run, with the same MariaDB process before and after and no service restarted; a probe of the site saw only 200s, and a minute later the cache had served its first 215 hits. Had a server refused the change, the value would simply wait for its next restart: --optimize never forces one for it, retries the live switch on every run and says in its report when the cache is configured but still off on the running server.

Back to top ↑