Httpd – Does Linux, Apache HTTPD or PHP cache frequently / recently accessed files

apache-2.4cachehttpdiophp-fpm

Let's say we have a simple index.php file that goes:

<?php
    inclde('file1.php');
    inclde('file2.php');
    inclde('file3.php');

Presumably, this will ultimately result in three additional separate I/O requests to the disk, on top of index.php when the file is served.

Now, let's say this file is placed on a website and frequently requested. It would make sense if these files are somehow cached in memory, rather than make I/O requests every time someone visits the website.

Questions:

  1. Does Apache httpd cache index.php?
  2. What about the 3 include() operations?
  3. Does PHP request the files from OS through Apache? Does it matter which mpm model is used?
  4. Does nginx, lighttpd or any other webservers do caching?
  5. Does this also depend on the OS filesystem? i.e. will Linux OSes generally cache files that are frequently accessed?
  6. Or is there caching at an even lower-level?
  7. Or does PHP or any other server application engine somehow cache the files and I/O requests never make it to the OS?

Since disk I/O is usually the biggest bottleneck, knowing how files are cached at various levels by a web server can help in tweaking of performance, and even application development, e.g. perhaps concatenating short application files rather than multiple includes help performance.

Thanks for helping!

Best Answer

The OS has filesystem cache, which caches read / write requests to the actual hardware device.

Then to the specific questions:

  1. Apache doesn't do caching.
  2. The include() functions don't do caching in default PHP installations. If you install an OPCode cache like APC, Xcache or OPCache, then it will cache compiled versions of the code in memory.
  3. File system accesses are done via OS filesystem calls.
  4. There is no benefit in them doing any caching, therefore they don't do it. Filesystem cache is enough for static file caching, and any dynamic languages have better options for caching.
  5. Linux VFS layer does the caching, so it is not done by filesystems.
  6. Hard disks themselves have some caching also, but the benefit isn't that big on that layer, because it doesn't have any information on data structure that is beneficial for caching.
  7. Yes, with a proper OPcode cache mentioned in answer #2.
Related Topic