Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Linux filesystem cache files efficiently?

Tags:

I'm creating a web application running on a Linux server. The application is constantly accessing a 250K file - it loads it in memory, reads it and sends back some info to the user. Since this file is read all the time, my client is suggesting to use something like memcache to cache it to memory, presumably because it will make read operations faster.

However, I'm thinking that the Linux filesystem is probably already caching the file in memory since it's accessed frequently. Is that right? In your opinion, would memcache provide a real improvement? Or is it going to do the same thing that Linux is already doing?

I'm not really familiar with neither Linux nor memcache, so I would really appreciate if someone could clarify this.

like image 901
laurent Avatar asked Aug 19 '11 07:08

laurent


People also ask

Does Linux cache files?

Linux maintains four caches of I/O data: page cache, i-node cache, buffer cache and directory cache. Figure 5 shows these caches and how they interact with the kernel, each other and user level programs. The page cache combines virtual memory and file data. The i-node cache keeps recently accessed file i-nodes.

What is file system cache in Linux?

The file system cache holds data that was recently read from the disk, making it possible for subsequent requests to obtain data from cache rather than having to read it again from the disk.

How does Linux cache work?

The Linux cache approach is called a write-back cache. This means first, the data is written to cache memory and marked as dirty until synchronized to disk. Then, the kernel maintains the internal data structure to optimize which data to evict from the cache when the cache demands any additional space.

What is the fastest filesystem Linux?

On the NVMe SSD, the four-thread FS-Mark was the fastest on XFS followed by Btrfs. JavaScript is required to view these results or log-in to Phoronix Premium. Under Compile Bench, EXT4 was the fastest on all three drives followed by a mix of XFS and F2FS.


1 Answers

Yes, if you do not modify the file each time you open it.

Linux will hold the file's information in copy-on-write pages in memory, and "loading" the file into memory should be very fast (page table swap at worst).

Edit: Though, as cdhowie points out, there is no 'linux filesystem'. However, I believe the relevant code is in linux's memory management, and is therefore independent of the filesystem in question. If you're curious, you can read in the linux source about handling vm_area_struct objects in linux/mm/mmap.c, mainly.

like image 51
Robert Martin Avatar answered Oct 03 '22 04:10

Robert Martin