Everyone knows that RAM is so much faster than a hard disk. To illustrate, while a current SATA disk has peak transfer rates of 375 MB/s, current RAM can do a mind blowing 12,500 MB/s! Normally only the system itself makes use of this ultra fast storage, but we can also access this space directly. And that opens a great window of opportunity.

Possible Uses

There is an unlimited number of uses for this technology, but here are 3 from my own experience:

  • What if you're running a blog, so successful that your server can't handle it. Although your blogging software caches plain html files in the /cache dir to speed up processing, it still doesn't give you enough performance.
  • What if you're an internet host and you want to show off your massive bandwidth by having users download a dummy .bin file of 100MB. You'll find that if many users access this file at the same time, your hard drive becomes the slow factor and you're running into disk IO problems.
  • What if you're running a PXE server with an ISO stored on it, and an entire webcluster is accessing this file for installation. Again. Your hard drive will not be able to cope with these kinds of speeds.

If only you could store these files in memory..

How Does It Work?

Everybody who's running a linux server must have seen the /dev/shm on their system.

This not a normal directory on your machine. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device. The standard /dev/shm grows automatically as more space is needed, but is by default limited to half of your physical RAM. If you have 2GB, it can grow to 1GB at most.

So everything you copy to that place is in fact stored in your RAM. And that's cool because your RAM is about 33 times faster than your normal filesystem!

Let's do this

In this article I presume you have basic knowledge of server administration, be carefull because you could really mess things up if you've got no clue what you're doing. I warned you!

Use Current Volume

As told before, you probably already have a /dev/shm on your linux system. So just copy a file to it:

$ cp -af /root/100mb.bin /dev/shm/

And now it's in your RAM! Using this file will be 30 times faster than before!

Enlarge Current Volume

But maybe the limit of half of your physical RAM just does not cut it for you. Then you might want to increase the maximum size of this volume to 4GB:

$ mount -o remount,size=4G /dev/shm

Create a New Volume

Another possibility is to create a brand new memory device. We can do this with the filesystem type: tmpfs. Let's say you want to create a tmpfs instance on /var/www/www.mysite.com/ramdrive that can allocate a max of 500MB RAM and that can only be changed by root, but accessed by everyone (like Apache):

$ mkdir -p /var/www/www.mysite.com/ramdrive
$ mount -t tmpfs -o size=500M,mode=0744 tmpfs /var/www/www.mysite.com/ramdrive

Restore That Volume Everytime Your Server Boots

Easy, just make sure the directory exists, and add the following line to your /etc/fstab:

tmpfs /var/www/www.mysite.com/ramdrive tmpfs size=500M,mode=0777 0 0

This will create a ramdrive of max 500MB in /var/www/www.mysite.com/ramdrive everytime your server boots. The mode 0777 will give it full access for everybody on your system, so just change that to a suitable umask.



Now as with everything too cool, there is a pitfall:

  • Everything in tmpfs is temporary in the sense that no files will be created on your hard drive. If you reboot, everything in tmpfs will be lost.

So you will need to create a script that automatically restores the files or applications that you need. You can let this script run at boot time, or schedule it as a cronjob.