Research · mica v0.2
mica v0.2: how it works and how fast it is
mica gives a VM or a container a disk whose data lives in an S3 bucket. The disk runs at local SSD speed, and it can move to another machine in a few seconds. This page explains how, and shows what mica measures on a host and in Firecracker.
01Why a disk in S3
A sandbox is a small VM or container that runs one user's work. A platform stops idle sandboxes, and starts them again when the user comes back. If the disk is on the SSD of one machine, the sandbox can start only on that machine. If that machine is full or down, the user waits.
mica keeps the shared copy of each disk in S3, and uses the local SSD as a cache. Any machine can attach the disk. Three rules hold in every part of mica:
- A write never waits for S3. Writes and FLUSH go to the local SSD only.
- A read never waits for data that it does not need. An attach copies nothing. Prefetch runs behind the guest's reads.
- After a detach, S3 has everything. Then any machine can attach the disk.
02Where mica sits
mica is a program in user space, not a kernel module. It uses ublk, a Linux driver that makes a block device whose requests go to a user-space program. For each attached disk, mica asks ublk for a device: /dev/ublkb0, with the link /dev/mica/<disk>. To the rest of the system it is a normal disk. Firecracker, QEMU, a container or a host mount can use it.
The diagram follows one 4 KiB write from an app in a Firecracker guest to the SSD.
- In the guest, the app writes a file. ext4 turns it into a block write, and the virtio-blk driver puts it on a virtqueue that Firecracker shares with the guest.
- Firecracker takes the request and writes it to
/dev/mica/diskon the host, with io_uring. - The host kernel sends the block request to the ublk driver.
- The ublk driver hands the request to mica. Each mica queue thread keeps io_uring commands waiting on
/dev/ublkc0. The driver completes one with the request: operation, offset and length. For a write, the data is copied into the queue thread's buffer. - The queue thread finds the chunk, and writes the data into the disk's data file with io_uring. Then it tells ublk that the request is done, in the same call that waits for the next request.
- The host filesystem keeps the data in the page cache. It goes to the SSD with writeback, and for sure at the next FLUSH.
- Later, the disk engine uploads changed chunks to S3. It also downloads a chunk when a read needs one that is not local.
The queue thread serves most requests alone: reads and writes of chunks that are local, with no lock wait and no other thread. A request that needs more goes to the disk engine, on a tokio runtime in the same process. Examples: the first write to a chunk that is not local, a read that misses the cache, a FLUSH, a FUA write. Each device has one queue thread per CPU, up to 4.
If the mica daemon stops, the kernel keeps the device and holds its requests. The next daemon takes the device over with ublk user recovery, and the guest sees a pause of about 1 s.
03From a disk to chunks
To mica, a disk is a row of bytes, cut into chunks of one size. The size is set when the disk is made: 256 KiB by default, from 64 KiB to 4 MiB. A 20 GiB disk has 81,920 chunks. A chunk is the unit of everything that follows: the local data, the upload, the download and the dedup.
Take a 4 KiB write at byte 1,056,768 of a 20 GiB disk. mica finds its place with three divisions:
- Chunk
- 1,056,768 ÷ 262,144 = 4, rest 8,192: chunk 4, 8 KiB into it
- Data file
- 1,056,768 ÷ 1 GiB = 0:
dirty/data-0, at byte 1,056,768 - Page
- chunk 4 ÷ 1,024 = 0, rest 4: page 0, slot 4
On the local SSD, a disk has sparse data files, one for each GiB of the disk. Each chunk sits at its own disk offset, so a write needs no lookup table: it goes to the same offset in the data file. A data file uses space only for the chunks the guest wrote.
In S3, a chunk is an immutable object. Its key is the SHA-256 of its content, and its body is a zstd frame. So:
- Two equal chunks are one object. Disks made from one image share their chunks.
- mica checks each download against its key.
- An all-zero chunk is not stored. A new disk stores nothing.
A request that crosses chunk borders is split. A 512 KiB read at byte 200 KiB covers chunks 0, 1 and 2, and each part is served on its own.
Pages, roots and snapshots
A page lists the hashes of 1,024 chunks: 32 KiB, for 256 MiB of disk. A root lists the hashes of all pages of a disk: 12.5 KiB for 100 GiB. The head of a disk names its current root. Pages and roots are also named by their hash. So a change writes only the new pages, a new root and the head, and everything else is shared.
chunks/<xx>/<chunk hash> one chunk, zstd, immutable
pages/<xx>/<page hash> the hashes of 1,024 chunks
manifests/<root hash> one root: the hash of each page
disks/<disk>/head the current root and the read profile
disks/<disk>/attached the machine that owns the disk now
snapshots/<name> a named root
04Writes and FLUSH
A guest write goes into the data file and returns. It does not sync. If the chunk is not local yet and the write covers only part of it, mica first copies the chunk from the cache into the data file. On XFS and btrfs that copy is a reflink. On ext4 it is a real copy. A chunk of zeros needs no copy, because every range that is not local is a hole.
A FLUSH is the guest's request for durability, for example at fsync or a journal commit. On FLUSH, mica syncs the data files written since the last FLUSH. Then it appends the chunks that became local to the dirty log, the list of a disk's local chunks, and syncs the log. In that order, the log never lists a chunk whose data a power cut can lose.
Linux starts writeback late: after 10% of free memory is dirty, or after 30 s. So after each 16 MiB of writes, mica starts writeback itself, with sync_file_range. A FLUSH after a large write then finds little left to write.
A write that returned also survives a crash of the daemon before any FLUSH. The page cache keeps its data, and a bitmap in /run lists the chunks that became local since their last FLUSH. /run is a tmpfs: it survives a crash of mica, as the page cache does, and a reboot clears it, as it clears the page cache.
mica needs only plain Linux file calls: sparse files, fdatasync, copy_file_range and hole punching. It works on ext4, XFS and btrfs.
05Reads
For each chunk of a read, the first match wins:
- A local chunk: read the data file.
- A zero chunk: return zeros.
- A chunk in the cache: read the cache file. The cache holds clean chunks that S3 also has, and all disks on the machine share it.
- Otherwise: get the chunk from S3, decompress it, check its hash, store it in the cache, and read it.
Many reads that miss one chunk share one GET. Behind the guest's reads, three kinds of download keep the link busy, in this order:
- Reads by the guest always come first.
- Likely reads. When reads follow each other, mica fetches ahead: 1 MiB first, then up to 8 MiB. At attach it also fetches the read profile: the chunks that the guest read first in its last session, up to 64 MiB.
- The fill. When the link has nothing else to do, mica downloads the rest of the disk, in disk order. It starts at attach, also when the guest reads nothing. Then every read is local.
06Checkpoints
A checkpoint uploads the local chunks that S3 does not have, and commits a new root. It starts 30 s after the last one, or earlier when 512 MiB of changes wait. It also runs at detach and at snapshot.
- Take the list of unsaved chunks. Later changes go to the next checkpoint, so a busy writer cannot make a checkpoint last for ever.
- For each chunk, read it into memory and PUT it, with up to 64 MiB in flight. The guest keeps writing in place.
- PUT the changed pages and the new root.
- Check that this machine still owns the disk. Then write the head.
A chunk stays local after its upload, so the next write to it costs nothing extra. Saved chunks leave the data files only when local data passes its limit, and at detach. They go into the cache.
Garbage collection (GC) deletes objects that no root needs and that are older than 24 hours. A GC can see a new upload as old only if it runs while the PUT ends. So during an upload, mica reads the GC state once a second. Until a check shows that no GC ran, the first write to an uploaded chunk keeps a copy of the uploaded content. If a GC ran, the checkpoint checks every committed chunk and page, and uploads a deleted one again.
07Moving a disk
This is the main use of mica: a sandbox stops on machine A and starts on machine B.
Only one machine may write a disk. With S3 conditional writes, the claim is one atomic create: a PUT with If-None-Match: *. Of two machines that claim at the same moment, only one creates the marker. RustFS, AWS S3, R2 and MinIO support this. For a store without it, such as Garage, mica writes a claim, waits 1 s and reads it back, so an attach takes about 1 s.
08Crash safety
- After a FLUSH returns, its writes survive a crash of mica and a power cut.
- After a write returns, it survives a crash of mica, also without a FLUSH.
- After a detach, S3 has all data, and any machine can attach the disk.
A test harness checks the first two. A writer fills a 256 MiB area with 4 KiB blocks that each name their block, their write and a CRC, and FLUSHes now and then. At a random moment, a device-mapper target under the data folder drops every write: a power cut. Then the daemon dies, and the folder is mounted again, as after a reboot. Every block must hold its last write before the last completed FLUSH, or a later one.
| Test | Data folder | Runs | Lost a write |
|---|---|---|---|
| Power cut | btrfs | 6 | 0 |
| Power cut | XFS | 6 | 0 |
| Power cut | ext4 | 6 | 0 |
| Daemon killed while the guest writes (every returned write must survive) | btrfs | 6 | 0 |
09The lab
All tests ran on one machine. S3 is local, but its link is limited, so the numbers show how mica behaves on a slow link.
| Part | Setup |
|---|---|
| Machine | 16 CPUs, 27 GiB RAM, NVMe SSD, Fedora 44, Linux 7.0 |
| S3 | RustFS 1.0.1 in Docker, 100 MiB/s down and up, 1 ms round trip (20 ms in one series) |
| Local disk | btrfs on the NVMe, limited to 300 MiB/s read and write for mica (cgroup io.max) |
| VM | Firecracker 1.16.1, 2 vCPUs, 2 GiB RAM, Ubuntu 24.04 or Alpine 3.22, on a 100 GiB mica disk |
| mica | 256 KiB chunks, a checkpoint every 30 s or at 512 MiB, conditional writes on |
Without mica, a 4 MiB GET or PUT stream gets 91–97 MiB/s. The disk does 300 MiB/s, and 1,799 synced 4 KiB writes per second.
Test data. The test files are random (AES-CTR), so they do not compress, and no two chunks are equal. Real system files do compress: /usr/bin and Python libraries (2.0 GiB) upload as 675 MiB.
10Results on the host
A script writes 42,048 files (7.93 GiB) into a 20 GiB mica disk with ext4: 48 files of 64 MiB, 2,000 files of 256 KiB to 2 MiB, and 40,000 files of 4 KiB to 128 KiB. Every read pass checks every file against its checksum, and all passes found all files correct. "Cold" means an empty cache: every chunk comes from S3.
| Step | v0.1 | v0.2 |
|---|---|---|
| Attach one disk (p50 of 10) | 1,035 ms | 24 ms |
| Attach 4 disks at the same time | 4.2 s | 0.05 s |
Write 7.93 GiB of files (page cache), then sync | 13.9 + 16.0 s | 17.3 + 15.0 s |
| Start of the write until all data is in S3 | 122.4 s | 120.4 s |
| Cold: attach and mount | 1.29–1.31 s | 0.14–0.19 s |
| Cold: first byte of a 64 MiB file | 0.16 s | 0.00–0.02 s |
| Cold: read 100 small files, one after the other | 0.57 s | 0.23–0.41 s |
| Cold: read and check all files, 1 reader | 115.2 s | 89.5 s |
| Cold: read and check all files, 8 readers | 83.8 s | 82.6 s |
| Whole disk local after a cold attach (the fill) | never | 86 s |
| Warm: read and check all files, 1 reader | 26.8 s | 27.3 s |
The link sets the cold limit: 7.93 GiB at 95 MiB/s takes at least 85 s. v0.2 comes close with one reader, because read-ahead keeps several downloads in flight. The disk limit (300 MiB/s) sets the warm limit. The upload also needs at least 85 s. Checkpoints start it during the write, but the host writes much faster than the link, so the link sets this time too.
Writes and FLUSH
A small tool writes 4 KiB blocks with O_DIRECT on the mica device, 15 s for each test. The first five rows are the mean of 3 rounds on a quiet machine. The last two come from one earlier run of both builds, one after the other. v0.2a is v0.2 with one file per local chunk, the layout before the data files.
| Test | v0.2a | v0.2 |
|---|---|---|
Write + fdatasync, idle disk | 1,515/s | 1,438/s |
| Longest FLUSH right after a 2 GiB write | 3.6–4.6 s | 0.46–0.95 s |
Write + fdatasync while that 2 GiB uploads | 586/s | 695/s |
Random write + fdatasync to chunks the last checkpoint saved | 265/s | 584/s |
| Random write to saved chunks, no sync | 512/s | 19,290/s |
| Random 4K read of saved local chunks, queue depth 1 | 56,326/s | 84,853/s |
| 4 parallel writers, 1 GiB each, 1 MiB direct writes | 1,651 MiB/s | 1,651 MiB/s |
The idle FLUSH rate is 80% of the raw disk's 1,799 per second, and 5% lower than with one file per chunk. A test without mica shows the cause: on btrfs, a synced 4 KiB write into a large file costs about 50 µs more than into a new small file.
11Results in Firecracker
The VM's root disk is a 100 GiB mica disk with ext4. The mica device goes straight to Firecracker as a virtio block device (io_engine = Async). A boot time runs from mica disk attach until ssh in the guest answers. It includes the attach, the start of Firecracker, the kernel and the init system.
Build. These numbers come from the v0.2 build before the data-file layout. Boots and restores were not measured again after that change. Guest fdatasync measured the same for both layouts.
| Boot, 1 ms round trip | v0.1 | v0.2 |
|---|---|---|
| Cold boot, Ubuntu (empty cache) | 4.61 s | 2.33 s |
| Warm boot, Ubuntu / Alpine | 3.29 s / – | 2.28 / 1.44 s |
| Cold boot of a disk after an 8 GiB fill | 5.5–7.9 s | 2.39 s |
| S3 download during a cold boot (before compression) | ≈ 250 MiB | 136 MiB |
| Upload of a new root filesystem (270 MiB of files) | 5.7 s | 1.5 s |
| Guest: write 7.9 GiB of files until all data is in S3 | 169.5 s | 148.6 s |
| Boot, 20 ms round trip (S3 in the same region) | v0.1 | v0.2 |
|---|---|---|
| Cold boot, Ubuntu | 7.46 s | 3.96 s |
| Cold boot of a disk after an 8 GiB fill | 8.2–9.5 s | 4.0 s |
| Warm boot, Ubuntu | 3.52 s | 2.37 s |
| Cold / warm boot, Alpine | 4.01 / 2.64 s | 2.68 / 1.51 s |
| Guest: read and check 7.9 GiB, 1 reader, cold | 149.6 s | 109.6 s |
Two changes give most of the gain. Smaller chunks cut the download of a cold boot nearly in half: with 4 MiB chunks, each small read of a file or of ext4 metadata cost 4 MiB. And the attach went from about 1 s to 24 ms, because the claim is one atomic create, and each disk has its own lock.
I/O in the guest
fio in the guest, on a 4 GiB file, with direct I/O. "Idle" means a checkpoint ran before each test. "During upload" runs right after the 4 GiB file is written, while its checkpoint uploads.
| fio in the guest | v0.1 | v0.2, idle | v0.2, during upload |
|---|---|---|---|
| 4K random read, queue depth 32 | 166.5k IOPS | 159.7k IOPS | 47.3k IOPS |
| 4K random read, queue depth 1 | 26.8k IOPS | 27.8k IOPS | 20.9k IOPS |
| 4K random write, queue depth 32 | 124.8k IOPS | 110.0k IOPS | 100.0k IOPS |
| 4K random write, queue depth 1 | 16.5k IOPS | 11.5k IOPS | 12.8k IOPS |
Firecracker opens the device without O_DIRECT, so guest reads also hit the host page cache. v0.1 never uploaded during these tests, because it checkpointed only every 3 minutes. While an upload runs, guest reads share the SSD with it.
Guest fdatasync rates moved by up to 2 times between runs on this busy machine. In one session, both local layouts did 355 per second. Right after a 4 GiB write, while its upload ran, the data-file layout did 68 per second and the older layout none. Early writeback came after these runs, and is not measured in the guest yet.
12Memory snapshots
A Firecracker memory snapshot stores the guest RAM in a file. A restore starts a new Firecracker from that file, and the guest continues without a boot. In the test, the guest runs an HTTP server that holds 384 MiB of random data in RAM, and each GET returns a hash of all of it. So the first answer needs all app memory back, and the hash proves that it is correct. Every restore returned the correct hash.
The memory file (2 GiB) is an ordinary file on a second mica disk. mica stores it in 307 MiB: zero chunks are not stored, and the rest is compressed. Each restore uses a new clone of the root disk's snapshot.
| Restore | v0.1 | v0.2, 1 ms | v0.2, 20 ms |
|---|---|---|---|
| Cold: attach of the root clone and the memory disk | 2.43 s | 0.26 s | 0.57 s |
| Cold: ssh answers | 2.38 s | 0.64 s | 1.56 s |
| Cold: app answers | 6.71 s | 4.63 s | 5.77 s |
| Cold: S3 download | 630 MiB | 453 MiB | – |
| Warm: ssh / app answers | 0.20 / 0.63 s | 0.23 / 0.71 s | 0.20 / 0.64 s |
| Memory disk in S3 | 996 MiB | 307 MiB | 361 MiB |
Warm, ssh answers 0.23 s after the snapshot load. A warm boot needs 2.28 s from attach until ssh. A cold restore is limited by bytes: the app's 384 MiB of random data does not compress, and alone it takes about 4 s on a 95 MiB/s link. A restore that touches less memory starts faster.
mica uses Firecracker's File backend: the kernel loads each page from the memory file when the guest first uses it. A userfaultfd handler that installs a recorded set of pages first was no faster cold (4.77 s against 4.82 s, both limited by the link), and slower warm on demand (3.19 s against 0.77 s). So mica stays a plain block device, not tied to Firecracker. VMs that restore from one snapshot on one machine share mica's chunk cache, so each chunk downloads once.
13Limits
- The link sets the cold speed. mica can change the order of downloads, not the bytes that the guest needs. Compression and small chunks lower the bytes.
- Firecracker does not pass discard to the disk. Space that a guest frees stays in mica and in S3. In the guest fill test, 4 GiB of deleted data uploaded.
- The cache evicts by last use, from any disk. An attached disk can lose cached chunks, and the fill stops once the cache is 90% full. A mode that keeps attached disks fully local is planned.
- An attach takes about 1 s without conditional writes, for example on Garage.
- Writes during a large upload share the SSD with the upload and its writeback.
- Two GC gaps. A page that GC deletes between a commit and its repair is lost if the machine loses power in that window. Without conditional writes, two GCs at the same time can hide each other.
- One lab machine. S3 runs on the same SSD as mica, and the machine also ran other work, so tail latencies here are worse than on a dedicated node.
14Sources
- mica v0.2 optimization: each change, its measurement and its review.
- mica v0.1 deck: the first design.
- Linux ublk documentation, including user recovery.
- Firecracker snapshot support.
- S3 conditional writes.
sync_file_range(2)and errseq, for how Linux reports writeback errors.- RustFS, the S3 server of the lab.