Design an in-memory file system
medium 85 minDesign directories and files that live in memory: create, read, write, list, move and remove by path.
Watch lesson5:02
Where it shows up
The same pattern appears across everyday software.
tmpfs on Linux
Keeps all files in virtual memory. Unmount it and everything is gone. It can be limited by size and by the number of inodes, the two limits of our page on sizes.
Jimfs for Java
An in-memory file system by Google behind the standard `java.nio.file` interfaces. It can behave like a Unix or a Windows system, which is useful to test path code for both.
memfs for Node.js
The `fs` functions of Node, kept in memory, also for browsers. Tests and bundlers use it to avoid the disk.
Afero for Go
A file system interface with a concurrent-safe `MemMapFs` and layers such as copy on write over a read-only base.
pyfakefs for Python
Replaces the file modules of Python during a test by one that works in memory, so code under test need not change.
Emscripten MEMFS
Programs compiled to WebAssembly expect files. An in-memory file system is mounted at `/` by default.
The Linux VFS
Names and things are separate: directory entries (dentries) live in memory only, as a cache for path lookups, and point at inodes. The walk is the hottest path, so it has its own cache.
Linux directory locking
The kernel documents our problem: a lock for renames in the whole file system, parents locked ancestors first, and a check that neither source nor target is below the other.
Git trees
A tree object lists names with the hash of a file or of another tree. Nodes never change, so versions share everything that is equal.
inotify
Watches directories and reports a rename as two events with a shared cookie. Watching a whole tree needs a watch for each directory.