Table of contents of the article:
It is well known that the study of technology at least on our part has the sole purpose of obtaining practical and tangible solutions to improve the performance, stability and speed of the websites of our valued customers.
As exciting as it may be, to test, try, experiment with new technologies, web servers, configurations, in the end the ultimate focus is always on doing better.
In a previous article we compared the speed, throughput and the advantages that can be obtained by using Unix Domain Sockets rather than TCP sockets to establish a connection with a MySQL database.
Because basically having a local database and connecting via TCP as if it were a remote database necessarily involves all the operations at the TCP level to establish a connection, first of all the Three Way handshake TCP.
This process necessarily involves additional operations at the network level, and greater latency than opening a UNIX file descriptor, effectively making the connection of our WordPress CMS (but the speech is valid for any other CMS) slower and less performance compared to the use of UNIX DOMAIN SOCKET.
The benefits we have already widely seen in the previous article which dealt with the difference in performance between connections MySQL via socket rather than MySQL via TCP, but obviously a technical article can be useless if not adequate for our purpose which is to make our WordPress or WooCommerce installation more performing.
Do Unix Domain Sockets improve WordPress performance?
On some WordPress systems a simple change to your configuration file can speed up communication between WordPress and your database. If your WordPress installation is on Linux, then yours DB_HOST has as a setting localhost or 127.0.0.1 it may be possible to use Unix Domain Sockets to speed up your system and communication with the MySQL database.
This tutorial will show you how to do this, explain the reasoning behind it, and test the results.
The WordPress codex mentions this technique in the documentation on editing the file wp-config.php, but it doesn't go into much detail.
If your web server has a typical configuration on Linux, the procedure could be as simple as opening the file/wp-config.phpfile in an editor, then looking for a line like this:
define( 'DB_HOST', 'localhost' );
define( 'DB_HOST', '127.0.0.1' );
and replacing it with this:
define( 'DB_HOST', 'localhost:/var/lib/mysql/mysql.sock' );
you will have instructed your WordPress installation to use the MySQL socket file descriptor rather than the TCP connection.
Troubleshooting known issues
If the setting shown above doesn't work and you have shell access to your web host, you can verify the Unix domain socket name using the lsof command which means list open files.
lsof -U | grep mysql mysqld 737 mysql 37u unix 0xffffa0a4b9628400 0t0 15179 /var/run/mysqld/mysqld.sock type=STREAM
This command will work for both MySQL and MariaDB databases which our very good and favorite Tap Server. If you don't see output similar to the one shown above, then a Unix domain socket is not available to you. If you get a list, but the socket path is different from/var/run/mysqld/mysqld.sock, copy the correct socket path and paste it into yours wp-config.php after localhost:in your definition DB_HOST
For example in derived RedHat Linux systems, such as RHEL itself, CentOS, Fedora, Almalinux, Rockylinux, Scientific Linux, the path is normally /var/lib/mysql/mysql.sock
Let's take apart some urban legends
There is some bad information circulating on the internet regarding this subject. The following should allay any fears.
- Unix domain sockets are unreliable and data corruption is possible.
FALSE - At least in this case. There is a type of UDS that is defined as unreliable, called SOCK_DGRAM. But thetype = STREAM shown by the command lsof indicates that this is a SOCK_STREAM UDS which guarantees the delivery of an ordered data stream. It is highly unlikely that a database server will ever use a UDS datagram. For reference, see the definition of SOCK_STREAM .
- On modern Linux systems, using TCP / IP with localhost is as fast as Unix Domain Sockets.
FALSE - If the socket function() it is called with a socket domain of AF_INET o AF_INET6(Internet Protocol v4 or v6), no matter what IP address is used, all communications will have protocol overhead TCP / IP. Unix domain sockets are a type of interprocess communication (IPC) that have much less processing overhead than TCP / IP.
- While there are fewer overheads, the performance difference isn't that big.
POSSIBLE - The Local TCP / IP loopback driverin Linux it performs some optimizations, such as skipping checksum generation and verification. However, the benchmarks show that the switch from TCP / IP a UNIX domain socket it will improve connection latency and data transfer speed. The more data transferred per query, the faster the data transfer rate will be. Because WordPress uses large datasets and many database queries when saving or viewing a web page, using UNIX DOMAIN SOCKET will improve performance regardless. However, when the WordPress database is on the local host, queries only take 3% to 6% of the execution time when loading a page. You'll likely only notice a speed improvement on heavily trafficked, data-intensive websites. The real answer depends on your system, your web traffic, and your point of view.
How does this approach work?
TCP / IP is a network communication protocol. A three-way handshake (the aforementioned Three Way Handshake) must first be performed in order to subsequently establish a TCP / IP network connection and that data can be sent.
Then, all sent data is first split into smaller packets (this is necessary for effective network bandwidth sharing). The source and destination are added to an envelope around each data packet. Error-checking data is calculated and also added to the envelope. A sequence number is also added to the envelope so that the recipient can correct any out-of-service data packets. Additional data is also added to each packet envelope to help identify and route the packet over local networks. The recipient must check and correct any out-of-service packets, check each packet for errors, send an acknowledgment for each packet, and reassemble the original data block from the data within each packet.
Note: Checksum calculation and error checking are ignored for localhost connections on modern Linux systems.
Unix domain Sockets (Unix domain Sockets) do not need this additional fragmentation and data processing. They work more like reading and writing to a file, but unlike a normal file, the data is passed to system memory rather than to a disk drive. For this reason, Unix Domain Sockets move data more quickly and efficiently than TCP / IP especially where you are equipped with fast RAM at the hardware level.
The chart shows examples of WordPress page load times using TCP / IP and Unix Domain Sockets as the database connection method. There was an average speed improvement of 25% with Unix Domain Sockets. The error bars also show that Unix Domain Sockets reduced the variance in runtime by 20%.
What benefits to expect and when to use it?
We are actually talking about a pulcinella secret, as easy to implement as little used. We migrate dozens of WordPress and WooCommerce websites every day and we always (and always means always, that is 100% of cases) using localhost or 127.0.0.1 in the wp-config.php configuration
Certainly applying a virtuosity such as the one described is within everyone's reach even by making simple attempts, however obtaining tangible results only and exclusively makes sense once the other performance problems have been solved.
For example, it would be useless to recover 5ms of speed and then have 1 second latencies due to the lack of indexes on the tables, or to have TCP performance limits due to the lack of TCP BBR for example.
It is different in the case in which massive operations are carried out on the database, such as imports of CSV catalogs on WooCommerce, massive price changes, or operations of this type, including crawling of the sitemap by Google's crawlers.
In short, this trick must be considered as the icing on the cake, able to improve something already good of his and all WordPress performance optimization process which we have already extensively described, and certainly will not work miracles.
With the advent of Google Core Web Vitals and of the claim imposed by Google in obtaining a fast and performing website, however, even a few milliseconds can be important values to be taken into consideration in view of a much broader and more productive optimization and improvement strategy.
Surely the best return on this technique is what it can get you on Dedicated Servers and dedicated instances such as Cloud or VPS, adequately sized at the resource level and expertly configured at the tuning software level.
As mentioned above, if the database is on the local host, database queries represent less than 6% of the execution time while WordPress is processing a request. The overall effect of switching from TCP / IP to Unix Domain Socket will be barely noticeable except on very busy websites. But for such an easy change, why not save a little on processing time and power consumption?
As reiterated, we talked about WordPress, but the same thing is applicable to any CMS.