logo

Linux - tmpfs

What is tmpfs?

Normally, tmpfs (Temporary File System) is a Linux filesystem that lives entirely in Virtual Memory. It is incredibly fast because it operates at the speed of RAM. However, it has two major traits:

  1. Volatility: If the power goes out or you reboot, everything in tmpfs is deleted.
  2. Dynamic Size: It only takes up as much RAM as the files inside it actually use.

What is "Disk-Backed" tmpfs?

The term Disk-backed tmpfs refers to a configuration where the speed and behavior of tmpfs are combined with the capacity (and sometimes the persistence) of a physical disk (SSD/NVMe).

There are a few ways this is implemented, depending on the goal:

1. The "Default" way: Swap Space

Technically, all standard tmpfs mounts are "disk-backed" by default via the Linux Swap mechanism.

  • Because tmpfs lives in "Virtual Memory," if your RAM fills up, the Linux kernel will automatically move "cold" (infrequently used) files from the tmpfs in RAM onto your Swap partition on the hard drive.
  • The benefit: You can store more in /tmp than you have physical RAM.
  • The downside: Once it hits the disk (swap), performance drops significantly.

2. The "Cloud/Container" way: Local SSDs

In environments like Kubernetes or AWS, you might see "disk-backed tmpfs" used to describe EmptyDir volumes or Instance Stores.

  • In this setup, the OS creates a filesystem that acts like tmpfs (it's wiped when the container/instance stops), but it is physically mapped to a high-speed local NVMe drive rather than RAM.
  • Why? If you have a process that generates 500GB of temporary logs or scratch data, you can't fit that in RAM. You use a "disk-backed" temporary store to get high capacity while maintaining the "scratchpad" behavior.

3. The "Performance" way: zram with Writeback

Modern Linux systems (and Android) often use zram. This is a compressed block device in RAM.

  • "Disk-backed" zram: You can configure zram so that it compresses data in RAM, but if the data is incompressible or the RAM is truly full, it "writes back" the data to a physical disk.
  • This gives you a "tiered" approach: RAM -> Compressed RAM -> Disk.

4. The "Persistent Memory" way (PMEM/DAX)

Using hardware like Intel Optane (Persistent Memory), you can create a filesystem that is as fast as RAM but doesn't disappear when the power shuts off.

  • Using a mode called DAX (Direct Access), the OS can mount this "disk" in a way that the CPU treats it exactly like tmpfs, but the data is physically stored on the persistent hardware.

Why would you use it?

Feature Standard tmpfs (RAM) Disk-Backed tmpfs
Speed Extreme (RAM speed) High (SSD speed)
Capacity Limited by RAM size Limited by Disk size
Cost Expensive (RAM is pricey) Cheap (SSD/NVMe is cheap)
Persistence Data lost on reboot Usually lost (unless using PMEM)

Use Case Example:

Video Rendering or Large Compilation: If you are compiling a massive software project (like the Linux Kernel), you want it to be fast. Using tmpfs is best. But if the source code and temporary object files are 64GB and you only have 32GB of RAM, a standard tmpfs will crash your system.

A disk-backed tmpfs (via a large swap file or a dedicated NVMe scratch disk) allows the process to finish by "overflowing" onto the disk, providing the best possible speed without running out of space.

tmpfs vs ramdisk vs ramfs

While all three store data in RAM, they differ significantly in how they manage memory, how they handle size limits, and how they interact with the rest of the Linux kernel.

1. Ramdisk (The "Old" Way)

A ramdisk is a fixed-size chunk of RAM that the kernel treats as a block device (like a physical hard drive).

  • How it works: You create a device (e.g., /dev/ram0), format it with a filesystem (like ext4), and then mount it.
  • Size: Fixed. If you create a 1GB ramdisk, it takes up 1GB of RAM immediately, even if it's empty.
  • The "Double Caching" Problem: This is the biggest downside. Because the kernel thinks it's a physical disk, it uses the Page Cache to "buffer" reads and writes to it. This means the same data exists in RAM twice: once in the ramdisk and once in the cache.
  • Status: Mostly obsolete.

2. Ramfs (The "Simple" Way)

ramfs was created to solve the double-caching problem of ramdisk. It is a very simple filesystem that exports the kernel's existing caching infrastructure as a mountable storage area.

  • How it works: It stores files directly in the kernel's page cache. There is no "block device" underneath.
  • Size: Dynamically growing. It only uses as much RAM as the files inside it.
  • The "Danger" Factor: ramfs does not have a size limit. If a process starts writing to a ramfs mount, it will continue to grow until it consumes 100% of the system's RAM and causes a kernel panic or triggers the OOM (Out of Memory) killer. Only the "root" user should ever have access to ramfs.
  • Swapping: ramfs cannot be swapped to disk.

3. Tmpfs (The "Modern/Smart" Way)

tmpfs is the successor to ramfs. It is the standard for modern Linux systems (used for /tmp, /run, and /dev/shm). It is essentially ramfs with "smarts."

  • Size Limits: Unlike ramfs, tmpfs allows you to set a maximum size (e.g., mount -t tmpfs -o size=512M tmpfs /mnt). If you try to write more than the limit, you get a "Disk full" error, protecting the system from crashing.
  • Dynamic: Like ramfs, it only uses the RAM it actually needs. If the mount limit is 10GB but you only have 1MB of files, it only uses 1MB of RAM.
  • Swappable: This is a key difference. tmpfs lives in Virtual Memory. If your system runs low on physical RAM, the kernel can move "cold" files from tmpfs into your Swap space on the hard drive/SSD.
  • Efficiency: It does not suffer from the "double caching" of ramdisk.

Summary Comparison Table

Feature Ramdisk Ramfs Tmpfs
Type Block Device Filesystem Filesystem
Size Fixed (Pre-allocated) Dynamic (No limit) Dynamic (Limit-aware)
Double Caching Yes (Inefficient) No No
Swappable? Yes No Yes
Risk Low (Fixed size) High (Can crash OS) Low (Respects limits)
Modern Use Minimal Special kernel tasks Standard (/tmp, /run)

Which one should you use?

In 99% of cases, you should use tmpfs. It gives you the speed of RAM with the safety of size limits and the flexibility of swapping.

ramfs is only used by kernel developers for very early boot processes (like initramfs) where they know exactly how much data is being handled and don't want the overhead of the VM subsystem.