How do you make good use of multicore CPUs in your PHP/MySQL applications?

How do you make good use of multicore CPUs in your PHP/MySQL applications?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How do you make good use of multicore CPUs in your PHP/MySQL applications?

When a document submission hits your server, you have eight physical cores sitting idle while your application logic runs on a single thread. This is common for traditional PHP deployments. While modern web servers handle concurrency by spawning many processes to serve different clients simultaneously, the heavy lifting inside a single request—parsing HTML, updating search indexes, and executing complex queries—happens sequentially within that one process.

To utilize your dual quad-core Xeon processors effectively, you must decouple the CPU-intensive tasks from the web server's request lifecycle. The goal is to spawn background processes that can execute in parallel on separate cores and then signal completion only when all work is finished.

Isolating CPU-Heavy Tasks

The most effective strategy involves moving the computationally expensive operations out of the synchronous request path. In your specific case, HTML content parsing (Category 2) and search index updating (Category 3) are prime candidates for offloading. MySQL queries (Category 1) are also heavy, but they often benefit from connection pooling and query optimization before considering parallel execution.

You can achieve this by using a message queue or a dedicated job processor. When the web request arrives, it should enqueue tasks rather than executing them immediately. This allows the PHP process to return control to the user quickly while workers pick up the heavy lifting in the background.

A common approach in the PHP ecosystem is to use pcntl_fork on Linux systems or leverage a robust queue driver like Redis with a library such as Laravel's Queue or Symfony Messenger.

Implementing Parallel Processing with pcntl_fork

If your stack allows for it, spawning child processes directly via pcntl_fork is a low-latency way to parallelize work within a single machine. You can write a script that accepts a batch of tasks, forks N children (where N matches or is less than your core count), and distributes the workload.

Here is a conceptual example of how you might structure this in PHP:

#!/usr/bin/env php
<?php

require 'vendor/autoload.php';

// Simulate a batch of document IDs to process
$documents = ['doc_101', 'doc_102', 'doc_103', 'doc_104'];
$totalCores = 8; // Based on dual quad-core Xeon
$processCount = min(count($documents), $totalCores);

// Prepare the tasks for distribution
$tasks = [];
foreach ($documents as $id) {
    $task = [
        'id' => $id,
        'type' => 'heavy_task', // e.g., HTML parsing or index update
        'payload' => $id
    ];
    $tasks[] = $task;
}

// Distribute tasks to workers
$workerProcesses = [];
for ($i = 0; $i < $processCount; $i++) {
    $pid = pcntl_fork();

    if ($pid == -1) {
        die("Could not fork process\n");
    } elseif ($pid == 0) {
        // Child process: Execute specific tasks
        // In a real scenario, pass $tasks[$i] to the child via global or file
        // Here we simulate work and exit immediately after starting
        pcntl_exec('./worker.php', ['task_index' => $i]);
    } else {
        // Parent process: Track the PID
        $workerProcesses[] = $pid;
    }
}

// Wait for all child processes to finish
$exitCode = 0;
foreach ($workerProcesses as $pid) {
    pcntl_waitpid($pid, $status);
    if (!empty($status)) {
        $exitCode |= $status & 0xFF;
    }
}

if ($exitCode > 0) {
    echo "One or more worker processes exited with an error.\n";
    exit(1);
}

echo "All parallel tasks completed.\n";

This approach ensures that the CPU-bound work happens in parallel across multiple cores. The parent process blocks until every child finishes, satisfying your requirement to treat the submission as complete only when all processes are done.

Handling MySQL and Search Indexes

For Category 1 (MySQL queries) and Category 3 (Search index updates), you must ensure that the background workers have access to a database connection pool. Do not create new connections for every forked process without managing them carefully, as this can exhaust resources. Using a persistent connection manager or a driver like predis for Redis-backed queues is often more reliable than direct MySQL connections in a fork-heavy model.

Regarding the HTML parsing (Category 2), if the volume of documents is high, consider whether a single process per core is sufficient. Sometimes, splitting the HTML stream into chunks and processing them in parallel within a single PHP script using fread buffers can be faster than spawning OS processes due to context switching overhead. However, given your hardware (8 cores) and the nature of parsing, process-based parallelism remains a strong candidate if the parsing logic is extremely CPU-bound.

Profiling Before You Optimize

Before implementing complex multi-process architectures, you must verify that the bottleneck is actually CPU time and not I/O or database locking. Your "Related question" regarding profiling tools is critical here.

Use Xdebug with a profiler like Blackfire.io to pinpoint exactly where time is being spent. If your HTML parser is spending 90% of its time in string manipulation, parallelization will yield significant gains. However, if the database is waiting on locks or disk I/O, adding more CPU cores won't help.

Tools like Blackfire provide granular data on function execution times and memory usage, allowing you to confirm that Category 2 is indeed the primary consumer of cycles. Without this data, optimizing prematurely can introduce complexity without measurable benefit.

Final Considerations

While pcntl_fork offers direct control, modern frameworks often abstract this away. If you are using Laravel, the queue system handles worker management automatically. You can configure your application to push heavy tasks to a queue and run them with multiple workers listening on the same queue driver. This approach scales horizontally if you decide to add more servers later, whereas pcntl is strictly a vertical scaling technique for a single box.

Remember that parallel processing introduces synchronization complexity. Ensuring transactional consistency across multiple processes updating the same search index requires careful design, potentially using optimistic locking or versioning on the index tables to prevent race conditions.

Our network