Software RAM disks use the normal RAM in main memory as if it were a partition on a hard drive rather than actually accessing the data bus normally used for secondary storage such as hard disk. How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?
You can create the ram disk as follows (8192 = 8M, no need to format the ramdisk as a journaling file system) :
# mkfs -q /dev/ram1 8192
# mkdir -p /ramcache
# mount /dev/ram1 /ramcache
# df -H | grep ramcache
Sample outputs:
/dev/ram1 8.2M 1.1M 6.7M 15% /ramcache
Next you copy images or caching objects to /ramcache
# cp /var/www/html/images/*.jpg /ramcache
Now you can edit Apache or squid reverse proxy to use /ramcache to map to images.example.com:
<VirtualHost 1.2.3.4:80> ServerAdmin admin@example.com ServerName images.example.com DocumentRoot /ramcache #ErrorLog /var/logs/httpd/images.example.com_error.log #CustomLog /var/logs/httpd/images.example.com_access.log combined </VirtualHost>
Reload httpd:
# service httpd reload
Now all hits to images.example.com will be served from the ram. This can improve the speed of loading pages or images. However, if server rebooted all data will be lost. So you may want to write /etc/init.d/ script to copy back files to /ramcache. Create a script called initramcache.sh:
#!/bin/sh mkfs -t ext2 -q /dev/ram1 8192 [ ! -d /ramcache ] && mkdir -p /ramcache mount /dev/ram1 /ramcache /bin/cp /var/www/html/images/*.jpg /ramcache
Call it from /etc/rc.local or create softlink in /etc/rc3.d/
# chmod +x /path/to/initramcache.sh
# echo '/path/to/initramcache.sh' >> /etc/rc.local
A Note About tmpfs
tmpfs is supported by the Linux kernel from version 2.4+. tmpfs (also known as shmfs) is a little different from the Linux ramdisk. It allocate memory dynamically and by allowing less-used pages to be moved onto swap space. ramfs, in contrast, does not make use of swap which can be an advantage or disadvantage in many cases. See how to use tmpfs under Linux.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 23 comments… read them below or add one }
This is very interesting. I am going to show this to one of my colleuges in regards to this benifiting us.
Great Tutorial.
jaysunn
/dev/ram1 seem not exist on Suse based systems:
linux-jgbr:/home/dpanasiuk # ls /dev/ra*
/dev/random
linux-jgbr:/home/dpanasiuk # cat /etc/issue
Welcome to openSUSE 11.2 “Emerald” – Kernel \r (\l).
linux-jgbr:/home/dpanasiuk #
@Dariusz
Try
Let me know…
How to unmount RAM ??
It works in SuSE 11.4
Could you please compare the speed with serving the files from disk? I believe this is completely useless – as the RAM disk will only steal memory which would otherwise be used as file system cache, which will be quiet as efficient as serving the files directly from RAM!
@Swen, Why not run ab against such a setup? I guess you trust kernel too much.
On a related note, you can use ramdisk to improve privacy too.
why use a journaled filesystem for the ramdisk?
@Diggity, ignore -t ext3 but you are right on spot.
@ Swen, let face it disk i/o going to be slow as compare to your ram. Not all projects are same, so it may or may not produce results at the end of day. YMMV.
@Vivek Gite
Your instructions for Suse (and I suppose for any other Linux which doesn’t have /dev/ram1 created by default) worked.
dpanasiuk@linux-jgbr:~> uname -a
Linux linux-jgbr 2.6.31.12-0.1-default #1 SMP 2010-01-27 08:20:11 +0100 i686 i686 i386 GNU/Linux
dpanasiuk@linux-jgbr:~> df -H | grep ram
/dev/ram1 8.2M 1.1M 6.7M 15% /ramcache
dpanasiuk@linux-jgbr:~>
cheers
Super post.
On Debian, I tryed to do this :
mkfs -q /dev/ram1 97468
(arround 96M).
I guess this alert :
# mkfs -q /dev/ram1 97468
mkfs.ext2: Filesystem larger than apparent device size.
Proceed anyway? (y,n)
I don’t know if it’s really dangerous.
Pass ramdisk_size=N option to linux kernel. You need to edit grub.conf and pass that option. Once done reboot the system. This command is added to the kernel line in grub. N must be in K size. See
http://www.cyberciti.biz/files/linux-kernel/Documentation/ramdisk.txt
Nice Post as usual :)
if we unmount the ram disk, will it still reserve RAM or release it for system’s normal use ?
@ Tapas,
Short answer – No it will not release it.
Long answer – once memory has been allocated to the ramdisk, it can not be freed until you reboot the system. It will get flagged so that the Linux kernel will not try to reuse the memory again. Therefore, you cannot reclaim the RAM after you are done with using the ramdisk. If you remount the ramdisk, your data will still be there.
HTH
This rocks! Thanks for sharing : )
# mkfs -q /dev/ram1 8192
# hdparm -tT /dev/{hda,ram1}
/dev/hda:
Timing cached reads: 1512 MB in 2.02 seconds = 750.01 MB/sec
Timing buffered disk reads: 154 MB in 3.03 seconds = 50.76 MB/sec
/dev/ram1:
Timing cached reads: 1350 MB in 2.00 seconds = 674.96 MB/sec
Timing buffered disk reads: 16 MB in 0.03 seconds = 561.60 MB/sec
to use memory as storage and still allow the kernel to reuse the memory, you can use ramfs or the newer tmpfs
just mount a directory
mount -t tmpfs tmpfs /dir
if you don’t fill up the space in the tmpfs, the kernel gets to use the free RAM for buffers and cache. If you delete a file, the kernel will reclaim the ram, and if you umount the directory, all data is lost
The /dev/ram method didn’t work for me, even after running mknod. I probably deselected a necessary kernel option. But the tmpfs method is working great so far, thanks :)
“How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?”
Linux is a kernel, commonly used together with the GNU operating system instead of Hurd. In that case the OS is GNU with Linux or GNU/Linux.
Thanks for Your article, it sounds really neat and I will try & test performance boost from Your solution soon.
I’m thinking of putting my entire wordpress directory on ramdisk (and in the future also database files). For the wordpress files part it seems quite easy to do – entire directory would be on ramdisk with rsync syncing it with backup folder on harddisk (lets say in 30 minutes cycles).
Or maybe there is a chance of setting apache to write to one location (harddisk) and serve files from another one (ramdisk which would be rsync’ed from harddrive in that case)?
But how to manage syncing of database files form ram to local filesystem? As far as I remember I shouldn’t rsync db files when database server is running (or maybe mysqlhotcopy would do the job?)… how to cope with unexpected power-downs – I can create shutdown script syncing database after shutting down database server, but it will not manage any restarts from hardware/power failures in that way.
Any advice would be welcome.
How can I erase the filesystem in /dev/ram1?
Ramdisk/tmpfs is kind of last resort. It’s almost never needed. Linux kernel uses all the free RAM for file cache by default, so all the frequently accessed files remain cached in RAM. Short-lived temporary files may stay in cache and not hit the disk at all. Linux kernel is smart. :)
There’re just a few cases when ramdisk/tmpfs is much faster than real filesystem (and most of them can be reported as a bug). Usually there’re better alternatives.
For example to reduce number of disk write attempts you can increase vm.dirty_* sysctls (http://www.kernel.org/doc/Documentation/sysctl/vm.txt). If you’re trying to reduce number of HDD spinups for a laptop, then vm.laptop_mode and laptop-mode-tools package would give you the best solution. If your software uses a lot of slow fsync’s that you need to avoid, then `eatmydata` wrapper is just for you.
Ramdisk takes your RAM from other apps even when not used, and often it’s not easy to keep it in sync with your local data. The only way to make sure that you need it is to benchmark it, not with some general benchmarking tool, but with your particular test case.