06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Decentralised & Resilient Systems · Techno Tim · · 9:10 runtime

I Didn't Know Linux Could Cache NFS Files Locally

In the wiki NFSLinuxFS-CachecachefilesdDeepSeek Also namedDeepSeek V4, ext4
Transcript 1751 words

I keep most of my large files on my NAS, especially my AI models.

I've got a handful of machines that use them, and I really don't want to maintain separate copies of everything on each one.

Some models are 100 or 200 gigabytes too. And I've got 10 gig networking on some of these machines, but only one or 2.5 on the others.

And when I'm testing, I'll make a change and reload the same model over and over again, and reading that much data takes a lot of time.

Now, sure, I could put the models on local SSDs, but then I've got copies to manage,

and I can't keep every model on every machine, and that's why I have a NAS to begin with.

So I'd rather keep the files centralized and still get local speeds for repeat reads.

So I started looking into caching, and I found something in Linux I didn't know existed.

It's FS-Cache, and I wanted to see if it could help. There are three pieces: FS-Cache is the kernel framework,

CacheFiles stores the data on the SSDs, and cachefilesd registers the cache and helps make room when it fills up.

The original files stay on my NAS, and when Linux needs data that isn't cached, it reads it over NFS and can keep a copy. Then on later reads, it can use the SSD instead if that copy is still valid.

I can enable this for the entire NFS mount too, and each machine caches only what it reads without copying the whole NAS share locally. Pretty awesome. Let me show you how this works. So first I'm going to SSH into one of my machines so it's the client that's going to connect to my

NAS. Then I'm installing some dependencies, you might already have some of these installed,

but I'm installing the NFS client some caching tools. And then after that, I'm checking that

the local SSD has room for the 256 gig cache that we're going to create here in a second.

And you can see right down here, I have enough room for our cache. So with this command,

I'm going to mount my share at /mnt/llm without caching first.

And it's read-only and I'll keep the other NFS options the same when we add fsc and caching

later. Now my LLM share is mounted and if I ls that folder at /mnt/llm/models/huggingface, you

could see all my models. So now we're connected to the NAS without caching. So one of the models that I've been testing a lot lately and takes a really long time to

load because it's pretty big is DeepSeek V4. And if we list out that folder and see all of the files, which are shards, we can see

that it's 155 gigabytes over 74 files. So it's pretty big and not only big, there's multiple files there.

That's a lot of files and a lot to transfer. So let's start small first. Let's pick one of the shards that's about 3.3 gigabytes.

Oh, and everything that I'm running, you'll find on my documentation site.

And you can find that in the description below. But this first command is just going to go through and read out one of those files.

So reading this file directly over the network without cache took about four and a half seconds.

Doesn't seem like a lot until you have lots of these files or lots of big files.

So now let's enable caching for the share. Next we need a place on disk to cache the files. Now I don't want an unlimited cache filling up this machine's drive so I'm going to reserve

256 gigabytes for it. And I can do that with a single file on local SSD and put an ext4 filesystem inside of it.

And that gives CacheFiles its own space without repartitioning anything.

So we'll create /var/cache/fscache-demo.img and we'll allocate the full size up front.

Then we'll format that new image file only. And then we'll mount it at /var/cache/fscache-demo.

Then we're going to reserve that disk space on the host system.

Then I'm going to create my CacheFiles config. Here are the settings that I use. Now all of these B settings control how much disk space we have left.

If it drops below 10%, CacheFiles starts to do something called culling.

Now that's really just cleaning up old cache file copies to make room.

And it keeps doing that until we're back above 25% free.

If it gets all the way down to 3%, it stops adding new data to the cache until there's

room again. So when my cache is full, how does it know which ones to clear out first?

Well it uses an LRU or least recently used.

And the idea behind that is that data I haven't accessed in a while is the first to go and

data that I've used recently is more likely to stay. And there's no timer or anything like that the cache could live indefinitely.

And so the F settings work the same way but they protect the number of available file entries

instead of disk space. So this matters more when you're caching lots of small files.

So that config doesn't do anything until we start cachefilesd and that's what we're

going to do here with this service. Now we can see that the service is running and we can see that /var/cache/fscache-demo has

251 gigabytes available to it. So the cache is registered, but the NFS mount on this client isn't using it yet.

So first we're going to unmount our old share and then we're going to remount it with this

fsc option, which is our FS-Cache, our Linux file cache option for this mount.

So let's do that. Now that we've remounted that share with a caching option, what we want to do is read

that file back. And the first read is going to be a cold cache because we're going to reread it over the

network and FS-Cache is going to cache it.

And the second one is then going to try to read it from the network again, and we'll see

if we get any improvements. Awesome. So you can see on the cold cache read, it took about four and a half seconds again.

once that file was then in the cache and we tried to read it again over NFS, the second time took

1.7 seconds, so half the time. It's pretty awesome. So the second read was two and a half times faster

than the first. And you might be thinking, wow, we saved two seconds. No big deal. Well, that might

be true on one file. But as you remember, DeepSeek was many files, I think 74, and it was up to 155

gigabytes that we needed to read. So this is amplified the more files you have and the bigger

they are. So let's actually test that out. I'm going to run this script really quick that's going

to unmount that mount, clear out some cache, and then mount that mount again without the caching

option. So let's run this really quick. Now it's ready for a cold read of that entire model and all

of its shards. Okay, so this is going to do the cold read, and then it's going to do the warm read,

and it's going to print those both out. It's going to take a long time too. As you can see,

it's taking a while. We're at 30 of 74 files and this is reading them over the network. But at the

same time, it's also copying them to the cache. So it's warming that cache up. You could see in

btop on that machine, there's actually network activity. So it's reading that file over the

network. And we could see here my FS-Cache, which actually has 155 gigabytes in it. So that's

DeepSeek itself. All right, there was cold cache, 565 seconds. So that's what, about eight, nine,

nine, a little over nine minutes. There it goes. So it read all of the files now in 93 seconds.

So about a minute and a half. So we're what, about nine, nine and a half minutes on the cold read.

And then once we have it cached, it only took 93 seconds. So pretty, pretty awesome improvement.

So before I added FS-Cache on some of my machines, while I was testing DeepSeek, I was paying most of that 565 seconds there.

Now, it's not truly reading from cold every single time you read a file, even without cache, because some of the blocks get stored into memory, into RAM.

But if I didn't have that cached in RAM, it would be 565 seconds.

And now if we read it again, it's probably going to go a little bit faster because things are cached in RAM.

So if you're testing this, you might want to flush out the RAM. I have a command in the documentation where you can do that, where you can test and play with this too.

So pretty awesome. I'm definitely going to keep this around. And that's why I wanted it for AI models. When I'm reloading them, I'd rather not transfer hundreds of gigabytes every time.

Now, if you don't use models, I can see this helping with shared datasets, installers, or even Linux ISOs that you read more than once.

Now, one thing to keep in mind is that this is a read cache.

It's useful when you're reading the same files over and over, but it won't speed up writes,

and there's not much benefit if you're only reading something once.

If you're looking to cache application data, something like Redis or Valkey might make more sense.

And for caching local storage at the block level, there are tools like bcache or dm-cache

and a whole bunch of others that I've used before. But these all solve different problems. But for me, FS-Cache fits because the files that I want to cache are on NFS and

I read them over and over. Now I'm so glad I stumbled on this feature in Linux and I've had some time to check it

out. If there are others like this that you know about, let me know in the comments. I'd

love to hear about them. Well, I hope you enjoyed this video. I'm Tim. Thanks for watching.

Related videos

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's the Front Page for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 537 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks