Speeding Up Nginx Caching With tempfs

July 5, 2014
screen-shot-2014-07-05-at-1.35.25-am

If you’ve done anything with optimizing the speed of websites, you know that caching is a must. It basically means you’re creating a page once, and then using it multiple times—as opposed to having to create it every time someone asks for it.

So caching is good, is what I’m saying.

Caching 2.0

There’s a caching superpower that you can employ that most people aren’t aware of: caching to memory instead of to disk.

Whether you’re using a caching plugin, or doing opcode caching, or using fast-cgi caching (or all three), you should be writing your cache files to a directory that lives in tempfs.

What’s tempfs

tempfs is a filesystem in memory. Most modern Linux distros have it on by default, actually:

  • In CentOS your default tempfs directory is /dev/shm

  • In Ubuntu your default tempfs directory is /var/run

So, whatever you’re using to create cache files, make use of your Linux distro’s built-in memory-based file system.

supporting = loving

Since 1999 I've been creating ad-free technical tutorials and essays here. It's a one-person effort that's also my life and livelihood. If it makes your day more livable in any way, please consider supporting the work with a monthly or one-time donation. Your support means a lot to me, and makes all the difference. 🫶🏼