Set PHP realpath_cache_size “correctly”

Over the last few months I’ve started enabling PHP realpath_cache_size and realpath_cache_ttl for the performance benefits. At the time I followed the settings I found here. It bugged me that I was blindly setting the size of the cache without actually know what and how much storage was being used. In this post I will show you how to view the size and contents of realpath_cache. Thus enabling you to tune settings based on your usage.

Checking realpath_cache_size usage

As you search the web you’ll find blog posts telling you to set realpath_cache_size to 128K, 512K and even 1M! All these suggestions made blindly without actually knowing how much of the storage is being used. Recently I found the solution to this and thought I’d share it with those who stumble on this post.

Simply create a new php file. You can name it anything, like rpc_size.php then paste the below code into it:

Upload it to your web server eg. “/var/www” or “/var/www/html/” then visit yoursite.com/rpc_size.php and it will return how much memory realpath cache is using. Similar to:

So in my case I’m using about 27kb of realpath_cache. I had realpath_cache_size set to 256k and so I was able to reduce it to “realpath_cache_size = 64k” in php.ini.

Note that if your using Apache Prefork-mpm mod_php to run PHP (the fastest when loading only PHP and not with static content) then the realpath_cache_size is used for each apache2 client launched. So if maxclients is set to 50 in your Apache2 config and you have say 40 clients spawned, a setting of realpath_cache_size = 1M will apply 40 times! Thus you can see why – to be safe – the PHP dev team made the default settings 16k storage with 120 seconds TTL. :) PHP’s default:

Setting realpath_cache_size

My advice set yours to 256K,  then check storage using the instructions above. Finally set storage accordingly. Personally I would not set realpath_cache_size higher than 256k unless you have lots of RAM. Even then, 256k is still more than enough. For best performance simply tune realpath_cache_ttl. Basically set large, then tune to down to size.

Here’s my current realpath_cache settings in php.ini for one of my forum sites:

Oh and to view the contents of realpath_cache use:

If safe_mode or open_basedir is used in your php.ini then see this bug (workaround at the bottom of the page).Basically realpath cache is not used if safe_mode is on or if open_basedir restriction is enabled… thus causing lots of calls to lstat.

Enjoy the performance and comfort of “knowing”.

Set PHP realpath_cache_size “correctly” was last modified: August 2nd, 2014 by Hayden James

Set PHP realpath_cache_size “correctly”

7 Responses

  1. excellent post. i like yourself was setting this blindly. glad to find a way to correctly find out what realpath cache should be set to. big ups!

    Anonymous User November 29, 2012 at 9:22 am #
  2. thanks. Esp for heavier web apps such as Magento, Drupal, etc this can really help performance.

    @hydnj January 5, 2013 at 3:50 am #
  3. Thanks to this tutorial I can see what is going on behind the php engine.
    But I am puzzled by the output of int(x) value. The value goes up after a couple of refreshes then drops to 0 or a low value and starts rising again.

    My interpretation is that after hitting the value of real_path_cache_size the cache resets it selves and start over again.
    So I tried to raise the real_path_cache_size to even 128M but I think there is a limit around 256K.
    I don’t get higher int(x) values.

    Is there a limit somewhere?

    Bottom line after disabling open_basedir restriction the real_path_cache started working, And all though the cache seems to be resetting itself after seconds I noticed a 10-15% speed gain.

    Karel February 24, 2013 at 1:14 pm #
  4. Remember to set realpath_cache_ttl. I don’t know the limit but 128M is crazy to try. I’ve seen it beyond 256k with large Magento stores. I would not set to more than 1024k, better to reduce ttl in such cases as it works best on busy websites where there’s 100′s of page hits per minute or more, then a setting of even realpath_cache_size = 512k realpath_cache_ttl = 60 can improve throughput.

    @hydnj February 24, 2013 at 1:21 pm #
  5. Thank you for great post. But I’m receiving negative int(-x) values. I checked my php.ini for open_basedir, but is starting with ; and there was no value assign after “open_basedir =”, and my safe mode is off. And realpath cache = 32k is set in php.ini. This a magento store.

    Mike April 6, 2014 at 12:08 pm #
  6. It even goes to int(-4xxxxx)

    Mike April 6, 2014 at 12:12 pm #
  7. Great writeup.

    Just a couple quick questions. I am running a magento site roughly 15,000 items, and the default settings were 32kb cache, 7600 ttl, and the site was runnning slllllooooow. I adjusted the settings to cache: 256kb / ttl: 300 and noticed an immediate speed bump. Since doing the update i have been browsing around and checking the realpath_cache_size(); value and it bounces around between 140-200kb so i am guessing that the 256kb is a good value to stick with for now. But I have no clue what to do with the ttl value. I picked 300 because the average session duration is under 5min and figured this would cover that, but should it be even lower? Do you recommend just dropping it by 100 each week then reviewing my logs to see average load times?

    thanks again for great write up!

    Mickey September 4, 2014 at 9:11 pm #

Leave a Reply