mica v0.2 optimization
The baseline of mica v0.1 in a local lab, each optimization step of v0.2, and the result at 1 ms and 20 ms round trip to S3.
Context
mica gives a VM or a container a disk that lives in an S3 bucket. The bucket holds the shared copy of the disk. The local SSD of the machine is a cache. A chunk is a 4 MiB part of the disk. mica downloads a chunk when a read needs it, and uploads changed chunks in the background. One upload round is a checkpoint.
The goal of v0.2 is a fast wake: a VM that stops on one machine must start on another machine quickly. Before we change mica, we must know how fast it is now, and where the time goes. This page records that baseline. Later parts compare each change against these numbers.
The lab
All tests run on one machine. Three parts simulate a real node:
- S3: RustFS 1.0.1 in Docker. A shaper container limits its network link with
tc: 100 MiB/s in each direction, with 1 ms round trip. - Local disk: a separate mica daemon (
mica-lab.service) keeps its chunks on the NVMe disk, on btrfs. A cgroup limit (io.max) caps this daemon at 300 MiB/s for reads and for writes. - VM: Firecracker 1.16.1 boots Ubuntu 24.04 from a 100 GiB mica disk.
| Item | Value |
|---|---|
| Host | 16 CPUs, 27 GiB RAM, NVMe SSD, Fedora 44, Linux 7.0 |
| mica | v0.1 (commit 3e56460), release build |
| mica settings | cache_limit_gib = 150, dirty_limit_gib = 40, checkpoint_interval_secs = 180 |
| S3 link | 100 MiB/s down, 100 MiB/s up, 1 ms round trip |
| Local disk limit | 300 MiB/s read, 300 MiB/s write, no IOPS limit |
Raw capacity of the lab
These numbers show the limits that mica works within.
| Test | Tool | Result |
|---|---|---|
| S3 round trip | ping | 1.06 ms |
| S3 PUT, 4 MiB objects, 1 at a time | warp | 69 MiB/s (58 ms each) |
| S3 GET, 4 MiB objects, 1 at a time | warp | 91 MiB/s (44 ms each) |
| S3 PUT / GET, 4 MiB objects, 4–32 at a time | warp | 94–96 / 96–97 MiB/s |
| S3 upload of one 1 GiB object, 4–16 streams | s5cmd | 11.2 s (≈ 92 MiB/s) |
| Disk 1M sequential read / write | fio, direct I/O | 299 / 300 MiB/s |
| Disk 4K random read / write, queue depth 32 | fio, direct I/O | 76k / 19.7k IOPS |
| Disk 4K random read / write, queue depth 1 | fio, direct I/O | 15.4k IOPS, 64 µs / 19.8k IOPS, 49 µs |
Disk 4K write with fdatasync | fio | 1,799 per second |
With 1 ms round trip, one stream almost fills the link. Thus, at this latency, the number of requests at the same time has little effect. A second lab profile with 20 ms round trip will show the effect of latency.
mica on the host
A script writes 42,048 files with random content into a 20 GiB mica disk with ext4: 7.93 GiB in total. The content comes from an AES-CTR stream, so no two chunks are equal and no chunk is all zeros.
| Class | Files | Size of each file | Total |
|---|---|---|---|
| Large | 48 | 64 MiB | 3,072 MiB |
| Medium | 2,000 | 256 KiB – 2 MiB | 2,274 MiB |
| Small | 40,000 | 4 KiB – 128 KiB | 2,585 MiB |
After the write, the script records a checksum of each file. Each later read pass reads every file and compares the checksum. All passes found all 42,048 files correct.
Write the data and save it in S3
| Step | Time | Note |
|---|---|---|
mica disk create (20 GiB) | 0.04 s | |
| First attach and mount | 1.14 s | Includes the ext4 format |
| Write 7.93 GiB of files | 13.9 s | 572 MiB/s, into the page cache |
sync | 16.0 s | The 300 MiB/s disk limit applies here |
| Record checksums | 4.9 s | From the page cache |
| Unmount | 0.5 s | Includes fstrim |
mica disk detach | 87.2 s | Uploads 8.0 GiB: ≈ 94 MiB/s |
| Start of the write until all data is in S3 | 122.4 s |
In this run, the upload starts only when the detach starts. The data (8 GiB) is below the early-checkpoint level (half of dirty_limit_gib, here 20 GiB). The run is also shorter than the checkpoint interval (180 s). Thus write, sync and upload run one after the other. The upload alone runs at the speed of the link.
Read the data
A cold read has an empty local cache: every chunk comes from S3. A warm read has all chunks in the local cache. Before each pass, the script empties the page cache of the host.
| Test | Cold | Warm |
|---|---|---|
| Attach and mount | 1.29–1.31 s | 1.14 s |
| First byte of a 64 MiB file | 0.16–0.17 s | under 0.01 s |
| Read one 64 MiB file | 0.72–0.73 s (≈ 89 MiB/s) | 0.10 s |
| Read 100 small files, one after the other | 0.57–0.59 s | 0.10–0.11 s |
| Read and check all files, 1 reader | 115.2 s (69 MiB/s) | 26.8 s (296 MiB/s) |
| Read and check all files, 8 readers | 83.8 s (95 MiB/s) | 26.8 s (296 MiB/s) |
| Read and check all files, 32 readers | 83.2 s (95 MiB/s) | – |
| Unmount and detach, no file changes | 0.96–1.08 s | 0.90–0.92 s |
mica disk warm downloads all chunks into the cache: 84.9 s with 8 downloads at the same time, and 84.9 s with 32. Both reach the link limit.
What these numbers show
- The link and the disk set the limits. Cold reads with 8 or more readers and
disk warmrun at the link limit (≈ 95 MiB/s). Warm reads run at the disk limit (296 MiB/s). - One cold reader loses about 27%. It gets 69 MiB/s, because it waits for one chunk download after the other. At 1 ms round trip the cost is small. At a higher round trip, it will grow.
- A mount costs about 1.1–1.3 s, also with a warm cache. The next step is to find where this time goes.
- Moving a disk is serial. Write, sync and upload do not overlap, so the time to move a disk is their sum.
Firecracker on a mica disk
The VM has 2 vCPUs and 2 GiB RAM. Its root disk is a 100 GiB mica disk with ext4. The guest is Ubuntu 24.04 with systemd, sshd and the test tools: 270 MiB of files. The disk device goes straight to Firecracker as a virtio block device (io_engine = Async, cache_type = Writeback).
Boot
A boot time here is the time from mica disk attach until ssh in the guest answers. It includes the attach (about 1.1 s), the Firecracker start, the kernel boot and systemd (about 0.6 s of user space).
| Test | Time | S3 download |
|---|---|---|
Make the disk: disk create + mke2fs -d with the root filesystem | 3.9 s | – |
| First detach (uploads the root filesystem) | 5.7 s | – |
| Cold boot, empty cache | 4.61 s | ≈ 250 MiB |
| Warm boot, all chunks in the cache | 3.29 s | 0 |
| Cold boot of the same disk after the 8 GiB fill | 5.5–7.9 s | ≈ 200–400 MiB |
| Snapshot of the disk / clone from the snapshot | 0.02 s / 0.04 s | – |
| Boot of the clone (the cache already has the chunks) | 3.35 s | 0 |
| Guest reboot until Firecracker exits | 0.7–1.1 s | – |
The cold boot downloads about 250 MiB, but the root filesystem has only 270 MiB of files. The boot reads only a part of these files. With 4 MiB chunks, each small read of ext4 metadata or of a file downloads a full 4 MiB chunk. At 100 MiB/s, these 250 MiB cost about 1.3 s of the 4.6 s cold boot. Smaller chunks can remove most of this download.
I/O in the guest
fio runs in the guest on a 4 GiB file in the root filesystem, with direct I/O. Firecracker opens the mica device without O_DIRECT, so the host page cache also holds the disk data. Thus the read numbers show the host page cache, not the mica disk limits.
| Test | Result |
|---|---|
| 4K random read / write, queue depth 32 | 48k IOPS / 100k IOPS |
| 4K random read / write, queue depth 1 | 24.7k IOPS, 40 µs / 15.5k IOPS, 64 µs |
| 1M sequential read / write, queue depth 16 | 9.7 GiB/s / 3.7 GiB/s (host page cache) |
4K write with fdatasync | 427 per second |
Fill the guest, save, read back
The guest writes the same 7.93 GiB data set into its root filesystem. Then it shuts down, and mica disk detach uploads the disk.
| Step | Time |
|---|---|
| Write 7.93 GiB of files in the guest | 17.1 s |
sync in the guest | 15.5 s |
| Record checksums in the guest (2 readers) | 23.5 s |
Guest reboot, then mica disk detach (uploads 12.2 GiB) | 113.5 s |
| Start of the write until all data is in S3 | 169.5 s |
| Cold boot, then read and check all files in the guest, 1 reader | 7.9 s + 92.3 s |
| Cold boot, then read and check all files in the guest, 8 readers | 5.5 s + 85.6 s |
| Clone boot, then read and check all files, 8 readers (warm cache) | 3.4 s + 26.5 s |
The detach uploads 12.2 GiB, not 8 GiB. The other 4 GiB is the fio test file. The guest deleted it, but fstrim in the guest fails: "the discard operation is not supported". Firecracker's virtio block device does not pass discard to the host. Thus, space that a guest frees on a mica disk stays in the disk, and mica uploads and stores it.
What these numbers show
- A cold boot downloads almost the full root filesystem. 250 MiB for a 270 MiB system, because each small read costs a 4 MiB chunk.
- Attach is about a third of a warm boot. A warm boot takes 3.3 s, and the attach takes about 1.1 s of this.
- Freed guest space is never released. Without discard from Firecracker, deleted data stays in S3. A fix needs discard support in the VMM, or a periodic trim through another path.
- A clone of a snapshot is free. The snapshot and the clone take less than 0.05 s, and the clone boots warm from the shared cache.
Memory snapshots
A Firecracker memory snapshot stores the full guest RAM in a file, and the device state in a small second file. A restore starts a new Firecracker process from these files. The guest continues from the moment of the snapshot, without a boot.
The test VM runs an app before the snapshot: an HTTP server that holds 384 MiB of random data in RAM. Each GET returns a hash of all 384 MiB. Thus the first answer after a restore needs all app memory back in RAM, and the hash shows that the memory is correct. All restores returned the correct hash.
Make the snapshot
- Pause the VM.
- Write the snapshot files to an ext4 filesystem on a second mica disk (4 GiB). The memory file is 2,048 MiB.
- Make a mica snapshot of the root disk while the VM is paused. Thus the disk and the memory are from the same moment.
- Detach both disks. Each restore starts from a new clone of the root disk snapshot.
| Step | Time | Note |
|---|---|---|
| Pause | 0.01 s | |
| Firecracker snapshot to the mica disk | 5.81 s | 2,048 MiB memory file |
| mica snapshot of the root disk | 6.01 s | Includes a checkpoint of the root disk |
| Detach the memory disk | 10.8 s | Uploads 996 MiB. mica does not store the all-zero chunks. |
Restore with the File backend
With the File backend, Firecracker maps the memory file into the guest. A page loads from the file when the guest first uses it. On a mica disk, this read goes through ext4 and the mica device. On a cache miss, mica downloads the full 4 MiB chunk from S3.
The times start at the snapshot load request. "Attach" is the time before it: a clone of the root disk, the attach of both disks, and the mount of the memory disk.
| Variant | Attach | Load | ssh answers | App answers | S3 download |
|---|---|---|---|---|---|
| Cold cache | 2.43 s | 0.23 s | 2.38 s | 6.71 s | 630 MiB |
| Cold cache, with mica read-profile prefetch (512 chunks) | 2.84 s | 0.03 s | 2.40 s | 5.87 s | 630 MiB |
| Warm cache | 2.22 s | 0.02 s | 0.20 s | 0.63 s | 3 MiB |
mica disk warm of the memory disk first (10.5 s), then restore | 2.27 s | 0.02 s | 0.21 s | 0.65 s | 27 MiB |
What these numbers show
- A warm restore is 5 times faster than a warm boot. ssh answers 0.20 s after the load, and the app 0.63 s after the load. A warm boot needs 2.2 s until ssh, and then the app must start.
- A cold restore is limited by the bytes it downloads. The app answer needs 630 MiB: at 95 MiB/s that is 6.6 s, and the measured time is 6.7 s. The guest uses only a part of each 4 MiB chunk, but mica downloads the full chunk.
- The mica read profile does not help here. The profile has 157 chunks (628 MiB), and the prefetch downloads them in about 6 s. These are the same chunks that the page faults need, so the link is the limit again. Only fewer bytes for each page fault can help: smaller chunks, or a handler that knows which pages are in use.
- The attach takes more time than a warm restore. Two disks attach one after the other, and with the clone and the mount this takes 2.2–2.8 s. Parallel attach and a faster attach path are important for a fast wake.
Next: two restore variants with a userfaultfd handler (pages on demand, and a recorded page profile that loads first). These need one more permission on the lab machine.
S3 cost by object size
Smaller chunks mean smaller S3 objects, and more of them. This test measures RustFS with 64 KiB, 256 KiB and 4 MiB objects, with 1 to 64 requests at the same time (warp, 15 s each, 1 ms round trip).
| Object size | PUT, 1 at a time | PUT, 8 | PUT, 32 | GET, 1 at a time | GET, 8 |
|---|---|---|---|---|---|
| 64 KiB | 13.8 MiB/s (4.6 ms) | 81 MiB/s | 94 MiB/s | 46 MiB/s (1.3 ms) | 95 MiB/s |
| 256 KiB | 38 MiB/s (6.6 ms) | 85 MiB/s | 95 MiB/s | 95 MiB/s (2.6 ms) | 95 MiB/s |
| 4 MiB | 65 MiB/s (62 ms) | 94 MiB/s | 96 MiB/s | 95 MiB/s (42 ms) | 97 MiB/s |
- A PUT has a fixed cost of about 4 ms in RustFS. With one PUT at a time, small objects are slow: 13.8 MiB/s at 64 KiB. With 8 PUTs at a time, 256 KiB objects reach 85 MiB/s, and with 32 they fill the link.
- A GET of 256 KiB fills the link with one request at a time at 1 ms round trip. A GET of 64 KiB needs about 8 at a time.
- Thus, 256 KiB chunks need about 32 uploads at the same time to fill this link. mica v0.1 uploads 16 at a time. At a higher round trip, more are needed.
The same GET test with a 20 ms round trip, which is closer to an S3 service in the same region:
| Object size | GET, 1 at a time | GET, 8 at a time | GET, 32 at a time |
|---|---|---|---|
| 64 KiB | 2.9 MiB/s (21.5 ms) | 24 MiB/s | 95 MiB/s |
| 256 KiB | 11.6 MiB/s (21.7 ms) | 95 MiB/s | 96 MiB/s |
| 4 MiB | 65 MiB/s (62.5 ms) | 97 MiB/s | – |
At 20 ms, one request at a time is slow for small objects: 11.6 MiB/s at 256 KiB. With 8 at a time, 256 KiB objects fill the link. So small chunks need many downloads at the same time: read-ahead for one reader, and parallel fetches for the prefetch and the fill.
v0.1 against v0.2, all steps
The same lab and the same scripts: 100 MiB/s, 1 ms round trip, 300 MiB/s disk. v0.2 is the build after all the steps below, with the review fixes. Every read pass checked all 42,048 files.
| Test | v0.1 | v0.2 |
|---|---|---|
| Attach of one disk (p50 of 10) | 1,035 ms | 24 ms |
| 4 disks attached at the same time | 4.2 s | 0.05 s |
| Cold Firecracker boot, Ubuntu: attach until ssh answers | 4.61 s | 2.33 s |
| Warm Firecracker boot, Ubuntu / Alpine | 3.29 s / – | 2.28 s / 1.44 s |
| Cold boot of a disk after an 8 GiB fill | 5.5–7.9 s | 2.39 s |
| Upload of a new root filesystem (Ubuntu, 270 MiB of files) | 5.7 s | 1.5 s |
| Cold memory restore: ssh answers | 2.38 s | 0.64 s |
| Cold memory restore: app answers (384 MiB of random data in RAM) | 6.71 s | 4.63 s |
| Cold memory restore: attach of root disk and memory disk | 2.43 s | 0.26 s |
| Cold memory restore: S3 download | 630 MiB | 453 MiB |
| Memory disk in S3 (2 GiB memory file) | 996 MiB | 307 MiB |
| Warm memory restore: ssh / app answers | 0.20 / 0.63 s | 0.23 / 0.71 s |
| Host: 7.9 GiB write until all data is in S3 | 122.4 s | 111.3 s |
| Guest: 7.9 GiB write until all data is in S3 | 169.5 s | 148.6 s |
| Host: first byte of a 64 MiB file / 100 small files, cold | 0.16 s / 0.57 s | 0.01 s / 0.28 s |
| Host: read and check 7.9 GiB, 1 reader, cold | 115.2 s | 87.6 s |
| Host: read and check 7.9 GiB, 8 readers, cold | 83.8 s | 82.4 s |
| Host: whole disk local after a cold attach | – (no fill) | 84 s |
Guest fio 4K write + fdatasync, while an upload runs | 427 per second | 112–169 per second (worse; see step 5) |
Where v0.2 is not faster, the link is the limit: 8 readers already used the full 100 MiB/s in v0.1. The warm restore and the fsync rate are slower; the next section explains both.
v0.1 against v0.2 at 20 ms round trip
The same suite with a 20 ms round trip to S3, closer to an S3 service in the same region. v0.1 runs with its own defaults (a checkpoint every 3 minutes, no fill). The v0.2 build is from before the last fixes of step 5.
| Test (20 ms) | v0.1 | v0.2 |
|---|---|---|
| Cold Firecracker boot, Ubuntu: attach until ssh answers | 7.46 s | 3.96 s |
| Cold boot of a disk after the 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 s / 2.64 s | 2.68 s / 1.51 s |
| Cold memory restore: attach / ssh answers / app answers | 3.02 / 3.27 / 8.04 s | 0.57 / 1.56 / 5.77 s |
| Warm memory restore: ssh / app answers | 0.21 / 0.67 s | 0.20 / 0.64 s |
| Memory disk upload | 996 MiB in 11.3 s | 361 MiB in 7.9 s |
| Host: attach and mount | 1.65 s | 0.18–0.25 s |
| Host: first byte of a 64 MiB file / 100 small files, cold | 0.21 s / 0.96 s | 0.03–0.10 s / 0.49–0.67 s |
| Host: 7.9 GiB write until all data is in S3 | 119.9 s | 117.9 s |
| Guest: 7.9 GiB write until all data is in S3 | 173.2 s | 147.5 s |
| Host: read and check all files, 1 reader, cold | 136.1 s | 140.0 s |
| Guest: read and check all files, 1 reader, cold | 149.6 s | 109.6 s |
| Read and check all files, 8 readers, cold (host / guest) | 85.7 / 89.4 s | 86.7 / 87.5 s |
| Host: whole disk local after a cold attach | – (no fill) | 366 s (too slow; fixed) |
Guest fio: 4K write + fdatasync / 4K write at queue depth 1 | 388/s / 16.4k IOPS | 205/s / 8.6k IOPS |
At 20 ms the gains are larger than at 1 ms: a cold boot is 1.9 times faster, and a cold restore answers ssh 2 times faster. Three results need more work:
- The background fill was slow: 2 downloads in flight for each disk give only 22 MiB/s at 20 ms. The fill now has 8 MiB in flight for each disk.
- One host reader of many small files did not gain. The files are close together on the disk, but not end to end, so read-ahead did not start. Now a read that starts up to 1 MiB after the last one also counts as sequential.
- Writes while an upload runs are slower, as at 1 ms. See step 5.
Optimizations
Each step below changes mica, then measures again in the same lab, with the same S3 link (100 MiB/s, 1 ms) and the same disk limit (300 MiB/s). "Before" is v0.1, measured with the same script.
| Step | Metric | Before (v0.1) | After |
|---|---|---|---|
| 1. Attach | Attach of one disk, p50 of 10 | 1,035 ms | 24 ms |
| 1. Attach | 4 disks attached at the same time | 4,160–4,210 ms | 45–64 ms |
| 1. Attach | Warm Firecracker boot, attach until ssh answers | 3.29 s | 2.33–2.39 s |
| 2. Small chunks | Cold Firecracker boot, attach until ssh answers | 4.61 s | 2.80 s |
| 2. Small chunks | S3 download during a cold boot | ≈ 250 MiB | 136 MiB |
| 2. Small chunks | Cold boot after the 8 GiB fill | 5.5–7.9 s | 3.63 s |
| 2. Small chunks | Host: first byte of a 64 MiB file, cold | 0.16 s | 0.01 s |
| 2. Small chunks | Host: 100 small files one by one, cold | 0.57 s | 0.21–0.24 s |
| 2. Small chunks | Host: read and check all files, 1 reader, cold | 115.2 s | 98.9 s |
| 2. Small chunks | Host: write 7.93 GiB of files (to the page cache) | 13.9 s | 6.0 s |
| 2. Small chunks | Guest fio 4K random read / write, queue depth 32 | 48k / 100k IOPS | 129k / 116k IOPS |
| 2. Small chunks | Memory restore, cold: attach / ssh answers / app answers | 2.43 / 2.38 / 6.71 s | 0.30 / 1.89 / 6.19 s |
| 2. Small chunks | Memory restore, cold: S3 download | 630 MiB | 555 MiB |
| 2. Small chunks | Memory disk upload (2 GiB memory file) | 996 MiB | 943 MiB |
| 3. Prefetch | Host: read and check all files, 1 reader, cold | 115.2 s | 87.3 s |
| 3. Prefetch | Host: whole disk local after a cold attach (background fill) | – | 84 s (link limit) |
| 3. Prefetch | Guest: read and check all files, 1 reader, cold | 92.3 s | 88.3 s |
| 3. Prefetch | Clone: read and check all files, 8 readers | 26.5 s | 23.2 s |
| 3. Prefetch | Memory restore, cold: app answers / S3 download | 6.71 s / 630 MiB | 6.60 s / 652 MiB (worse than step 2) |
| 4. Compression | Cold Firecracker boot (Ubuntu), attach until ssh answers | 4.61 s | 2.35 s |
| 4. Compression | Memory disk upload (2 GiB memory file) | 996 MiB in 10.8 s | 358 MiB in 4.6 s |
| 4. Compression | Memory restore, cold: ssh answers | 2.38 s | 0.55 s |
| 4. Compression | Memory restore, cold: app answers / S3 download | 6.71 s / 630 MiB | 4.66 s / 451 MiB |
| 4. Continuous checkpoints | Guest: start of the 8 GiB write until all data is in S3 | 169.5 s | 139.3 s |
| 4. Continuous checkpoints | Guest fio: 4K write + fdatasync | 427 per second | 263 per second (worse) |
| 8. Local storage | Host: random 4K writes to saved chunks (before: v0.2a) | 1,174 per second | 25,611 per second |
| 8. Local storage | Host: same, with fdatasync after each (before: v0.2a) | 217 per second | 712 per second |
1. Attach: an atomic claim, and no global lock
Problem. An attach took about 1 s, also with a warm cache. Of this, 1 s was a fixed wait. mica writes a marker to S3 to claim the disk. Without conditional writes, two nodes can claim at the same moment. So mica waits 1 s, then reads the marker back. Also, the daemon held one global lock during the full attach, so attaches of different disks ran one after the other.
Change.
- A new setting,
s3.conditional_writes. When the store supports it, the claim is one atomic create (PUTwithIf-None-Match: *). Of two nodes that claim at the same moment, only one can create the marker, so there is no wait. RustFS, R2, AWS S3 and MinIO support this. Garage does not, so the old claim with the 1 s wait stays as the default. - With the old claim, mica now loads the disk metadata during the 1 s wait, not after it.
- Each disk has its own attach lock. The global registry is locked only for short updates. Two attaches of the same disk wait for each other and get the same device.
- Each attach logs and returns the time of each stage.
Result. One attach now takes 24 ms (p50). The stages are: claim and metadata load 16 ms, open the disk 2 ms, start the ublk device 2 ms. Four attaches at the same time take 45–64 ms. With conditional_writes = false, an attach still takes 1.03 s, as before.
2. Small chunks and paged metadata
Problem. A cold boot downloaded 250 MiB to read a part of a 270 MiB system, because each small read cost a 4 MiB chunk. Smaller chunks fix this, but they make the old manifest large. A manifest listed every chunk: 32 bytes for each chunk, so 12.5 MiB for a 100 GiB disk with 256 KiB chunks. mica uploaded the full manifest on each commit, and downloaded it on each cold attach. The daemon also held a lock and a slot for every chunk in memory.
Change.
- Chunk size for each disk.
mica disk create --chunk-sizetakes 64K to 4M. The default is now 256 KiB. - Paged metadata. A page holds the hashes of 1,024 chunks (32 KiB; it covers 256 MiB). The root holds one hash for each page: 12.5 KiB for 100 GiB. Pages are immutable, and their key is their hash, so checkpoints, snapshots and clones share every page they do not change. A page of zero chunks is not stored. A commit uploads only the changed pages and the new root.
- Lazy state. The daemon loads a page, and makes the slots of its 1,024 chunks, when the guest first uses it. A node-level page store keeps verified pages in memory and in local files.
- Checkpoint. The checkpoint takes the set of local chunks at its start, and freezes and uploads each chunk in one stream. Up to 64 MiB of uploads are in flight. A writer that keeps writing cannot make the checkpoint run for ever.
- No HEAD for each chunk after a commit. v0.1 checked every committed chunk with a HEAD request, in case a GC deleted it during the commit. Now GC writes a record at the end of each run. A checkpoint reads the GC lock and this record before and after it. It checks the chunks and pages only when a GC ran in this time. The cost is four requests for each checkpoint, not one for each chunk.
- No sync for cache downloads. A cold read no longer waits for a sync of the cache file. After a crash, a torn file is found by its hash before its first read.
- No sync for a new chunk file of zeros. A new chunk file without content to copy is no longer synced before its rename. Recovery restores its length after a power loss.
Review. A second model (codex) reviewed the change for crash and race problems. It found four real problems, which are fixed: pages also need the GC check; the GC lock and record must be read one after the other; a failed upload must not stop other chunks in the middle of a freeze; a chunk file can be short after a power loss.
Result. A cold boot downloads 136 MiB instead of about 250 MiB, and takes 2.8 s instead of 4.6 s. Small cold reads are 2–16 times faster. The upload speed does not change, because the link is the limit. The cold memory restore improves less: the app reads 384 MiB of its own memory, and this alone takes about 4 s at 100 MiB/s.
3. Prefetch, read-ahead and background fill
Problem. mica downloaded a chunk only when a read touched it. A single reader waited for one download after the other, and a disk never became fully local.
Change.
- Read-ahead. When reads continue each other, mica fetches the next chunks: 1 MiB ahead at first, then twice as much each time, up to 8 MiB.
- Startup prefetch, on by default. At attach, mica fetches the chunks of the read profile: the first 64 MiB that the disk read in its last session.
- Background fill. After that, mica downloads every other chunk of the disk, in disk order, until the cache is nearly full. Then all reads are local, also if the link goes down.
- An order of priority for downloads. Reads by the guest come first (64 at a time). Likely reads, from read-ahead and the profile, start only while no read waits. The fill starts only while no read and no likely read is in flight.
Review. codex found six problems in this step, all fixed: a cancelled read could leave a counter high for ever; read-ahead could pile up tasks without limit; fetches could continue after a detach; the fill could hold the slots that read-ahead needs; a shutdown time-out could cancel a checkpoint in the middle of a freeze; and a repair check could run before a running GC sent its last delete.
Result. One cold reader is now at the link speed: 87 s for 7.9 GiB, which is 91 MiB/s. After a cold attach, the background fill makes the full disk local in 84 s, which is also the link limit. Boot times do not change.
A regression. The cold memory restore became slower: 6.60 s, with 652 MiB downloaded. The startup prefetch of the root disk downloaded its 64 MiB boot profile in 0.6 s. A restored guest does not read its boot files again, so this download only took bandwidth from the restore. Small fill downloads in the gaps between reads also took some. The fix for step 4: likely reads start only while fewer than 8 read downloads are in flight, and the fill starts only after 250 ms without a new read download.
4. Compression and continuous checkpoints
Problem. After step 3, cold reads and uploads are limited by the link. The only way to make them faster is to send fewer bytes. Also, a disk that the guest fills uploads only at detach, so write, sync and upload run one after the other.
Change.
- zstd compression. Each chunk is stored as a zstd frame (level 1), and each page too (level 3). The key stays the SHA-256 of the plain data, so equal chunks still share one object. A download is decompressed, then checked. The local cache keeps plain files, so local reads do not decompress.
- Continuous checkpoints. A checkpoint now starts every 30 s (was 3 minutes), and also when 512 MiB of local data waits. Since step 2, a commit writes only the changed pages and a small root, so frequent checkpoints are cheap.
mica disk checkpointuploads the changes of an attached disk on request.- The two scheduling rules from the step 3 regression.
Result. The test data of the file tests is random, so it does not compress, and those numbers do not change. Real system data does compress: a test with /usr/bin and Python libraries (2.0 GiB) uploaded 675 MiB. The memory file of the snapshot shrinks from 996 MiB to 358 MiB in S3. A cold restore answers ssh after 0.55 s instead of 2.38 s. The app answers after 4.66 s. Most of its 451 MiB is the app's own 384 MiB of random data, which does not compress. A cold boot takes 2.35 s, down from 4.61 s in v0.1.
Problems found.
- Writes during an upload became slower. The checkpoint synced each chunk before it renamed it to
.frozen. Under heavy writes, up to 64 of these syncs at the same time stalled the writer for seconds, and the upload ran at only 17 MiB/s during the write. Also, a write to a frozen chunk must first copy it. Thusfdatasyncin the guest went from 427 to 263 per second while an upload ran. The fix: the freeze no longer syncs. The upload reads the file through the page cache, and a guest FLUSH now also syncs the.frozenfile of a chunk it wrote. Measured in the next run. - The Alpine guest did not get a network address, because its network service needs a package that the image does not have. Fixed with a start script. ssh now answers 1.34 s after the start of Firecracker, against 2.17 s for Ubuntu.
Review. codex found one more bug, also present in v0.1: with wait_when_behind, a checkpoint woke the waiting writers before it cleared the "behind" state, so a writer could wait for ever. Fixed.
5. Durability fixes and the cost of uploads during writes
A fourth review by codex looked only at durability: does every FLUSH still survive a power loss? It found four problems. Two came from the freeze without a sync. Two were already in v0.1:
- Chunks found at recovery were not in the list of the next FLUSH. A guest that wrote before a daemon restart, and flushed after it, could lose that write in a later power loss. Now recovered chunks are in the list.
- A move into the cache could expose a torn file. A committed
.frozenfile was moved into the cache without a sync. If a power loss undid the move, recovery would trust the torn file over S3. Now clean-up syncs each frozen file first, outside the chunk locks. - A FUA write could trust a folder sync that was still running (v0.1). The folder state is now a count of changes and a count of synced changes, checked with the chunk locks held.
- Two sync failures did not stop the disk (v0.1): the folder sync of a clean-up, and the sync of a new chunk copy. Now they stop it.
The cost of uploads during writes. v0.2 uploads while the guest writes. A write to a chunk that a checkpoint has frozen must first copy it, and the copy must be synced. In the fio test, the 4 GiB test file is still uploading while fio runs. So 4K writes at queue depth 1 drop from 15.5k to 9.4–9.9k IOPS, and fdatasync from 427 to 112–169 per second. v0.1 never checkpointed during this test (3 minutes, 10 GiB), so it shows no such cost.
Part of the fsync loss was a mistake: after the change above, each freeze counted as a folder change, so each guest FLUSH synced the folder. A rename from .chunk to .frozen needs no folder sync, because recovery takes either name. This is fixed. A separate test now measures guest fio twice: right after a 4 GiB write, while its upload runs, and again when nothing waits for upload.
6. The last fixes
The 20 ms run and a separate fio test showed four more problems. All four are fixed.
- Background fill at 20 ms. 2 fill downloads for each disk gave 22 MiB/s. Now each disk has 8 MiB of fill downloads in flight (32 chunks of 256 KiB). At 20 ms the whole 7.9 GiB disk is now local after 85 s instead of 366 s. This is the link limit.
- One reader of many small files. A read that starts up to 1 MiB after the last read now counts as sequential, so read-ahead starts. At 20 ms, one host reader now takes 126.6 s instead of 140.0 s (v0.1: 136.1 s).
- A FLUSH after scattered writes. With 256 KiB chunks, random writes to a 4 GiB file touch about 16,000 chunk files. The next FLUSH synced them one by one, which took longer than the 15 s fio test. Now a FLUSH of more than 32 chunks does one
syncfsof the data filesystem. - Hot chunks. A background checkpoint now leaves out the chunks written in the last second, because the guest would copy them again on its next write. Detach, snapshot and
mica disk checkpointstill take all chunks. A fifth review by codex found two problems in this and in the durable list of committed chunks, which are fixed.
Guest fio after these fixes, 1 ms round trip. "Nothing waits for upload" runs mica disk checkpoint before each test:
| Guest fio | v0.1 | v0.2, nothing waits for upload | v0.2, while a 4 GiB upload runs |
|---|---|---|---|
| 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 |
4K write + fdatasync | 680 per second | 466 per second | 0 per second |
7. Memory restores with userfaultfd
With the File backend, the kernel loads guest pages from the memory file. With the Uffd backend, a separate process, the handler, gets each page fault of the guest and copies the page in. The handler can also install pages before the guest asks for them. The lab handler has four modes:
- On demand: copy each page at its first fault.
- Record: on demand, and write down each page in the order of its first fault.
- Profile: install a recorded set of pages first, from a second thread, while the guest runs. The set is the pages that two recorded restores both used, in their average order. This is the method that E2B uses.
- Fetch only: read the recorded pages into the page cache (and so into the mica cache) without installing them.
The memory file is on a mica disk in all cases, so a page that is not in the cache comes from S3. Results at 1 ms, v0.2:
| Restore | ssh answers | App answers | S3 download |
|---|---|---|---|
| File backend, cold cache | 0.73 s | 4.82 s | 455 MiB |
| Uffd on demand, cold cache | 0.80 s | 5.29 s | 461 MiB |
| Uffd with profile, cold cache | 0.81 s | 4.77 s | 453 MiB |
| Uffd, fetch only, cold cache | 0.78 s | 4.61 s | 450 MiB |
| File backend, warm cache | 0.23 s | 0.77 s | – |
| Uffd on demand, warm cache | 0.38 s | 3.19 s | – |
| Uffd with profile, warm cache | 0.24 s | 0.84 s | – |
The profile kept 106,921 pages (417 MiB) of the 2 GiB of guest RAM. The two recorded restores used 107,244 and 107,316 pages, so almost all pages that a restore uses are stable.
- Cold, the link is the limit for all backends. This app reads all its 384 MiB of random data, so a restore needs about 450 MiB, which takes 4.6–4.8 s at 95 MiB/s. A profile cannot change how many bytes are needed, only their order. An app that uses a smaller part of its memory at the start would show a larger difference.
- Warm, the handler costs time for each page. On demand, each of the about 100,000 page faults is a round trip to the handler: 3.19 s, against 0.77 s for the File backend. With the profile, the handler installs the pages in 0.27 s before the guest needs them, and the app answers in 0.84 s.
- So, for mica: the File backend is the simple default. A handler with a profile is worth it only when it can do more than the kernel: for example, load pages straight from mica chunks without the filesystem, or start the restore before all chunks arrive.
Decision. mica uses the File backend only, and has no userfaultfd code. mica stays a plain block device that is not tied to Firecracker: the memory file is an ordinary file on a mica disk. With the File backend, many VMs that restore from one snapshot also share the host page cache of the memory file, so they use less host RAM than with a handler, which copies each page into each VM. The lab handler and its test variants are removed.
8. Local storage: data files in place of chunk files
The problem. In v0.2a each local chunk was a file. A checkpoint renamed the file to .frozen, uploaded it, then moved it into the cache. The next guest write to that chunk had to make a new file: a copy, a sync, a rename. The next FLUSH then had to sync the folder, and on btrfs that sync waited behind the checkpoint's own renames. With 256 KiB chunks there are 16 times more such files than with 4 MiB chunks.
The change. Each disk now has sparse data files, one per GiB of the disk, with each local chunk at its disk offset. The guest writes them in place. No write, FLUSH or checkpoint makes, renames or deletes a file.
- A dirty log lists the local chunks. FLUSH syncs the data files that changed, then appends new local chunks to the log, then syncs it. After a power loss, only listed chunks are local, and every other range is punched out. Two log files are used in turn, so one is always whole.
- A bitmap in
/runlists chunks that became local since their last FLUSH./runis a tmpfs: the bitmap survives a crash of mica, as the page cache does, and a reboot clears it, as it clears the page cache. So no write that returned is lost in a daemon crash. Setting a bit is one memory store. A first version appended to a file instead, and that cut 4 parallel writers from 1,651 to 421 MiB/s on btrfs. - A checkpoint reads each chunk into memory and uploads that. The chunk stays in place, and stays local after its upload, so the guest writes it again at once.
- GC safety without frozen files. GC can delete an upload only if a GC was running when the PUT ended. During the upload, mica reads GC activity once a second. An upload that ended before a read with no GC is safe. Until then, the first guest write to its chunk keeps a copy, in memory up to 64 MiB, then in a hold file. The repair after a GC uses the data file or that copy.
- Saved chunks leave the data files only when local data passes
dirty_limit_gib, and at detach. They go into the cache first. - A FUA write syncs its own data file under the same lock as FLUSH. Linux reports a writeback error only once to each open file, so two syncs at the same time could each miss a failure.
It needs only plain Linux file calls: sparse files, fdatasync, copy_file_range (a reflink on btrfs and XFS, a copy on ext4) and hole punching (zeros where a filesystem has none).
Results on the host, 1 ms round trip, v0.2a (per-chunk files) and the new layout one after the other on the same machine state. A Python tool writes 4 KiB with O_DIRECT straight on the mica device:
| Test | v0.1 | v0.2a | v0.2, data files |
|---|---|---|---|
Write + fdatasync, idle disk | 1,301 per second | 1,629 per second | 1,317 per second |
Write + fdatasync, during a 2 GiB upload | 1,150 per second | 554 per second | 636 per second |
Random write + fdatasync to chunks the last checkpoint saved | 1,457 per second | 217 per second | 712 per second |
| Random write to chunks the last checkpoint saved, no sync | 32,762 per second | 1,174 per second | 25,611 per second |
| Random 4K read of saved local chunks, queue depth 1 | – | 56,326 per second | 84,853 per second |
| 4 parallel 1 GiB writers, 1 MiB direct writes | – | 1,651 MiB/s | 1,651 MiB/s |
ext4 on mica: write 7.9 GiB of files (page cache), then sync | – | 13.1 s + 17.0 s | 14.9 s + 13.3 s |
| ext4 on mica: read and hash all files, 16 readers, local data | – | 7.5 s | 5.0 s |
All host read rounds checked every file: 0 bad files in 6 full read-backs.
v0.1 checkpoints only every 180 s, so no upload runs during its 15 s tests. v0.2 uploads at 512 MiB of changes, so its rewrite tests run while chunks upload. With checkpoints off, the new layout does 41,407 rewrites per second without sync, more than v0.1.
In the guest, this host was busy (load average 5 to 18) during these runs, so guest numbers moved by up to 2 times between runs of one build. In the same session, guest fdatasync was 355 per second for both layouts. Right after a 4 GiB write, while its upload ran, the new layout did 68 per second, against none for v0.2a.
Reviews. Five rounds of review by codex found 23 problems in this change, all fixed. The last round found one. Most were orderings between FLUSH, the log rewrite and the freeing of space, and crash windows in the log and the bitmap. Codex also found three gaps that were there before: a GC that outlived the 10-minute wait, two GCs that could hide each other, and a stale GC lock that two GCs could both take. With conditional writes, the GC lock is now an atomic create, and a stale lock needs an operator. Without conditional writes, two GCs at once remain a gap.
Rollback is by binary, not by a setting. The S3 format is the same. A new build refuses a disk folder in the old layout, so a disk is detached with the old build first.
9. Crash test
lab/bench/crash.sh runs a second mica daemon whose data folder sits on a device-mapper target. A writer fills a 256 MiB area of a disk with 4 KiB blocks that each name their block, the write that made them, and a CRC. It FLUSHes or writes with FUA now and then, and logs each write and each completed FLUSH. At a random moment (2 to 20 s, while checkpoints run every 3 s) the writer stops, and the target drops every write from then on: a power cut. Then the daemon dies, its ublk device is deleted and the folder is mounted again, as after a reboot. The new daemon uploads what is left and detaches. The disk is attached again, and every block must hold its last write before the last completed FLUSH (or a FUA write), or a later one. A second mode kills only the daemon while the writer runs. The next daemon takes the device over, and every write that returned must be there, flushed or not.
| Mode | Data folder | Runs | Runs that lost data |
|---|---|---|---|
| Power cut | btrfs | 6 | 0 |
| Power cut | XFS | 6 | 0 |
| Power cut | ext4 | 6 | 0 |
| Daemon killed, guest writes on | btrfs | 6 | 0 |
The first run found a bug that was there since v0.1: the node ID file was written without a sync. After a power cut it was empty, so the node took its own disks for another node's and kept their data as orphaned. Now the file is synced and renamed into place, and an empty ID stops the daemon with an error. A first version of the harness also counted FLUSHes that ended after the cut as durable; the writer now stops before the cut, as power would stop it.
10. FLUSH: early writeback, and the idle gap
The problem after a large write. Linux starts writeback late: after 10% of free memory is dirty (about 1.3 GB on this host), or after 30 s. After a 2 GiB write with no FLUSH, almost all of it was still in the page cache, so the next FLUSH wrote all of it. With the lab's 300 MiB/s disk that FLUSH took 3.6–4.7 s, in every build. On a 125 MiB/s cloud disk it would take about 16 s.
The change. After each 16 MiB of writes, mica starts writeback of the data files that the next FLUSH must sync, with sync_file_range(SYNC_FILE_RANGE_WRITE). The call does not wait, makes nothing durable, and does not take the writeback error that the FLUSH fdatasync must see. It works on any Linux filesystem.
Results on the host, the mean of 3 rounds, with the builds alternating. Each run started only when the host's I/O pressure was under 2%:
| Test | v0.2a | v0.2, data files | v0.2, data files + early writeback |
|---|---|---|---|
Write + fdatasync, idle disk | 1,515 per second (p50 574 µs) | 1,473 per second (p50 624 µs) | 1,438 per second (p50 631 µs) |
| Longest FLUSH right after a 2 GiB write | 3.6–4.6 s | 3.6–4.7 s | 0.46–0.95 s |
Write + fdatasync, during the 2 GiB upload | 586 per second | 611 per second | 695 per second |
Random write + fdatasync to chunks the last checkpoint saved | 265 per second | 437 per second | 584 per second |
| Random write to chunks the last checkpoint saved, no sync | 512 per second | 19,985 per second | 19,290 per second |
The idle gap is 3%, not 19%. The 19% came from runs while the host was busy. On a quiet host the data files are 3% slower in rate, and 50 µs slower at p50. Timers in the daemon put almost all of a FLUSH in the data file's fdatasync (p50 550 µs). mica's own steps take about 25 µs. A test without mica shows the same 50 µs on this btrfs: a synced 4 KiB write into a large file costs more than one into a new 256 KiB file, with 1 GiB and 64 MiB files alike. So the gap comes from btrfs, for a guest that syncs each 4 KiB write, and smaller data files do not close it.
Tried and reverted: RustFS keeps its data on the same btrfs as the lab daemon, and its writes slow the daemon's syncs: a plain fdatasync loop ran at 0.64 of its quiet rate during a 2 GiB upload. RustFS on its own XFS, on a loop device, gave 0.65, because the loop file is on the same btrfs and the same SSD. The lab keeps its old setup.
11. Limits for each disk, and online growth
The problem. v0.2 had one cache for all disks on a host. A small disk and a large disk shared it, so one busy disk could push the chunks of all other disks out. Some uses need a large disk with a small local footprint, for example a 50 GB disk with 500 MB of cache. Also, a disk could grow only while it was detached.
The change. Each disk now has two budgets. mica stores both in the disk's head object in S3, so they move with the disk to a different node:
- Cache budget (
--cache-size): clean chunks that S3 also has. The default isdisk_cache_limit_mib, 1 GiB. - Pending-write budget (
--dirty-size): local chunks that S3 does not have yet. When it is full, writes wait until a checkpoint uploads chunks.
The two budgets are separate, so writes can continue when the cache is full. mica disk set-limits changes them, also while the disk is attached. The host limit (cache_limit_gib) and a free-space reserve (local_reserve_mib) are now only a safeguard against a full SSD.
A reconciler runs once a second. It frees local chunks that a checkpoint uploaded, and trims each cache to its budget. When the host total is too high or free space is low, it trims caches until the shortfall is gone. It measures free space once per pass: btrfs shows deleted files as free only after its next commit, so a new measurement after each cache made a first version empty every cache.
Online growth. mica disk resize now grows an attached disk. mica grows its local data files, commits a larger root, then tells the kernel the new size with the ublk command UPDATE_SIZE. If the kernel step fails, the same command again finishes it. A disk cannot get smaller.
Live test in the lab, 1 ms round trip:
| Test | Result |
|---|---|
| Grow an attached 1 GiB disk to 3 GiB | Kernel device size went from 2,097,152 to 6,291,456 sectors at once |
| Make it smaller | Refused |
| Data before and after growth, after checkpoint, detach and attach | All blocks correct. New space reads as zeros |
| Write 300 MiB, then read it back, with a 64 MiB cache and a 32 MiB pending-write budget | Byte-exact. Cache stayed under 64 MiB. Local data fell to 20 KiB after the upload |
| Delete a disk | Its cache folder is removed (a first version left it on the host) |
12. Audit fixes, and cold reads of several chunks
A code audit by codex found 11 problems, all fixed:
- Writes that wait for space now fail when the disk loses ownership. Before, they could wait forever.
- Cache eviction deletes files outside the lock that reads use, in small batches.
- The queue of saved chunks keeps one entry for each chunk. Before, each checkpoint could add the same chunk again.
- Two equal pages that save at the same time use different temporary files.
- A checkpoint hashes a chunk first, and compresses it only when it must upload it.
- Resize, delete and attach of one disk take the same lock, and a detached resize or delete takes ownership in S3 first.
- Recovery at startup tries again when S3 is not available.
- Background fill streams the metadata pages and keeps a fixed number of tasks, so its memory does not grow with the disk.
- Shutdown checkpoints disks in parallel, with one deadline for all.
- A read that needs several chunks downloads them in parallel. Before, it downloaded one chunk, then the next.
Cold reads, old and new build alternating, 20 ms round trip, empty cache, no background fill, no prefetch, 256 KiB chunks. Each 1 MiB read needs 4 chunks:
| Run | Sequential 128 MiB, 1 MiB reads, queue depth 1 | 64 random 1 MiB reads, queue depth 1 |
|---|---|---|
| Before | 67.1 MiB/s | 62 ms each |
| After | 72.2 MiB/s | 39 ms each |
| Before | 72.7 MiB/s | 61 ms each |
| After | 73.7 MiB/s | 46 ms each |
- Random cold reads are about 30% faster, from 61 to 42 ms. The limit is about 30 ms: one 20 ms round trip, then 10 ms to move 1 MiB at 100 MiB/s.
- Sequential cold reads do not change. The read-ahead already downloads the next chunks before the kernel asks for them.
This was a short test of 2 runs per build. The full suite (fill, upload, all read rounds, FLUSH) was not run again for these changes.
Open problems
- A FLUSH during a large upload still shares the SSD with the upload's writeback and RustFS. Early writeback took the longest FLUSH from 4.7 s to under 1 s (section 10).
- On btrfs, a synced 4 KiB write costs 50 µs more in a data file than in a chunk file (section 10).
- A page that GC deletes between a commit and its repair is lost if the node loses power in that window. This gap was in v0.2a too.
- Firecracker does not pass discard to the disk. Space that a guest frees stays in mica and in S3. This is a limit of the VMM, not of mica.
Not done yet
- ublk zero-copy (the last step of the plan).
- The full suite with the changes of sections 11 and 12, and a run with a small cache on a large disk.