Speed Up Linux: Optimize I/O by Moving ‘Hot’ Data to RAM with tmpfs

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

When Even NVMe SSDs Aren’t Enough

You check the dashboard: CPU load is low, you’ve got tens of gigabytes of free RAM, yet the application is still sluggish? If the top command shows %wa (I/O Wait) regularly exceeding 10–15%, your system is bottlenecked at the disk.

In practice, even the fastest NVMe SSDs have a latency of around 10–100 microseconds, while RAM typically clocks in at just a few dozen to 100 nanoseconds. That means RAM is anywhere from 100 to 1,000 times faster than disk. For workloads that constantly write temporary data, forcing the disk to handle that work is a massive waste of resources.

Years ago, while managing a CentOS 7 server cluster handling thousands of sessions per second, I constantly wrestled with disk I/O hitting the ceiling. The solution turned out to be surprisingly simple yet highly effective: tmpfs. Instead of letting PHP write sessions to disk, I pushed them straight into RAM.

tmpfs: Flexible Virtual Memory Storage

tmpfs is a filesystem that stores data directly in virtual memory. It doesn’t pre-allocate a fixed amount of space upfront. If you mount 2GB but only use 100MB, the system only consumes exactly 100MB of actual RAM.

A common source of confusion is mixing up tmpfs with ramfs. The key difference: ramfs will keep growing indefinitely until it crashes the system (kernel panic) if you write too much data. tmpfs, on the other hand, lets you set a size limit and can spill data to the swap partition when physical RAM runs low — making it far safer for production environments.

Configure tmpfs in 30 Seconds

Every major Linux distribution — from Ubuntu and Debian to RHEL/CentOS — has tmpfs built into the kernel. No additional packages required.

1. Quick Mount for Testing

Say you need 1GB of space to process temporary files — just run:

sudo mkdir -p /mnt/ramdisk
sudo mount -t tmpfs -o size=1G tmpfs /mnt/ramdisk

Verify the result with df -h. You’ll see a new mount point with blazing-fast access speeds.

2. Persistent Configuration via /etc/fstab

To have the directory auto-mount after a reboot (critical for Nginx cache or PHP sessions), edit the system configuration file:

sudo nano /etc/fstab

Append this line to the end of the file:

tmpfs   /mnt/ramdisk   tmpfs   rw,nodev,nosuid,size=2G   0   0

Security parameter breakdown:

  • nodev: Prevents creation of device files on the partition.
  • nosuid: Blocks execution of SUID-bit files, reducing privilege escalation risk.
  • size=2G: The hard cap. You can also use size=50% for a flexible limit relative to total server RAM.

Run sudo mount -a to apply the changes immediately without a reboot.

Real-World Use Cases: Numbers That Speak

Don’t use tmpfs just because it “sounds fast.” Apply it precisely to these performance hotspots.

Accelerating Your Web Server (Nginx/PHP)

By default, PHP stores sessions in /var/lib/php/sessions. With 1,000 concurrent users, your disk is hammered with thousands of tiny read/write operations every second. Moving this directory to tmpfs can reduce your Time to First Byte (TTFB) from 200ms down to under 50ms.

# Add to /etc/fstab
tmpfs   /var/lib/php/sessions   tmpfs   rw,nodev,nosuid,size=512M   0   0

Protecting SSD Lifespan During Media Processing

If you use FFmpeg to convert video or ImageMagick to resize images, intermediate temporary files will wear down your SSD surprisingly fast. Point the temporary output directory to RAM instead:

import subprocess

# Directory already mounted as tmpfs
temp_path = "/mnt/ramdisk/output.mp4"
subprocess.run(["ffmpeg", "-i", "input.mov", temp_path])

Monitoring and Hard-Learned Lessons

tmpfs is a joy to work with, but complacency will cost you. When the RAM partition fills up, applications will immediately throw a “No space left on device” error and crash.

Monitor usage regularly with:

watch -n 5 df -h /mnt/ramdisk

3 rules you must never forget:

  1. Data WILL be lost: On power loss or reboot, everything in tmpfs is gone. Never store databases or critical source code here.
  2. Avoid OOM situations: Don’t allocate more than 50% of RAM to tmpfs if the server is running other services. If RAM runs dry, the kernel will activate the OOM Killer and start terminating processes like MySQL or Nginx.
  3. Set correct permissions: If using for /tmp, set the sticky bit (1777) for security: chmod 1777 /mnt/ramdisk.

Leveraging tmpfs is the cheapest way to boost server performance without spending a dime on new hardware. Happy optimizing!

Share: