Storing Web Cache on a RAM Disk: Part One

One way to speed up your web server is to cache frequently accessed pages and content. This is much faster because dynamic pages do not have to be recreated every time someone accesses them. Instead, the cached HTML files are loaded at a much faster rate. Using a RAM disk, you can make that caching even faster.

A RAM disk is a virtual drive that is entirely located within the system’s random access memory (RAM). Nowadays, most servers have a ton of RAM, so allocating a small portion of it to cache web files is no big deal. In fact, it may actually cost more memory to dynamically create pages from the database than it does to store compressed HTML files.

On Linux, you can create a RAM disk with a single command, such as this one:

mount -t ramfs none /path/to/directory

Upon reboot, however, this RAM disk will be gone. To make it permanent, you need to add it to /etc/fstab:

webcache /path/to/directory ramfs defaults 0 0

It should be noted that no matter what you do, a RAM disk, even a permanent one, will empty itself upon reboot. That is the nature of RAM. This should not be a problem with a web server, but if you need to keep cached files, even after a reboot, you can configure Linux to store them on disk before shutting down. In Part Two, you will learn how to configure Apache to use your RAM disk.