[wp-trac] [WordPress Trac] #63858: Trigger the wp_cron action from the shutdown hook instead of the init hook to reduce TTFB (β±οΈ Time To First Byte) and improve performance π
WordPress Trac
noreply at wordpress.org
Fri Oct 10 00:16:50 UTC 2025
#63858: Trigger the wp_cron action from the shutdown hook instead of the init hook
to reduce TTFB (β±οΈ Time To First Byte) and improve performance π
-------------------------------------------------+-------------------------
Reporter: pmbaldha | Owner:
| westonruter
Type: enhancement | Status: accepted
Priority: normal | Milestone: 6.9
Component: Cron API | Version: 2.1
Severity: normal | Resolution:
Keywords: has-patch needs-testing needs-dev- | Focuses:
note has-unit-tests | performance
-------------------------------------------------+-------------------------
Changes (by westonruter):
* keywords: has-patch needs-testing needs-dev-note => has-patch needs-
testing needs-dev-note has-unit-tests
* owner: (none) => westonruter
* focuses: => performance
* status: new => accepted
Comment:
I'm testing [this new PR https://github.com/WordPress/wordpress-
develop/pull/10205] with the following plugin active:
{{{#!php
<?php
/**
* Plugin Name: Cron TTFB Fix Test
*/
add_action(
'http_api_debug',
static function ( $response, $context, $class, array $parsed_args,
$url ) {
if ( str_contains( $url, 'wp-cron.php' ) ) {
error_log( 'Cron request spawned: ' . $url );
sleep( 1 );
}
},
10,
5
);
add_action(
'init',
function () {
if ( defined( 'DOING_CRON' ) ) {
error_log( 'Doing cron!! Spawned URL: ' .
$_SERVER['REQUEST_URI'] );
}
}
);
}}}
Note that it adds 1 second of additional latency on purpose to the
`wp_remote_post()` call to simulate the HTTP API not being correctly
handling the `timeout` and `blocking` params.
To get a baseline TTFB for the homepage:
{{{
$ curl -o /dev/null -s -w 'TTFB: %{time_starttransfer}s\n'
http://localhost:8000/
TTFB: 0.174550s
}}}
So a homepage request is 174 milliseconds.
Then I test spawning cron. On `trunk` I run the following from the command
line:
{{{
npm run env:cli cron event schedule foo '+1 second';
sleep 2;
curl -o /dev/null -s -w 'TTFB: %{time_starttransfer}s\nTTLB:
%{time_total}s\n' http://localhost:8000/
}}}
This results in:
{{{
Success: Scheduled event with hook 'foo' for 2025-10-10 00:13:59 GMT.
TTFB: 1.210728s
TTLB: 1.211613s
}}}
Note the TTFB is now 1 second worse because that HTTP request took longer,
and the Time To Last Byte (TTLB) is almost identical to the TTFB.
The expected "Doing cron!!" message also appears in my error log.
Then I switch to the branch in the PR and re-run the above command:
{{{
Success: Scheduled event with hook 'foo' for 2025-10-10 00:14:43 GMT.
TTFB: 0.185948s
TTLB: 1.196698s
}}}
I again see βDoing cron!!β appear in the error log.
Note the TTFB is back down to the original range at 162 ms, even though
the TTLB is unchanged.
All this indicates that cron is successfully moved to shutdown and that
this improves TTFB.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63858#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list