July 7 2024

PHP Performance Optimization via realpath cache.

PHP's realpath cache significantly improves application performance by optimizing file path resolution.

php-realpath

PHP performance optimization is a critical component to ensuring fast and efficient web applications. One of the lesser-known but extremely powerful tools for improving the performance of PHP applications is the use of realpath cache. In this article, we will explore what realpath cache is, how it works, and how it can be used to optimize the performance of PHP applications.

Introduction to realpath cache

What is realpath cache?

The realpath cache is an internal feature of PHP that caches resolutions of real file paths. Whenever a PHP application includes or requests a file, PHP must determine the absolute path to that file. This process can be time-consuming, especially in systems with large numbers of files or complex directory structures.

How does realpath cache work?

When PHP resolves a file path, it stores the resolved path in the realpath cache, creating a sort of internal map that associates the requested paths with their absolute resolutions. If the same route is subsequently requested, PHP can quickly retrieve the resolved path from the cache, avoiding having to resolve the path through a series of operations that can be quite costly in terms of time and resources. This caching mechanism significantly reduces the execution time of file include and request operations, especially in complex applications where the same path resolution may be requested repeatedly during the lifecycle of a script. The reduction of these resolution times translates into greater overall efficiency of the application, freeing up resources that can be used for other operations, thus improving the performance and responsiveness of the system.

Realpath cache and open_basedir

It is important to note that realpath cache has incompatibilities with the directive open_basedir of PHP. The directive open_basedir limits the paths that PHP can access for security reasons by specifying a list of allowed directories. When open_basedir is active, the realpath cache may not function correctly, as resolved paths may not be cached if they fall outside of allowed directories. This can cause an increase in path resolution times and a reduction in overall performance. Therefore, when using open_basedir, it is essential to evaluate the impact on performance and consider disabling this directive if system security allows it, or find a compromise that balances security and performance.

Configuring the realpath cache

Configuration parameters

PHP offers several configuration parameters to manage the realpath cache:

  • realpath_cache_size: This parameter defines the maximum size of the realpath cache. The default value is 16K. It can be increased to improve performance if the application works with a large number of files.
  • realpath_cache_ttl: This parameter defines the lifetime (in seconds) of entries in the realpath cache. The default value is 120 seconds.

Changing parameters

You can change these parameters in the file php.ini:

realpath_cache_size = 64K
realpath_cache_ttl = 300

Checking the configuration

To check the current realpath cache values, you can use a simple PHP page:

<?php
echo 'Realpath Cache Size: ' . ini_get('realpath_cache_size') . "\n";
echo 'Realpath Cache TTL: ' . ini_get('realpath_cache_ttl') . "\n";
?>;

Realpath cache monitoring

Use of realpath_cache_size()

PHP provides a function called realpath_cache_size() which returns the current size of the realpath cache. This can be useful for monitoring cache usage and determining if it needs to be increased realpath_cache_size.

<?php
echo 'Current Realpath Cache Size: ' . realpath_cache_size() . " bytes\n";
?>;

Use of realpath_cache_get()

The function realpath_cache_get() returns an array containing the current entries in the realpath cache. This can be used to debug and understand which paths are cached.

<?php
print_r(realpath_cache_get());
?>;

Benefits of realpath cache

Reduction of path resolution time

By using the realpath cache, PHP can avoid repeatedly resolving the same file paths, reducing the overall execution time of file include and request operations.

Improved overall performance

In applications with many file inclusions, realpath cache can significantly improve overall performance by reducing load on the file system and speeding up code execution.

Reduced load on the file system

Resolving file paths requires file system accesses, which can be slow. By caching resolved paths, PHP reduces the number of file system accesses, thus improving efficiency.

Best practices for using realpath cache

Determine the optimal size

The optimal size of the realpath cache depends on the size and complexity of the application, as a larger and more complex application will require a larger cache to store all resolved paths without having to continually recalculate them. Monitoring cache usage and adjusting realpath_cache_size based on your application's specific needs can help you achieve the best performance, allowing you to find the right balance between cache memory usage and performance improvements. Constantly analyzing the needs of the application and adapting the cache size accordingly allows you to optimize the available resources, ensuring that the cache is large enough to contain all the necessary path resolutions without waste, but also small enough to not take up too much memory, thus improving overall operational efficiency.

Balance between TTL and data freshness

Setting realpath_cache_ttl too low can reduce the effectiveness of the cache, as entries will expire too quickly, forcing PHP to re-resolve paths more frequently, negating the benefits of the cache. Conversely, a value that is too high can cause problems if files are modified frequently, as the cache may contain stale paths, leading to errors or unexpected behavior in the application. It's important to find a balance that meets the needs of your application, carefully considering how often files are modified and the benefit you get from keeping paths in cache for an appropriate period of time. This balance allows you to maximize cache efficiency, reducing the load on the file system without compromising the correctness and updating of the paths used by the application.

Continuous monitoring

Monitoring realpath cache usage is critical to ensuring it is functioning optimally. Use monitoring tools, such as custom PHP scripts that call functions like realpath_cache_size() e realpath_cache_get(), to collect detailed data on cache usage and identify any bottlenecks. Analyzing this data can reveal how much cache space is being used and how often entries are invalidated. In production environments, implementing continuous monitoring solutions like New Relic or profiling tools like Xdebug can provide a more comprehensive view of cache performance. With these tools, you can collect precise metrics on path resolution time and the number of cache hits and misses, allowing you to adjust configuration parameters such as realpath_cache_size e realpath_cache_ttl in an informed way. For example, if your data shows consistently high cache usage, you might want to increase it realpath_cache_size to prevent useful entries from being removed too quickly. However, if you notice a lot of cache misses or frequent invalidations, you may need to review realpath_cache_ttl to find an optimal balance between data freshness and cache efficiency.

Conclusions

Optimizing the performance of PHP applications is a complex task that requires attention to many details. The realpath cache is a powerful tool that can offer significant improvements with minimal configuration effort. By monitoring and tuning realpath cache configuration parameters, you can reduce path resolution time, improve overall application performance, and reduce load on the file system.

In a context where performance is crucial, such as Linux hosting and systems engineering, the effective use of the realpath cache can make the difference between a slow application and a high-performance one. Making sure you understand and apply the best practices described in this article can lead to significant benefits for any PHP application.

If you would like to learn more about this topic or have specific questions, please do not hesitate to contact us. Our team of experts is always available to help you optimize your applications and ensure the best possible performance.

 

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

Back to top