Reduced cpu usage workers

For a client, we have dozens of worker instances running, continuously executing jobs via the command line. The application is built with Symfony, using PHP 8.1, and deployed on the Amazon ECS platform.

The CPU usage is relatively high, even when these workers are performing simple "hello world" actions. This is because there is no default opcache available for PHP command line executions. As a result, every time a command is executed, all the necessary PHP files required for command startup need to be interpreted without any optimizations. With a mature application like the one based on the Symfony framework, there are quite a few files involved.

To address this issue, we enabled opcache but disabled the use of shared memory, allowing the application to benefit from opcache optimizations.

This approach has resulted in the following benefits:

  • Significant reduction in baseline CPU usage in the Docker containers running the workers.
  • Decreased execution time for a "hello world" command from approximately 3 seconds to 300 milliseconds.

The outcome is cost savings and improved speed performance.

Average and maximum cpu usage of the worker before and after the optimization; running on a 0.25 vcpu (Intel) Fargate task in AWS ECS:

Post date: June, 2023

What is PHP Opcache?

PHP OPcache is a built-in caching extension for PHP that enhances performance by storing precompiled script bytecode in shared memory. It eliminates the need for repetitive compilation and parsing, resulting in faster execution of PHP scripts.

OPcache stores the compiled code in memory, reducing the disk I/O and CPU overhead required for script execution. By caching the bytecode, OPcache reduces the server's response time and improves the overall PHP application performance.

It's a valuable tool for optimizing PHP-based websites and applications, ensuring efficient utilization of server resources and enhancing user experience.