Linux: In memory filesystems tmpfs vs ramdisk

Although reading and writing to files is fast in Linux with aggressive readahead and caching, it can still slow down applications that make extensive use of ondisk temporary files. As an example MySQL which can do a lot of on disk temporary tables if the temporary tables need to have a large varchar, text or binary column.
It makes sense to mount an in-memory filesystem on the MySQL’s tmpdir usually /tmp to ensure that your on-disk temporary tables are rapidly written to and read from memory to return query output fast by avoiding expensive disk IO. Similarly a lot of different web applications can derive a lot of benefit by writing temporary data to an in-memory filesystem as opposed to the disk.
The two choices ramdisk and tmpfs
Linux Kernel loads up 16 Ramdisks of 16 MB each at bootup time. They don’t occupy any memory space at initialization. Ramdisks allocate memory when they are put to use by formatting them as ext2 or some other non-journaling filesystem. ( no not ext3, there is no use of journaling for an filesystem that is transient ) Once allocated the memory can’t be returned from a ramdisk to the operating system. Ramdisks suffer from another limitation that its size can’t be dynamically increased.
tmpfs on the other hand doesn’t need to be formatted as another disk filesystem. Its can be dynamically resized. Un-utilized memory can be used by the operating system. The only downside is that tmpfs can also use Virtual Memory and its contents can be swapped out causing disk IO that we seek to avoid by using tmpfs. However swappiness should be minimized on a mission critical server anyway by tuning the /proc/sys/vm/swappiness value at boot time.

As an example of how to use tmpfs
/bin/mount -t tmpfs -o size=1G,nr_inodes=10k,mode=0775,noatime,nodiratime tmpfs /tmp

to dynamically increase its size
/bin/mount -t tmpfs -o size=2G,nr_inodes=20k,mode=0775,noatime,nodiratime,remount tmpfs /tmp

Tuning the swappiness
/bin/echo “1” > /proc/sys/vm/swappiness

These need to be added to /etc/rc.local to make the settings persistent across reboots.