没有不值得去解决的问题,也没有不值得去学习的技术!

Continued WordPress CPU Troubleshooting: Yoast Sitemap 500, PHP 256M OOM, and W3TC Cache Preload Failure

Figure 3: PHP-FPM log showing Allowed memory size of 268435456 bytes exhausted

作者:

在

My previous post recorded the abnormal traffic I observed at the EdgeOne layer after this CPU alert occurred, as well as the adjustments I ultimately made to adaptive rate limiting, JavaScript challenges, and anti-hotlinking within the capabilities of the personal edition.

However, EdgeOne primarily resolved issues at the traffic entry layer.

In fact, before this CPU alert, I had already noticed that the page cache status of W3 Total Cache was somewhat off.

My Page Cache lifetime is set to 4 days, so logically I should be able to see page files surviving for 2 days, 3 days, or even nearly 4 days in the cache directory. But during previous actual checks, I found that the cache was generally very fresh, with almost no pages older than 2 days.

I didn’t continue investigating deeper at the time because I was batch-processing a large number of historical articles.

Batch updating articles can itself trigger page cache invalidation, homepage refreshes, and other operations, so seeing a short cache age at that stage did not clearly distinguish:

Whether there was an actual issue with W3 Total Cache itself, or if it was normal cache invalidation caused by the batch processing of historical articles.

So this issue was temporarily shelved at the time.

The batch processing of historical articles has long since finished, and the site has returned to a relatively stable operating state. If the page cache still consistently fails to exceed 2 days, the explanation of “articles are being batch updated” no longer applies.

Since I encountered another server CPU alert this time, I decided to thoroughly investigate this lingering issue:

Why, with a Page Cache lifetime clearly set to 4 days, do the actual cached pages consistently fail to last more than 2 days?

As I continued investigating, I first discovered an issue that had been persisting for about half a month:

The combined Sitemap used by W3 Total Cache for page cache preloading had actually been failing to generate all along.

Tracing further, I ultimately pinpointed the issue to Yoast SEO’s post-sitemap.xml: when generating the post Sitemap, the PHP-FPM 256M memory limit was exhausted, resulting in an HTTP 500 response.

This time, I will continue documenting the server-side troubleshooting process.


1. W3TC Combined Preload Sitemap Consistently Failing to Generate

My WordPress uses W3 Total Cache’s Disk Enhanced page caching, with page cache preloading enabled.

To preload both the Chinese and English sites simultaneously, I previously wrote a custom combined Sitemap generation script that merges the URLs from:

Plaintext
www.shuijingwanwq.com

and:

Plaintext
en.shuijingwanwq.com

into:

Plaintext
w3tc-preload-sitemap.xml

which is then fed to W3TC Preload.

While investigating further, I checked the generation logs and found consecutive entries showing:

Plaintext
status: FAILED
读取失败:https://www.shuijingwanwq.com/post-sitemap.xml
curl: (22) The requested URL returned error: 500

There was also one occurrence of:

Plaintext
curl: (28) Operation timed out after 30002 milliseconds with 0 bytes received
Figure 1: W3TC combined Sitemap generation task failing consecutively, primarily stuck on the www site's post-sitemap.xml
Figure 1: W3TC combined Sitemap generation task failing consecutively, primarily stuck on the www site’s post-sitemap.xml

This means the combined Sitemap had not been updating properly for a long time, not because W3TC itself couldn’t read the Sitemap.

The problem occurred earlier:

Plaintext
Combined Sitemap generation script
        ↓
Reads www's post-sitemap.xml
        ↓
HTTP 500 / Timeout
        ↓
Combined Sitemap generation fails

This also explains why W3TC’s page preloading hadn’t been receiving the latest URL list previously.


2. First Ruling Out Linux Cron Not Running

Seeing a scheduled generated file not updating for a long time, the first natural suspicion is:

Is Linux Cron simply not running properly?

The system Cron for this combined Sitemap is configured to execute at the 17th minute of every hour.

Continuing to check /var/log/cron, I could clearly see:

Plaintext
09:17:01
10:17:01
11:17:01
12:17:01
13:17:01
14:17:01
15:17:01
16:17:01
17:17:01
18:17:01

It was triggering normally every hour:

Plaintext
/usr/bin/flock -n /run/lock/swq-w3tc-preload-sitemap.lock
/usr/local/bin/swq-w3tc-preload-sitemap.py
Figure 2: Linux Cron executing the combined Sitemap generation script normally at the 17th minute of every hour
Figure 2: Linux Cron executing the combined Sitemap generation script normally at the 17th minute of every hour

So we can rule out one direction here:

It’s not that the scheduled task isn’t running, but rather that every time the scheduled task runs, it fails when reading the upstream Sitemap.

The approaches to these two situations are completely different.

