Backend Development 6 min read

Improving addComment API Performance with Asynchronous Email Sending Using PHP pthreads

The article explains how a slow addComment API caused by a synchronous email‑sending step was optimized by converting the email operation to an asynchronous task using PHP's pthreads extension, detailing the problem analysis, installation of pthreads on Windows, configuration changes, and a working test example.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Improving addComment API Performance with Asynchronous Email Sending Using PHP pthreads

The addComment API was taking more than six seconds because the final step—sending an email—was executed synchronously, blocking the response to the client.

Three sequential operations were identified:

Connect to the database and modify data.

Write a user log via a logging API.

Call an external mail service to send an email.

Since the third step consumed the majority of the time, the solution was to make the email‑sending step asynchronous. PHP itself does not support multithreading, but the pthreads extension can provide true multithreading for PHP 5.3+.

Installation of pthreads on Windows

1. Verify PHP version and thread‑safety using phpinfo() .

2. Download the appropriate pthreads package from http://windows.php.net/downloads/pecl/releases/pthreads/ and select the version that matches your PHP version, architecture (x86/x64), and thread‑safety (TS).

3. Extract the zip file (e.g., php_pthreads-2.0.9-5.5-ts-vc11-x86.zip ) and copy php_pthreads.dll to D:\xampp\php\ext , and copy pthreadVC2.dll to D:\xampp\php , D:\xampp\apache\bin , and C:\windows\system32 .

4. Edit the php.ini file (the one indicated by "Loaded Configuration File" in phpinfo) and add the line:

extension=php_pthreads.dll

5. Restart the XAMPP server and confirm the extension appears in phpinfo.

Note: PHP 5 requires pthreads v2, while PHP 7 can use pthreads v3.

Testing the extension

A simple test script (e.g., test_.php ) can be run to verify that threads are created and work as expected.

Applying the solution to addComment

The original synchronous flow was replaced with an asynchronous design: after completing the first two steps, the API opens a non‑blocking socket to a helper service that handles the email sending in a separate process or thread, allowing the API to return immediately to the client.

Alternative approaches such as ParallelCurl were considered but did not meet the requirements, leading to the final implementation using a non‑blocking socket and a dedicated email‑sending service.

With the asynchronous redesign, the addComment response time is reduced dramatically, improving user experience.

BackendPerformanceMultithreadingPHPpthreads
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.