How to Load WP Rocket Cache with WordPress into RAM with TMPFS

WP Rocket is one of the most highly regarded WordPress caching plugins. WP Rocket cache stores all the website pages as HTML files, reducing the need for WordPress to load first, then send queries to the WordPress database to find out themes, plugins, and content to load, which can be slow and also quite taxing on the server leading to increase higher loads.

Using any caching system will give immediate benefit and improve times compared to not using a cache system in most situations, however taking it one step further is to use caching in RAM, especially for highly trafficable sites using traditional file caching systems can not be as effective with load times and speed even with SSDs, minus the extensive read and writes that shortens the life of the hard drive.

So what is TMPFS? tmpfs uses a combination of computer RAM and disk-based SWAP space to create a filesystem, such as EXT4, that the operating system can use. Because tmpfs is located in RAM, it’s swift to read and write data to and from it, several times faster than an SSD, so combining this method with WP Rocket can take it to the next level of performance.

In the following tutorial, you will learn how to mount WP Rocket Plugin for WordPress into any Linux server.

Disclaimer

This tutorial and method are designed for dedicated servers only.

Prerequisites

  • Recommended OS: Linux server that is up to date with terminal access.
  • User account: A user account with sudo or root access.
  • Required Software: WordPress and WP Rocket Plugin

Mount WP Rocket Cache into RAM

To configure WP Rocket cache to load with TPMFS into Ram is a speedy process broken down into two areas. This method can be used not just for WP-Rocket but any cache-based file.

Do not under any circumstance think to improve performance to use TMPFS on your entire WordPress directory! You will regret this when you next restart the server and lose data designed for temporary files such as caches.

Load WP Rocket Cache with TMPFS on Terminal

The first step in your Linux terminal is to mount the folder using TMPFS. As the WP Rocket cache is already created and located at /wordpress-location/wp-content/cache/wp-rocket all you need to do is estimate how much RAM you will use to mount it.

For example, your server has 16GB of RAM, and you are using only 15 to 20%; you can set 2 to 3 GB for the cache and monitor its growth to size, and on more significant sites, increase as needed. For the example below, 3GB will be used.

Use the following command to mount using TMPFS:

Root access command:

mount -t tmpfs -o size=3G,mode=0755 tmpfs /wordpress-location/wp-content/cache/wp-rocket

Sudo access command:

sudo mount -t tmpfs -o size=3Gmode=0755 tmpfs /wordpress-location/wp-content/cache/wp-rocket

Note, you need to put the full path, example could be /var/www/html/wordpress/wp-content/cache/wp-rocket. Another thing to remember, using TMPFS into RAM is like using a hard drive; if you run out of RAM as with a hard drive, things can go wrong on your WordPress site very quickly; make sure to make the cache 25 to 50% bigger than what you need at the minimum, and you shouldn’t run out of room, remember this tutorial is for dedicated servers with the availability of resources to utilize.

To break the terminology down in the command:

  • -t – Specify the file type to be mounted.
  • -o – Specify option with size you want to allocate.
  • mode – directory permissions of 755.

How to Monitor TMPFS Cache

The good thing is to check the status of your TMPFS cache is to use the following command:

df -lh

This will show if you have successfully mounted the cache, and secondly, the usage of used vs. available so you can increase as needed.

Example below:

As you can see, we have a 6GB cache that has 4.1 MB used, which equals 1% overall, so the cache has plenty of room for growth. Check this often, especially when first deploying using the TMPFS method with WP Rocket Cache.

Load Cache Into RAM on System Boot

By default, a drawback of using TMPFS to load files into RAM is it is stored in volatile memory instead of a persistent storage device. On restart of a Linux operating system, it is erased. To permanently have the directory mounted, you will need to edit /etc/fstab.

First, open /etc/fstab using any text editor; the tutorial will use nano:

sudo nano /etc/fstab

Now, add the following line and adjust to your preference:

tmpfs /wordpress-location/wp-content/cache/wp-rocket tmpfs defaults,size=3g 0 0

Example of TMPFS mounted in the /etc/fstab file.

How to load WP Rocket Cache with WordPress into RAM with TMPFS

To save (CTRL+O) then to exit (CTRL+X).

Unmount WP Rocket Cache from RAM

If you want to remove the WP Rockets cache from your RAM, use the following command:

umount /wordpress-location/wp-content/cache/wp-rocket

Note, you may need sudo. If you are having trouble using the command, try with:


umount -f /wordpress-location/wp-content/cache/wp-rocket

Nginx Server Side WP Rocket Cache

For users with Nginx installed on their server, a little try is to replace the default try_files $uri $uri/ /index.php?$args; first checking the wp-rocket cache directory for a hit. This will reduce server load further and speed things up quickly on par if better than Nginx FastCGI.

Replace the following line that should look similar to the example below.

 location / {
    try_files $uri $uri/ /index.php?$args;
 }

Next, copy and paste the following replacing the above example.

location / {
    try_files "/wp-content/cache/wp-rocket/$http_host/$request_uri/index-https.html" $uri $uri/ /index.php?$args;
}

Now test before restarting to make sure everything is in working order.

sudo nginx -t

Next, restart your Nginx service:

sudo systemctl restart nginx

Now Nginx will first check if the files exist in your cache before sending the request to PHP, and combined with TMPFS, and this will result in very quick TTFB and lower resource use.

Comments and Conclusion

In the tutorial, you have learned how to mount WP Rockets cache into RAM to deliver better performance. Overall, this solution isn’t for everyone and is only for dedicated systems and should not be attempted on shared limited environments for resource limits and security reasons. If you have a big busy website with plenty of underused RAM, using this method should see your load times decrease with immediate effect.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!