If Cron simply wasn’t running, fixing Cron would be the end of it.

The current problem clearly requires tracing further into WordPress / PHP.


3. Why Does www’s post-sitemap.xml Return 500?

Next, I tested directly:

Plaintext
https://www.shuijingwanwq.com/post-sitemap.xml

and found that it indeed returns an HTTP 500.

The English site’s:

Plaintext
https://en.shuijingwanwq.com/post-sitemap.xml

also had a similar issue.

But the site’s:

Plaintext
sitemap_index.xml

was accessible normally.

Therefore, the problem was further narrowed down:

It wasn’t that the Yoast Sitemap functionality was entirely broken, but rather that an exception occurred when actually generating the post Sitemap.

After checking the PHP-FPM logs, I quickly found the most critical line:

Plaintext
PHP Fatal error:
Allowed memory size of 268435456 bytes exhausted

Here:

Plaintext
268435456 bytes

is exactly:

Plaintext
256 MB

The logs also showed the exception occurring in:

Plaintext
wp-includes/class-wpdb.php

during the database result reading process.

Figure 3: PHP-FPM log showing Allowed memory size of 268435456 bytes exhausted
Figure 3: PHP-FPM log showing Allowed memory size of 268435456 bytes exhausted

Combining this with the complete call stack at the time and tracing upwards, I could see the request entering Yoast SEO’s post Sitemap generation logic, including:

Plaintext
wpdb->get_results()
WPSEO_Post_Type_Sitemap_Provider->get_posts()

Therefore, the direct cause of post-sitemap.xml returning an HTTP 500 could be confirmed:

When Yoast SEO generates the post Sitemap, a single PHP request exceeds the current 256M memory_limit.


4. Why Does Generating a Sitemap Consume So Much Memory?

This actually surprised me a bit at first.

A Sitemap seems to just generate a batch of URLs, which theoretically shouldn’t require a particularly large amount of memory.

But looking further into Yoast’s implementation, you find it’s not a simple query for:

Plaintext
post ID

followed by concatenating URLs.

Generating a post Sitemap requires reading a certain amount of post data.

By default, a single Yoast Sitemap can contain approximately:

Plaintext
1000 URLs

My WordPress has accumulated several thousand articles, many of which are lengthy technical posts containing large amounts of code, logs, and configuration content.

Therefore, the actual volume of data a single Sitemap request needs to process is not small.

When the PHP-FPM single-request memory limit is only:

Plaintext
256M

the current scale of articles can already hit this limit.

This HTTP 500 was actually the threshold being genuinely hit.


5. Two memory_limit Entries Still Exist in php.ini

Continuing to check:

Plaintext
/usr/local/php/etc/php.ini

I found another spot prone to causing confusion.

There are actually two entries in the configuration file:

Plaintext
memory_limit = 128M

and:

Plaintext
memory_limit = 256M

Before modification, their positions were approximately at lines:

Plaintext
430
1883

The one that actually took effect in the end was the latter:

Plaintext
256M

To avoid future confusion caused by duplicate configurations, I didn’t just modify one of them this time; I unified both to:

Plaintext
memory_limit = 512M

After the modification, checking again:

Plaintext
430:memory_limit = 512M
1883:memory_limit = 512M

The actual effective value for PHP-FPM also became:

Plaintext
memory_limit => 512M => 512M
Figure 4: php.ini originally had an effective 256M; after adjustment, both memory_limit entries are unified to 512M
Figure 4: php.ini originally had an effective 256M; after adjustment, both memory_limit entries are unified to 512M

A special note is needed here:

Setting memory_limit to 512M does not mean that every PHP-FPM worker will immediately occupy 512M upon starting.

It is merely the memory limit allowed for a single PHP request.

However, my server doesn’t have a lot of total memory, so this adjustment shouldn’t be interpreted as “the more memory, the better.”

More accurately, the current 256M has been proven insufficient by an actual failure, so I increased it to 512M first and will continue monitoring.

If high concurrency later causes overall server memory pressure due to high single-request memory usage, I will need to re-evaluate.


6. After Adjusting to 512M, post-sitemap.xml Returns to Normal

After modifying php.ini, I first checked the PHP-FPM configuration for syntax errors, then reloaded PHP-FPM.

I then requested the Chinese site again:

Plaintext
https://www.shuijingwanwq.com/post-sitemap.xml

Test results:

Plaintext
HTTP: 200
Size: 756668 bytes
Time: 8.156836s

Testing the English site:

Plaintext
https://en.shuijingwanwq.com/post-sitemap.xml

The result also recovered:

Plaintext
HTTP: 200
Size: 224628 bytes
Time: 15.314945s
Figure 5: After PHP memory_limit is increased to 512M, both Chinese and English post-sitemap.xml return HTTP 200
Figure 5: After PHP memory_limit is increased to 512M, both Chinese and English post-sitemap.xml return HTTP 200

