Puritan Board Server Optimizations

Semper Fidelis

2 Timothy 2:24-25
Staff member
I thought I'd provide an update on the work I've been doing behind the scenes over the last couple of weeks to address server load and improve the performance of the Puritan Board.

The Puritan Board shares a fairly powerful dedicated server with a number of other websites that I host. One of the challenges in administering a server like this is that performance problems are not always attributable to a single website or service. There are also a number of inefficiencies that can accumulate over the years as applications are upgraded, plugins come and go, and the nature of Internet traffic changes.

The sheer volume of automated traffic on the Internet has also become a significant problem. Between search engine crawlers, AI scrapers, malicious bots, and other automated processes, there is an enormous amount of traffic hitting websites that has nothing to do with actual human beings visiting them.

I've spent considerable time examining virtually every major component of the server, and I thought some of the more technically inclined members might appreciate an explanation of what I've been doing.

Cloudflare and Bot Traffic

I've been experimenting with a number of Cloudflare protections to reduce the amount of automated traffic that actually reaches the server. I've examined access logs, identified problematic IP addresses and networks, and experimented with various challenge and rate-limiting rules.

Some of these measures worked better than others. I experimented with restricting direct web traffic to the server so that requests had to come through Cloudflare, but ultimately reversed that change because of complications with other services and websites.

I also experimented with more aggressive challenges on certain areas of the Puritan Board, only to discover that some of these interfered with legitimate XenForo functionality, including the login process. I subsequently modified or removed rules that were causing problems for members.

I currently have Cloudflare's "I'm Under Attack" mode enabled. I recognize that this introduces some inconvenience for visitors, and I don't intend to leave it enabled indefinitely. My plan is to disable it in a few days and assess how the server performs with the other optimizations in place.

Elasticsearch

The Puritan Board uses Elasticsearch to provide its search functionality. One of the things I discovered was that Elasticsearch had been configured with a Java heap allocation of nearly 14.5 GB on a server with approximately 28 GB of RAM.

That was consuming a disproportionate amount of the server's available resources relative to the size of the Puritan Board's search index.

I reduced that allocation to 6 GB, which provides substantial capacity for our search requirements while making significantly more memory available for other services.

PHP Optimization

One of the more significant discoveries was how inadequately PHP's OPcache had been configured for the number and complexity of websites on the server.

For those unfamiliar with OPcache, PHP applications consist of scripts that have to be compiled into instructions that PHP can execute. OPcache allows those compiled instructions to be retained in shared memory rather than requiring PHP to repeatedly compile the same scripts.

I substantially increased the memory available to OPcache, increased the number of scripts it could accommodate, and expanded the memory available for shared strings.

The server is now caching more than 93,000 PHP scripts, and the cache hit rate is approximately 99.89%. Nearly 90.4 million cache hits have been recorded in just a few hours of operation following the latest configuration changes.

I also reconfigured PHP-FPM, which manages the PHP processes that serve the various websites. This included optimizing the way processes are created, how long they remain available, and how frequently they are recycled.

I subsequently examined the actual private memory consumption of these processes to ensure that the changes were not simply exchanging one performance problem for another. The results have been very encouraging.

Redis and Persistent Object Caching

Another fairly substantial undertaking was implementing persistent object caching across 36 WordPress installations that reside on the same server as the Puritan Board.

WordPress can be rather inefficient in the number of database queries and other operations it performs to generate pages. Persistent object caching allows the results of many of these operations to be retained in memory and reused.

I established 31 dedicated Redis instances, segregated by hosting account, so that the various websites could benefit from persistent object caching without interfering with one another.

The results have been impressive. One of the busier hosting accounts alone has already recorded more than 7.6 million Redis cache hits, with a hit rate of nearly 97%.

All of these additional Redis services combined are currently consuming less than 700 MB of actual resident memory.

WordPress Scheduled Tasks

Another source of inefficiency was the manner in which WordPress handles scheduled tasks. By default, WordPress relies on website traffic to initiate checks for scheduled background operations.

Multiply that across dozens of websites, particularly when those websites are being visited by large numbers of automated bots, and it creates a considerable amount of unnecessary activity.

I replaced this mechanism with server-managed scheduled tasks and staggered their execution so that multiple websites would not all attempt to perform their background maintenance at the same time.

Video Metadata Caching

One of the more surprising discoveries involved websites that display embedded Vimeo videos using certain WordPress page-building tools.

