Storing PHP Sessions/File Caches in Memory using TMPFS
Yesterday I was looking through some application logs and noticed a significant bottleneck with I/O reads in the implemented file cache. This cache is used to temporary store processed views and records for a set duration. I looked into a couple of solutions to alleviate the intense spinning disk usage, ranging from Memcache to Redis. These products are great for large-scale applications (spread over multiple systems), however, in my case simply required a single local configuration.
This was when I found ‘tmpfs’, saving me from all sorts of issues relating to adding yet another application to the production stack. ‘tmpfs’ appears as a mounted partition on your system, however, under the hood it allocates and uses a section of physical memory (non-persistent through reboots). This means that you are able to configure directories (such as the cache) to be mounted on a ‘tmpfs’ partition. This results in the desired speed boosts, without tampering with the application logic itself. Even better, if the mount is unsuccessful for some reason, it will safety fall-back to using the persistent hard-disk solution.
Though I have been discussing this solution in the case of caches, file-based PHP sessions can be setup in a similar manner. You must first work out where session files for your PHP installation are stored. Note, that if your using PHP-FPM you may be required to modify the second configuration line.
We can then make sure that the directory has been created, along with the fall-back permissions. So as to temporary see the performance increases, we are able to mount the ‘tmpfs’ partition to the session directory, setting ownership to the desired user.
If you are a satisfied with the configuration, you can persist the partition mount across reboots by adding the following line to your ‘fstab’ file.