Press "Enter" to skip to content

Speed up your website with a memory disk (tmpfs)

After migrating the blog to Tencent Cloud and getting SSL set up, I've been tuning PHP's performance. I spent the whole afternoon adjusting fpm and opcache, and in the evening it suddenly occurred to me that I'd once presented at a conference about using tmpfs (treating RAM as a hard disk) to speed up a website — so, let's do it!

An important thing to say three times: before you start, back up your nginx root directory — back it up, back it up, back it up!

This Tencent Cloud machine is running Ubuntu, version:

$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l

First, we create a directory, say /ramdisk, then use the tmpfs command to mount a portion of memory as a disk:

mount -t tmpfs -o size=256M tmpfs /ramdisk

Here I mounted up to 256M of memory onto the /tmp node. Note that the system won't immediately claim all 256M of memory — it uses as much as it actually needs, capped at 256M.

You can use df to confirm the operation succeeded:

$ df -h
Filesystem Size Used Avail Use% Mounted on
......
tmpfs 256M 0 256M 0% /ramdisk

Next we need a piece of magic I only discovered today (before I had to write my own crontab script to do the syncing, <, Anything-sync-daemon. It can automatically sync a specified directory to tmpfs, and can be set to sync on a schedule or at system boot. With this tool, all of this becomes very simple.

First, download asd (Anything-sync-daemon):

wget https://github.com/graysky2/anything-sync-daemon/archive/master.zip

After unzipping, run the install:

make install-systemd-all

(There's a gotcha here: by default it installs the systemd service to /usr/lib/systemd/system, but for some reason on my system systemd couldn't find the service. I fixed it by copying its several service scripts to /lib/systemd/system.)

mv asd-resync.service  asd-resync.timer  asd.service /lib/systemd/system

Then notify systemd:

systemctl daemon-reload

My blog's nginx root is under /home/huixinchen/www/htdocs. We now want this directory to be automatically synced to the tmpfs directory, i.e. /ramdisk. So we edit the asd.conf file and add:

WHATTOSYNC=('/home/huixinchen/www/htdocs/') // directory to sync
VOLATILE="/ramdisk" // tmpfs directory

You can sync multiple files — just list them comma-separated in the WHATTOSYNC array, e.g.:

WHATTOSYNC=('/home/huixinchen/www/htdocs/', '/home/huixinchen/local/xxxx') // directories to sync

Then we set it to sync changes on tmpfs back to disk once a day. Edit /lib/systemd/system/asd-resync.timer,

[Unit]
Description=Timer for Anything-sync-daemon - 1Hour
PartOf=asd-resync.service asd.service
[Timer]
OnUnitActiveSec=24h

Then we check with asd p:

$ asd p
Anything-sync-daemon v5.85 on Ubuntu 18.04.4 LTS

Daemon pid file is not present.
Resync cronjob is not present.
Overlayfs technology is currently inactive.

Asd will manage the following per /etc/asd.conf settings:
owner/group id:     huixinchen/1000
target to manage:   /home/huixinchen/www/htdocs
sync target:        /home/huixinchen/www/.htdocs-backup_asd
tmpfs target:       /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs
dir size:           237M
recovery dirs:      none

asd will sync my
/home/huixinchen/www/htdocs
directory to
/ramdisk/asd-huixinchen/home/huixinchen/www/htdocs,

and write the updated content on tmpfs back (on a schedule) to
/home/huixinchen/www/.htdocs-backup_asd

When we stop the asd service, asd will mv .htdocs-backup_asd back to htdocs, so you don't have to worry about your content being lost due to a sudden power outage on the server.

Now, let's start asd:

service asd start

Now /home/huixinchen/www/htdocs gets copied to tmpfs and symlinked, which means we don't need to modify nginx at all — we just need to restart fpm and reset the opcache:

$ ll
total 0K
lrwxrwxrwx 1 huixinchen huixinchen 50 Feb 15 22:27 htdocs -> /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs/

After restarting fpm, everything works out of the box!

PS: For more information on Asd, see: Anything-sync-daemon

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.