/ Guides

Loading a website from RAM

Nowadays the loading time of your website is key, reducing loading times by as little as 1 second can increase your conversion rate on an e-commerce site by as much as 7% (https://blog.kissmetrics.com/loading-time/) and on a blog or information site then every second counts if you are to keep the bounce rate down.

There are lots of things one can do to reduce the loading times of your website, but one of the coolest tricks you can do is to load your website content from a RAM disk. This website is loaded from a RAM disk and combined with all the other tricks of the trade it loads in under 700ms according to Pingdom.

What is a RAM disk?

A RAM disk is quite simply a disk partition loaded from RAM, it behaves like any other disk partition in Linux but has the file system type of tmpfs.

The advantage of a RAM disk is that because it is in RAM it is incredibly fast, even faster than an SSD would be. This speed means that when a file is requested by a website visitor those files are retrieved by the server and sent out the network port much much faster, none of the seek time to locate the data being requested that is associated with a regular hard drive, or even an SSD, RAM is even faster still, approximately 4-6 times faster than even the fastest SSD.

The disadvantage of a RAM disk is that RAM is volatile, so if the server looses power or is rebooted then the contents of your RAM disk will be gone. But this can be worked around.

Sounds rather risky!

If having your entire website erased every time the website reboots sounds risky to you then you would be right, it is risky. In the case of a dynamic website that has constantly changing files it could pose very risky as you risk a file being uploaded and lost in a reboot whilst the related database entries have been saved (Because the database isn’t in your RAM disk so is safe through a reboot), and so there is a dissonance between the database and the file system, this is probably a bad thing.

The only way to work around this would be to have the server synchronously write changes to the RAM disk and another hard disk location simultaneously so in the case of a reboot the data is safe. Something I am not going to cover in this blog, maybe one for the future.

Static HTML and RAM disks, a match made in heaven.

The ideal scenario and perfect use case for a RAM disk served website is a static website that can be copied from it’s regular storage location into the RAM disk on server boot up and on changes.

This website for example is static HTML generated by Jekyll, every time a change is made the site rebuilt into a directory and can then be copied into the RAM disk for the web server to serve up to the world. The advantage of this setup is that it doesn’t matter if the RAM disk contents are lost as the latest copy of the site is always stored on the main hard disk.

Create a RAM disk

To get started the first thing we need to do is create a directory to mount the RAM disk into and serve website content from.

mkdir /srv/www/kieromi.uk

Next, create a the RAM disk itself and mount it to the directory we just created.

mount -t tmpfs -o size=256M tmpfs /srv/www/kieromi.uk/

Obviously the size value should be adjusted for the amount of disk space your website requires, and the mount location adjusted for where you intend to serve your website from.

Setup Nginx to serve content from the RAM disk

Now there is a RAM disk, setup a server definition in Nginx (or your preferred web server) to serve content from that location.

server {
    listen 443 ssl http2;
    listen [2a00:b980:2:3::fe2c:a32]:443 ssl http2;
    server_name  kieromi.uk www.kieromi.uk;
    root         /srv/www/kieromi.uk;
    index index.html index.htm;
    charset utf-8;

That is just a sample and partial config for serving via HTTPS that is used for this website, the actual config will vary as per your requirements but this basic config is all that is required. Setup a server definition in Nginx and set the root location to the previously created RAM disk mount point.

Copy your website files into the RAM disk and restart Nginx

service nginx restart

And that’s it, you’re done. Your website should now be loading from RAM.

Creating the RAM disk on boot

Using the commands above will create the RAM disk only once, if the server is rebooted you need to re-run the mount command before re-copying the website files to the RAM disk. Add the following line to /etc/fstab to create the RAM disk on boot, adjusting the values according to your needs.

tmpfs       /srv/www/kieromi.uk tmpfs   nodev,nosuid,noexec,nodiratime,size=256M   0 0

Now the RAM disk is created on boot we should add a quick and dirty startup script to copy the website files to the RAM disk on startup, add the following line at the bottom of /etc/rc.d/rc.local

cp -r /home/kieran/jekyll/kieromi.uk/_site/* /srv/www/kieromi.uk

Naturally adjust that command with the correct source and destination paths as you created earlier.

That’s all folks!

And that’s all there is to it, one RAM disk created so that your website is now served from RAM at super speed so your website visitors can get to the content faster, and with a bit of luck stick around longer.