This step is quite critical because it wasn’t just seeing the logs stop reporting errors, but directly verifying the originally failing target URL.

Before the fix:

Plaintext
post-sitemap.xml
→ HTTP 500

After the fix:

Plaintext
post-sitemap.xml
→ HTTP 200

So, at least for this Sitemap 500 issue, insufficient 256M memory was the direct cause.


7. Combined Preload Sitemap Finally Resumes Updating

After the upstream post-sitemap.xml recovered, the combined Sitemap generation script was naturally able to retrieve the complete Sitemap data again.

Right after completing the fix, I manually regenerated the combined Sitemap once, which counted:

Plaintext
www   3321
en    3323
total 6644

Later, when preparing the screenshots for this article, the system Cron continued to update automatically, and it currently shows:

Plaintext
www: 3323
en : 3325
all: 6648
Figure 6: Combined preload Sitemap has resumed continuous updating, currently containing 6648 URLs
Figure 6: Combined preload Sitemap has resumed continuous updating, currently containing 6648 URLs

This actually verifies the previous conclusion once again:

Linux Cron itself was never broken.

It executed every hour previously, but only because:

Plaintext
post-sitemap.xml = 500

it kept failing to generate.

Now that the upstream has returned to normal, the same system Cron can continue to automatically update the combined Sitemap without any modifications.

As of the screenshot time:

Plaintext
Chinese site: 3323
English site: 3325
Total: 6648

The number of URLs has continued to grow.


8. This Means Newly Added Pages Were Not Entering the Latest Preload List

The prolonged failure to generate the combined Sitemap previously had another rather direct impact:

Articles and pages added during this period did not enter the latest W3TC combined preload Sitemap in a timely manner.

The old file previously contained approximately:

Plaintext
www: 3197
en : 3213
Total: 6410

Whereas it has now grown to:

Plaintext
www: 3323
en : 3325
Total: 6648

The difference between the two is:

Plaintext
6648 - 6410 = 238

This means that as the site continued to update, a noticeable gap emerged between the old preload list and the current actual number of URLs.

This doesn’t mean that these 238 URLs definitely have no page cache at all.

Because W3 Total Cache can still generate cache normally after users or crawlers actually visit the pages.

However, their absence from the latest active preload list means:

We can no longer rely on W3TC Preload to actively and continuously cache these new pages in advance.

For a personal blog with low normal traffic, this issue might not be obvious normally.

But once a sudden surge of automated access occurs, the first request to an unpreloaded page requires actual execution of WordPress / PHP, which can increase instantaneous server pressure.


9. Current Preload Speed Can Still Cover the 4-Day Cache Lifetime

In W3 Total Cache’s current Page Cache configuration:

Plaintext
Cache lifetime: 345600 seconds

which is:

Plaintext
4 days

Preload is currently set to:

Plaintext
Execute every 300 seconds
Preload 7 URLs each time

After conversion:

Plaintext
Per hour:
12 × 7 = 84 URLs

Per day:
84 × 24 = 2016 URLs

Based on the latest count at screenshot time:

Plaintext
6648 URLs

Completing a full cycle takes approximately:

Plaintext
6648 ÷ 2016 ≈ 3.30 days

which is about:

Plaintext
3 days 7 hours

Compared to the:

Plaintext
4-day cache lifetime

this theoretically still leaves a margin of about:

Plaintext
17 hours

Therefore, I did not increase the:

Plaintext
7 URLs / 5 minutes

any further because of this issue.

After all, this ECS only has 2 vCPUs.

Preloading itself is also a real page request; setting the speed too aggressively could conversely create sustained PHP pressure.

At this stage, it is more appropriate to first restore the original mechanism and then observe whether the cache can gradually form a stable 1-day, 2-day, and 3-day lifetime.


10. Why Not Directly Reduce the Number of URLs per Sitemap in Yoast This Time?

Since the problem occurred when Yoast generated a larger post Sitemap in a single pass, another potential solution was actually:

Reducing the number of URLs per Sitemap from the default of about 1000.

Theoretically, this would indeed reduce the amount of data a single request needs to process.

However, I didn’t go with this approach first this time.

The reason is that the current 256M limit wasn’t only hit by the Sitemap.

That same day, the PHP-FPM logs also showed other WordPress / W3TC requests hitting the 256M memory limit.

This indicates that as the site scales:

256M has already become tight for the current WordPress production environment.

Therefore, first increasing the PHP-FPM single-request limit to:

Plaintext
512M

is more in line with the current actual situation.

I temporarily kept Yoast’s Sitemap pagination count at its default, avoiding adjusting multiple variables simultaneously.

