Due to the introduction of TLS for MariaDB connections, MediaWiki connects to the database servers by using hostnames instead of IP addresses. This works fine, but connecting to MariaDB becomes much slower, because every connection (about four per request!) is responsible A and AAAA lookups through authoritative DNS.
Without any form of caching: 16.29% 58.451 4 - Wikimedia\Rdbms\DatabaseMysqli::mysqlConnect
When hardcoding IPv4 or IPv6 for 'db1[1-3].miraheze.org' in /etc/hosts: 7.67% 17.928 4 - Wikimedia\Rdbms\DatabaseMysqli::mysqlConnect
Presumably due to load on MediaWiki servers, four connections on a busier (e.g. flooding the server with requests) MediaWiki server take 700 ms(!), whereas the /etc/hosts hack reduced this to 200 ms.
Hardcoding the IP addresses of database servers in /etc/hosts is the easiest solution at first glance, but not the best example of reducing technical debt. Besides, applications like Matomo perform lots of DNS lookups as well, so a cluster-wide solution is the best one. https://phabricator.wikimedia.org/T171498 is a task at Wikimedia describing a different problem (overloaded DNS servers), but indicating the same solution: cache daemons on servers.