July 6 2022

Speed ​​up WordPress and maximize MySQL performance via Unix Domain Socket connection

Do Unix Domain Sockets improve WordPress performance over localhost connection? Let's see this simple trick together.

Inter-process communication on a single machine is a core functionality for many computer systems. There are two main ways to make connections locally: via TCP/IP protocol and via Unix Domain Socket (UDS). The TCP/IP protocol is a standard for communication between devices on a network and is used for communication between processes on a single machine, as well as for communication between machines on a network. On the other hand, Unix domain sockets are a communication mechanism used exclusively for inter-process communication on a single machine. Both of these options offer advantages and disadvantages depending on your specific system needs. In this post, we'll explore these two modes of communication in detail and see how to choose the one that best suits your needs.

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.

Three Way Handshakes

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 query performance?

Overview

Improve the communication speed between your WordPress system and the MySQL database with a simple change to the configuration file. Learn how to use Unix Domain Sockets on Linux systems to speed up your WordPress site. In this guide, we will give you a detailed explanation of the process and results of testing going to modify your constant DB_HOST defined in wp-config that defaults to value normally localhost or 127.0.0.1.

This tutorial will show you how to do this, explain the reasoning behind it, and test the results.

Procedure for using Unix Domain Sockets with WordPress.

The WordPress codex explains this technique in the file editing documentation wp-config.php, going to illustrate the editing methods that are very easy and within everyone's reach.

UDS WP Config

Just open the file/wp-config.php in an editor, then looking for a line that looks like this:

define( 'DB_HOST', 'localhost' );

and replacing it with this value:

define( 'DB_HOST', 'localhost:/var/lib/mysql/mysql.sock' );

you will have changed your WordPress configuration to use the MySQL socket file descriptor rather than the TCP connection.

ATTENTION: If your Hosting Provider uses a remote and not a local MySQL server, you will not be able to use this procedure in any way and you will have to settle for using the remote IP address and therefore establish a connection over TCP Over IP.

Troubleshooting known issues

If the setup shown above doesn't work and although the DB runs locally, and you have Linux shell access to your web host, you can check the Unix domain socket name using the lsof command which means list open files, or the list of open files.

You may not have the lsof command installed, however you can do so via the apt or yum utility if you have root privileges, or ask your hosting provider to install it.

[root@Server174 ~]# lsof -U | grep mysql mysqld 28112 mysql 18u unix 0x0000000006388651 0t0 777704714 /var/lib/mysql/mysql.sock mysqld 28112 mysql 25u unix 0x0000000019e2551a 0t0 1143856963 /libmysql.sock.

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 make some clarifications about UNIX Domain Sockets

There is some bad information circulating on the internet regarding this topic and it can lead to connection errors, site reachability or a lot of confusion. So let's try to clarify.

  • Unix domain sockets are very reliable
    The lsof command used to view open sockets may show the type of UDS STREAM, which indicates that it is a SOCK_STREAM UDS which guarantees delivery of data in order. This type of socket is particularly suitable for transmitting data in a continuous stream. It is very rare for a database server to use a UDS datagram as these sockets are better suited for transmitting unstructured data and do not guarantee orderly delivery of the data. In general, database servers require reliable and orderly delivery of data, so they are more likely to use STREAM type sockets. For reference, see the definition of SOCK_STREAM .
  • Unix Domain Socket is faster than TCP
    The socket() function is a fundamental component in creating inter-process communication connections. When called with a socket domain of AF_INET or AF_INET6, regardless of the IP address used, all communications will have TCP/IP protocol overhead. However, Unix domain sockets offer an alternative to interprocess communication (IPC) with significantly less processing overhead than TCP/IP. These sockets are an ideal option for systems that require high speed communication and low amount of processing.
  • The performance difference is not that big.
    The TCP/IP loopback driver in Linux, to optimize its operation, adopts some techniques such as avoiding the generation and verification of the checksum. Benchmarks show that switching from TCP/IP to UNIX Domain Sockets improves both connection latency and data transfer speed. In particular, the more data transferred per query, the faster the data transfer rate will be. For example, WordPress being a system that uses large datasets and lots of database queries when saving or viewing a web page, using UNIX DOMAIN SOCKET can improve system performance. However, it should be noted that when the WordPress database is on the local host, queries only take 3% to 6% of the execution time when loading a page. Therefore, you may only see an improvement in speed on heavily trafficked and data-intensive websites.
  • The use of Unix Domain Sockets allows you to avoid saturation of TCP ports
    The number of ports available for TCP connections is limited, and port saturation can cause performance problems on a server. Also, a large number of global TCP connections can cause system overload and performance degradation. By using UDS, instead of creating new TCP connections, a file system is used to create a connection point between processes, avoiding saturation of global server ports and connections.
  • The use of Unix Domain Sockets allows for greater security
    Using UDS allows for greater security and control than TCP connections, because UDS can use file system access controls to allow or deny access to processes. Although this is a marginal aspect since TCP/IP also allows firewalling mechanisms, or instance binding on private IPs such as localhost, and in any case the service requires a login using credentials, it is fair to say that security is better with the use of UDS.

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.

Test result of a MySQL query

 

We wanted to test a query in our test environment and got some interesting results. The chart above 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 over 25% with Unix Domain Sockets.

Basically it was nothing new and it was already widely documented on the net, including the test of Percona, the company that develops and supports Percona Server which had already shown and demonstrated this feature and the benefits of using UNIX DOMAIN SOCKETs instead of TCP /IP.

unix-socket-domain

If you are interested in reading their results regarding the goodness of this technique, we invite you to read this post: https://www.percona.com/blog/2020/04/13/need-to-connect-to-a-local-mysql-server-use-unix-domain-socket/

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.

Conclusion

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 Sockets will be barely noticeable except on very busy Web sites. But for such an easy change, why not save some processing time and power consumption by also avoiding saturating TCP ports?

Find out how Unix Domain Sockets can improve your system performance and reduce latency. Contact our support to learn more about this topic and get the most out of your system.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Would you like to see how your WooCommerce runs on our systems without having to migrate anything? 

Enter the address of your WooCommerce site and you will get a navigable demonstration, without having to do absolutely anything and completely free.

No thanks, my customers prefer the slow site.
Back to top