If the Sitemap starts consistently consuming abnormally high memory in the future, I will then separately consider reducing the number of URLs per Sitemap.


11. The Problem Wasn’t Actually “W3TC Preload Being Too Slow”

There is a rather easy trap to fall into during this troubleshooting.

When I first noticed the page cache count was inconsistent with expectations, it was easy to suspect:

Plaintext
Is W3TC preloading only 7 URLs at a time too slow?

So naturally, I thought about increasing:

Plaintext
7

directly to:

Plaintext
10
20
or even more

But continuing to investigate revealed that the real problem wasn’t “too few preloads per batch” at all.

It was:

Plaintext
W3TC Preload
        ↓
Depends on combined Sitemap
        ↓
Combined Sitemap needs to read Yoast post-sitemap.xml
        ↓
post-sitemap.xml returns 500 due to 256M OOM
        ↓
Combined Sitemap generation fails long-term
        ↓
W3TC never gets the latest URL list

If I hadn’t continued investigating the cause and just kept increasing the preload speed, not only would it fail to solve the problem, but it could further increase server pressure.

This is why performance issues can’t be diagnosed just by staring at the last parameter that manifests.


12. Behind the CPU Alert, There Was Actually More Than One Issue

Returning to the initial CPU alert.

The first post already confirmed:

Plaintext
Short-term massive abnormal access

was indeed an important external factor triggering the high server load.

But continuing the analysis towards the origin server, I also discovered:

Plaintext
W3TC combined preload Sitemap long-term failure

This was another internal issue that had been present for some time.

So this incident cannot be simply summarized as:

EdgeOne failed to block abnormal traffic.

A more accurate understanding should be:

Plaintext
External:
Short-term abnormal automated traffic
        ↓
EdgeOne massive requests to origin

Internal:
Partial page preload mechanism long-term abnormality
        ↓
More requests rely on the origin server's actual cache status
        ↓
PHP-FPM bears greater pressure

With the issues on both sides stacking, a server with only 2 vCPUs is naturally more easily pushed to 100% CPU.


13. I Thought It Was Over Here, But There Was Another Layer

Reaching this point, I originally thought I could wrap things up.

Because several key issues had already been restored:

Plaintext
Yoast post-sitemap.xml
500 → 200

PHP memory_limit
256M → 512M

Combined Sitemap
Resumed automatic updating

URL count
6410 → Current 6648

Next, as long as W3 Total Cache followed:

Plaintext
Every 5 minutes
Preload 7 URLs

to continue running, it should be able to gradually rebuild the complete page cache.

But while checking further, I discovered an even stranger issue:

W3TC’s w3_pgcache_prime and w3_pgcache_cleanup WP-Cron Events were surprisingly not working properly.

Even more absurdly:

Plaintext
Manually create Event
→ Shows success

Execute wp-cron.php
→ Event disappears again

To finally verify whether W3TC itself was deleting the tasks, I even temporarily created a completely random Cron Probe.

As a result, a new Event scheduled for two hours later, unrecognized by any plugin, also disappeared after running wp-cron.php.

Continuing to trace this, a very critical set of results ultimately emerged:

Plaintext
Database:
DB = YES

WordPress get_option("cron"):
NO

That is to say:

MySQL already contained the new Cron data, but WordPress was reading an old version from the Object Cache.

This issue ultimately led to the relationships among:

Plaintext
WP-Cron
WP-CLI
W3TC Redis Object Cache
alloptions
Multiple domains
Polylang
admin.shuijingwanwq.com

This part is already a relatively independent issue, so I will leave it to be documented separately in the next post.


14. Solving the “Origin Server Preload Entry” First This Time

At this point, a fairly clear conclusion can be drawn for this stage.

Initially seeing:

Plaintext
W3TC page cache seemingly not meeting expectations

did not mean:

Plaintext
W3TC Disk Enhanced itself was broken

The first issue discovered this time actually occurred upstream from it:

Plaintext
Yoast Sitemap
        ↓
PHP memory insufficient
        ↓
post-sitemap.xml 500
        ↓
Combined Sitemap generation fails
        ↓
Preload URL list long-term unable to update

Ultimately, through:

Plaintext
PHP memory_limit
256M → 512M

the post-sitemap.xml for both the Chinese and English sites was restored, and the combined Sitemap resumed automatic updating.

And currently:

Plaintext
6648 URLs

at a preload speed of 7 URLs every 5 minutes, it is theoretically still possible to complete a full preload cycle within the 4-day Page Cache lifetime.

Therefore, at this stage, I did not further increase the preload intensity, nor did I perform any full page cache purges.

Letting the existing cache run naturally is the more prudent approach right now.

As for why the WP-Cron Event later inexplicably disappeared, and how I ultimately caught the stale Redis alloptions cache using DB=YES / get_option=NO, I will continue in the next post.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系