Do WordPress cron jobs slow down page loading?

If a user visits a site and his visit triggers a cron job that is quite intensive, the page load speed will be slower for him right? As I understand the page doesn’t wait for the cron job to execute before loading, but since the cron job would be running in parallel, it might still be the case that the page loads slower since the server is busy right?

Related posts

2 comments

  1. Short answer – Nope. Any page request initializes the scheduled queue. It’s just an initialize request. Wp-cron request is a standalone request.

    so requesting URL /somepage you just initialize request to /wp-cron.php

    However – If cron event doesn’t work really well (it’s has 1000 db queries e.g. or its requesting a some really long-to-respond resource), or both, or re-scheduling cron event for each request… just like any other http request it will eat resources, CPU performance, memory, etc… if it eats enough resources, your page will become slower.

  2. The short answer is actually yes, in most cases.

    Firstly, on most set-ups, spawning a cron job incurs a 1 second delay on page load, because it is done via a loopback HTTP request with a 1 second timeout – see https://wordpress.org/support/topic/save-a-full-second-on-cron-execution/.

    Secondly, the spawned job will now be competing with the page load for database access (as well as other resources). Multiple processes can read the database concurrently; however, whenever a process is writing to the database, by default it is locked to prevent simultaneous write or read access by any other process – see https://stackoverflow.com/questions/1005206/does-sqlite-lock-the-database-file-on-reads#answer-1005218. The impact of this depends how complex the cron job’s database updates are and how long the database is actually locked for, and may be insignificant. Of course, it would also be an issue if a cron job happened to be running when a page is requested, but having cron jobs spawned on page load guarantees they’ll affect at least that page load.

    If your server/hosting permits, you are recommended to set up a scheduled cron job to run every few hours, with the command

    php -q /path/to/wp-cron.php
    

    and disable cron spawning on page load with the following entry in wp-config.php:

    define('DISABLE_WP_CRON', true);
    

Comments are closed.