Vimeo informed me that the server had been generating more than a million requests per day for video metadata. In some cases, information about the same video was being requested more than 100 times per minute.

I developed a WordPress plugin to cache Vimeo and YouTube video metadata locally, eliminating the need for the websites to repeatedly contact those services for information they had already obtained.

This not only reduces unnecessary server activity but also addresses problems with Vimeo throttling requests from the server.

Database Optimization

I also spent considerable time optimizing MariaDB, the database engine used by the Puritan Board and the other websites.

Among other things, I:

  • Increased the InnoDB buffer pool from approximately 110 MB to 4 GB.
  • Increased the capacity of the database's table caches.
  • Converted 124 WordPress database tables from the older MyISAM storage engine to InnoDB.
  • Removed numerous obsolete database tables left behind by plugins that had been uninstalled years ago.
  • Optimized heavily fragmented database tables, recovering more than a gigabyte of wasted disk space.
  • Cleaned up obsolete WordPress options that were being unnecessarily loaded into memory.
  • Removed remnants of outdated security, statistics, and e-commerce plugins.
  • Enabled slow query logging to identify any remaining database performance bottlenecks.
Some of the database cleanup was quite remarkable. One security logging table on another website was consuming more than a gigabyte of disk space, almost all of which was wasted space. After optimization, that same table occupied less than 19 MB without losing its records.

The recent InnoDB cache hit rate is now approximately 99.95%. The rate at which MariaDB has to retrieve pages that are not already in its buffer pool has also declined dramatically.

I've temporarily configured MariaDB to log queries taking longer than a quarter of a second so that I can identify and investigate any remaining inefficiencies.

Where Things Stand Now

The cumulative results of these optimizations have been extraordinary.

A couple of weeks ago, I was frequently observing periods during which the server had very little available memory, was using nearly all of its allocated swap space, and was struggling under significant bursts of activity.

As of this afternoon:

  • The server has approximately 10 GB of available RAM.
  • There is no significant active swapping in the latest monitoring samples.
  • CPU utilization is quite low during normal periods of activity.
  • The five-minute server load average has fallen to approximately 1.5 on a server with 12 logical processors.
  • PHP's compiled script cache has a hit rate of nearly 99.9%.
  • The database's recent InnoDB cache hit rate is approximately 99.95%.
  • All 36 WordPress installations are benefiting from persistent object caching.
  • PHP has processed more than 86,000 requests since this morning without recording a slow request in its current monitoring statistics.
What is particularly satisfying about all of this is that I have not had to purchase additional hardware or increase the server's hosting costs. I've simply been able to make much more effective use of the resources that are already available.

There is something of a cumulative effect to these optimizations. Reducing unnecessary PHP operations reduces database activity. Improving database caching reduces disk activity. Reducing unnecessary scheduled tasks frees up CPU resources. Caching objects and video metadata eliminates operations that never needed to be repeated in the first place.

I'm not yet declaring victory over the bot traffic problem. The real test will come when I disable Cloudflare's "I'm Under Attack" mode and observe how the server behaves under more normal traffic conditions. Some of the improvement in server load is undoubtedly attributable to the Cloudflare protections currently in place, and I want to distinguish that from the improvements attributable to the server itself.

Nevertheless, the underlying efficiency of the server is demonstrably better than it was two weeks ago, and it now has considerably more capacity to deal with periods of heavy traffic.

I appreciate everyone's patience with the occasional inconveniences as I've worked through these issues. There is quite a bit involved in keeping a site like the Puritan Board running, particularly in an Internet environment where an increasing proportion of the traffic consists of machines talking to other machines.

Hopefully, the end result will be a faster, more responsive, and more reliable Puritan Board for those of us who are actually human.
 
A few more updates on things I've discovered since my last post.

One interesting discovery last night was that some of our old vBulletin redirects were no longer working. When we migrated from vBulletin to XenForo years ago, we retained the ability to redirect links to old threads and posts to their corresponding locations in XenForo. There are links to Puritan Board discussions scattered across the Internet going back more than 20 years. Some of the legacy redirect scripts were no longer compatible with our current version of XenForo and were generating server errors. I've implemented the newer redirect mechanism and verified that old links are once again resolving to the appropriate discussions.

I've also made some additional discoveries concerning the amount of automated traffic hitting the Board. I've implemented some fairly extensive monitoring on the server that collects performance statistics every 30 minutes. What is remarkable is how much the traffic can vary over the course of a few hours. At one point this morning, the Board was receiving nearly 15 times the number of requests it was receiving yesterday evening, even with Cloudflare's heightened security measures in place.

I discovered this morning that some traffic was reaching the server without passing through Cloudflare's protections. I've corrected a configuration issue and am monitoring the results before implementing some additional protections.

There have also been some additional refinements to the way the server handles caching and background processing. Since the server hosts a number of other websites besides the Puritan Board, improvements to the efficiency of those sites also benefit the Board by freeing up resources.

The encouraging news is that, despite some extraordinary fluctuations in traffic, the server is handling the workload much better than it was before. The database is now finding more than 99.97% of requested InnoDB data pages in memory, and PHP is achieving a similar cache hit rate for compiled code. Both of these improvements substantially reduce the amount of work the server has to perform to serve a page.

I'm increasingly convinced that we have sufficient server resources to handle the legitimate traffic the Board receives. Much of the remaining work involves figuring out how to prevent the server from expending those resources on unnecessary traffic.

I'm trying to be fairly methodical about the remaining changes so I can measure their effects rather than simply changing a bunch of settings and hoping for the best. I appreciate everyone's patience as I continue working through these issues.
 
Another Server Update: Under Attack Mode Disabled

I'm pleased to report that I've been able to turn off Cloudflare's "I'm Under Attack" mode and the Board is currently performing very well.

Since my last update, I've spent considerable time analyzing the nature of the traffic hitting the Puritan Board. One of the benefits of the monitoring I've put in place is that I can now correlate the traffic reaching the server with its actual resource utilization and better understand what is causing the performance problems.

I conducted an experiment earlier this afternoon in which I disabled Under Attack mode. Within a few minutes, the amount of traffic being processed by PHP increased from about 4 requests per second to more than 80. CPU utilization increased dramatically, and I had to re-enable Under Attack mode.

What was encouraging, however, was that the other server resources held up remarkably well. We still had more than 9 GB of available memory, the database was performing extremely efficiently, and there was virtually no disk contention. This helped confirm that many of the optimizations I've made over the past several days are doing what I intended them to do.

I then spent some time analyzing the requests themselves. In less than five minutes, the server received approximately 17,400 requests for threads and individual posts from nearly 16,000 distinct IP addresses. More than 91% of those IP addresses made only a single request. This is strongly suggestive of distributed, automated browsing of the Board's content. It also explains why some of the more conventional approaches to blocking excessive traffic have had limited effectiveness.

I've since refined the security rules in Cloudflare to deal more specifically with this kind of traffic. Rather than subjecting everyone visiting the Puritan Board to heightened security checks, I'm now applying more targeted protections to the types of requests that have been generating the excessive workload.

You may still encounter an occasional Cloudflare Managed Challenge as you navigate the Board, but you should no longer be subjected to the more intrusive site-wide Under Attack mode. I've tested logging in, browsing discussions, navigating to individual posts, What's New, quoting, replying, and several other Board functions, and everything appears to be working properly.

I've also accomplished quite a bit of housekeeping since my last update:

  • Conducted a comprehensive DNS audit covering nearly 500 hostnames associated with the websites on the server.
  • Removed more than 50 obsolete DNS records accumulated over many years of hosting websites.
  • Corrected some inconsistencies in the way website traffic was being routed through Cloudflare.
  • Verified that our database performance optimizations survived a server reboot.
  • Verified that the dedicated caching services for the other websites on the server are operational and that the scheduled background processing we've been monitoring is functioning properly.
  • Continued refining the automated performance monitoring so I can better diagnose problems when they occur rather than relying on a snapshot of server activity after the fact.
Since disabling Under Attack mode for the second time this afternoon, PHP is processing about 12 requests per second rather than the 80-plus we experienced during the earlier experiment. At the most recent measurement, nearly 80% of the server's CPU capacity was idle, and the server had more than 9 GB of available memory. The database continues to achieve a cache hit rate well above 99.9%.

I'm going to leave things alone for a while and see how the server performs overnight. There are still a few things I intend to address, but I think we're finally getting to the point where the server is operating efficiently and we have a much better handle on the traffic reaching it.

Please let me know if you encounter any difficulties with normal Board functionality. In particular, I'd like to know if Cloudflare challenges are interfering with anything you're trying to do.

Thank you again for your patience over the past several days.
 
Back
Top