Cadabby: Content-Aware Mirroring | nsf.name

nsf.name

Cadabby: Content-Aware Mirroring

(Approx. 5 min read)

An attempt to do something useful with Rust in class.


Note: this project is still in progress, I’ll update this when I finish it.

This semester I have been taking a class on filesystems, and for the final project I realized that it would be somewhat fun to think about file archiving. Some of this was driven by the fact that I’ve found casync useful, but also because I found myself downloading lots of data from ESGF and, while waiting around for the slow downloads to finish, kept thinking of ways that I could make downloading hundreds of gigabytes of easily-compressible data simple and efficient.

cadabby1 is an attempt to do this, and something I plan to keep working on after this course is over. Its job is to make downloading big data from mirrors simpler: you should be able to request files over a network, get the minimum number of unique blocks of data in a compressed wire format, and then reconstruct the thing on your client, in parallel.

Content-Aware Chunking

Let’s start with the idea of content-awareness. We can abstractly think of a file as a big bag of [&u8]. How do we tell which parts are likely going to repeat across files? Fortunately, many content-defined chunking algorithms exist. I will use FastCDC because it is streaming, already implemented in Rust, and isn’t nondetermistic like RapidCDC or QuickCDC, which go even faster at the expense of skipping chunks, potentially missing data.

So now our [&u8] is now defined in terms of many chunk offsets and lengths. When we repeat this over many files and take the hashes of these chunks, we should eventually see (ideally lots of) collisions where files share the same chunks. We then only need to store each unique chunk exactly one time, saving on space compared to the original files.

There are other benefits to this: chunks are small, so it’s easy to parallelize jobs like compressing and hashing them. Because of how content-defined chunking works, it’s likely that chunks will have some form of internal structure, making compression more efficient (provided we check the Shannon entropy of the chunk first to see if it’s even compressible).

Mirroring Content

Right now, mirrors are one of the most ubiquitous yet inefficient services on the Internet. You go to a mirror, download some content over HTTP, which downloads every byte of the source file, usually wrapped in some archive format. This is fine for small files, but when you reach multi-gigabyte downloads, there’s often enough redundancy in what you’re downloading that standard gzip or xz is leaving substantial storage benefits on the table that could be gained with content-awareness. This is especially true of the types of highly-repetitive data that exists in stuff like climate datasets or LLM model weights2!

You might reasonably ask if zstd or other newer compression algorithms solve this. They are definitely better in this regard3, but compression algorithms that are lossless always fail on at least some data and bloat the size of the thing they are compressing due to the pigeonhole principle. Content-aware chunking and compression are orthogonal axes of improvement: you can use chunks to make compression smarter, and compression works better with well-defined chunks.

cadabby’s Design

cadabby takes an interesting approach to solve the problem. The rough flow of the application looks like this:

  • Walk the directory the user passed in and, using an index tree, build up a succinct in-memory representation of our file tree.
  • Then, filter this tree down to get all files in our tree.
  • Iterate through this list in parallel for each file:
    • Read the file linearly and use FastCDC to chunk the file.
    • Use mmap() to load select parts of the file into memory and run BLAKE3 on chunks to compute hashes. This takes advantage of the fact that we likely already have parts of the file in warm cache, so this could potentially do no disk I/O, making this random seeking much cheaper.
    • Check the file extension, the file’s magic bytes, and then the Shannon entropy to determine if the file is likely to be incompressible or not.
    • Do a comparison against a global hash table of chunk hashes to determine which chunks are unique; we can delete anything which we know isn’t unique.
    • Compress all remaining chunk data, checking to see if space is saved; if it isn’t, throw away the compressed result and use the original data.
  • Finally, serialize out all the chunks to disk with our directory tree.

The idea is that our in-memory state is roughly the same as that which we’ll put on disk, which lets us use serde to conveniently serialize out our data structures. This whole design is one of those things where I’ll only know if it’s more efficient once I’ve actually built the whole thing; if this doesn’t prove to be more efficient, I think the result will still be interesting regardless.

Of course, this is also only the design at present. The whole structure of cadabby will make it easy to transition to letting users make partial requests for the tree over a network, since that’s just the same process except repeated over a subtree of our main tree. I plan to think more about the networking and daemon-chunk-serve part of this once the course is over, since that’s a bit out of scope at the moment.


  1. Named after the Sesame Street character, because kermit exists, so it’s fitting. ↩︎

  2. I suspect the real reason OpenAI agents broke into HuggingFace was so they could get a faster download of the model weights they wanted; even LLMs grow tired of how slow their CDN is. ↩︎

  3. There is an attempt to solve this at Meta called OpenZL and it’s very cool, but requires writing custom-tooled compressors per data type. This is great when you’re Meta and can throw infinite engineers at writing exactly the ones you need, but for general-purpose usecases OpenZL is inferior to zstd, which works with anything out of the box. ↩︎

<< Previous Post

|

Next Post >>

#Programming #Rust