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.

Measured 25–29 October 2026 · baseline optimizations

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:

ItemValue
Host16 CPUs, 27 GiB RAM, NVMe SSD, Fedora 44, Linux 7.0
micav0.1 (commit 3e56460), release build
mica settingscache_limit_gib = 150, dirty_limit_gib = 40, checkpoint_interval_secs = 180
S3 link100 MiB/s down, 100 MiB/s up, 1 ms round trip
Local disk limit300 MiB/s read, 300 MiB/s write, no IOPS limit

Raw capacity of the lab

These numbers show the limits that mica works within.

TestToolResult
S3 round tripping1.06 ms
S3 PUT, 4 MiB objects, 1 at a timewarp69 MiB/s (58 ms each)
S3 GET, 4 MiB objects, 1 at a timewarp91 MiB/s (44 ms each)
S3 PUT / GET, 4 MiB objects, 4–32 at a timewarp94–96 / 96–97 MiB/s
S3 upload of one 1 GiB object, 4–16 streamss5cmd11.2 s (≈ 92 MiB/s)
Disk 1M sequential read / writefio, direct I/O299 / 300 MiB/s
Disk 4K random read / write, queue depth 32fio, direct I/O76k / 19.7k IOPS
Disk 4K random read / write, queue depth 1fio, direct I/O15.4k IOPS, 64 µs / 19.8k IOPS, 49 µs
Disk 4K write with fdatasyncfio1,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.

ClassFilesSize of each fileTotal
Large4864 MiB3,072 MiB
Medium2,000256 KiB – 2 MiB2,274 MiB
Small40,0004 KiB – 128 KiB2,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

StepTimeNote
mica disk create (20 GiB)0.04 s
First attach and mount1.14 sIncludes the ext4 format
Write 7.93 GiB of files13.9 s572 MiB/s, into the page cache
sync16.0 sThe 300 MiB/s disk limit applies here
Record checksums4.9 sFrom the page cache
Unmount0.5 sIncludes fstrim
mica disk detach87.2 sUploads 8.0 GiB: ≈ 94 MiB/s
Start of the write until all data is in S3122.4 s
Write and upload of the 7.93 GiB data set, sampled every second. The upload starts only at the detach.

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.

TestColdWarm
Attach and mount1.29–1.31 s1.14 s
First byte of a 64 MiB file0.16–0.17 sunder 0.01 s
Read one 64 MiB file0.72–0.73 s (≈ 89 MiB/s)0.10 s
Read 100 small files, one after the other0.57–0.59 s0.10–0.11 s
Read and check all files, 1 reader115.2 s (69 MiB/s)26.8 s (296 MiB/s)
Read and check all files, 8 readers83.8 s (95 MiB/s)26.8 s (296 MiB/s)
Read and check all files, 32 readers83.2 s (95 MiB/s)–
Unmount and detach, no file changes0.96–1.08 s0.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

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).

TestTimeS3 download
Make the disk: disk create + mke2fs -d with the root filesystem3.9 s–
First detach (uploads the root filesystem)5.7 s–
Cold boot, empty cache4.61 s≈ 250 MiB
Warm boot, all chunks in the cache3.29 s0
Cold boot of the same disk after the 8 GiB fill5.5–7.9 s≈ 200–400 MiB
Snapshot of the disk / clone from the snapshot0.02 s / 0.04 s–
Boot of the clone (the cache already has the chunks)3.35 s0
Guest reboot until Firecracker exits0.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.

TestResult
4K random read / write, queue depth 3248k IOPS / 100k IOPS
4K random read / write, queue depth 124.7k IOPS, 40 µs / 15.5k IOPS, 64 µs
1M sequential read / write, queue depth 169.7 GiB/s / 3.7 GiB/s (host page cache)
4K write with fdatasync427 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.

StepTime
Write 7.93 GiB of files in the guest17.1 s
sync in the guest15.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 S3169.5 s
Cold boot, then read and check all files in the guest, 1 reader7.9 s + 92.3 s
Cold boot, then read and check all files in the guest, 8 readers5.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

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

  1. Pause the VM.
  2. Write the snapshot files to an ext4 filesystem on a second mica disk (4 GiB). The memory file is 2,048 MiB.
  3. Make a mica snapshot of the root disk while the VM is paused. Thus the disk and the memory are from the same moment.
  4. Detach both disks. Each restore starts from a new clone of the root disk snapshot.
StepTimeNote
Pause0.01 s
Firecracker snapshot to the mica disk5.81 s2,048 MiB memory file
mica snapshot of the root disk6.01 sIncludes a checkpoint of the root disk
Detach the memory disk10.8 sUploads 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.

VariantAttachLoadssh answersApp answersS3 download
Cold cache2.43 s0.23 s2.38 s6.71 s630 MiB
Cold cache, with mica read-profile prefetch (512 chunks)2.84 s0.03 s2.40 s5.87 s630 MiB
Warm cache2.22 s0.02 s0.20 s0.63 s3 MiB
mica disk warm of the memory disk first (10.5 s), then restore2.27 s0.02 s0.21 s0.65 s27 MiB

What these numbers show

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 sizePUT, 1 at a timePUT, 8PUT, 32GET, 1 at a timeGET, 8
64 KiB13.8 MiB/s (4.6 ms)81 MiB/s94 MiB/s46 MiB/s (1.3 ms)95 MiB/s
256 KiB38 MiB/s (6.6 ms)85 MiB/s95 MiB/s95 MiB/s (2.6 ms)95 MiB/s
4 MiB65 MiB/s (62 ms)94 MiB/s96 MiB/s95 MiB/s (42 ms)97 MiB/s

The same GET test with a 20 ms round trip, which is closer to an S3 service in the same region:

Object sizeGET, 1 at a timeGET, 8 at a timeGET, 32 at a time
64 KiB2.9 MiB/s (21.5 ms)24 MiB/s95 MiB/s
256 KiB11.6 MiB/s (21.7 ms)95 MiB/s96 MiB/s
4 MiB65 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.

Testv0.1v0.2
Attach of one disk (p50 of 10)1,035 ms24 ms
4 disks attached at the same time4.2 s0.05 s
Cold Firecracker boot, Ubuntu: attach until ssh answers4.61 s2.33 s
Warm Firecracker boot, Ubuntu / Alpine3.29 s / –2.28 s / 1.44 s
Cold boot of a disk after an 8 GiB fill5.5–7.9 s2.39 s
Upload of a new root filesystem (Ubuntu, 270 MiB of files)5.7 s1.5 s
Cold memory restore: ssh answers2.38 s0.64 s
Cold memory restore: app answers (384 MiB of random data in RAM)6.71 s4.63 s
Cold memory restore: attach of root disk and memory disk2.43 s0.26 s
Cold memory restore: S3 download630 MiB453 MiB
Memory disk in S3 (2 GiB memory file)996 MiB307 MiB
Warm memory restore: ssh / app answers0.20 / 0.63 s0.23 / 0.71 s
Host: 7.9 GiB write until all data is in S3122.4 s111.3 s
Guest: 7.9 GiB write until all data is in S3169.5 s148.6 s
Host: first byte of a 64 MiB file / 100 small files, cold0.16 s / 0.57 s0.01 s / 0.28 s
Host: read and check 7.9 GiB, 1 reader, cold115.2 s87.6 s
Host: read and check 7.9 GiB, 8 readers, cold83.8 s82.4 s
Host: whole disk local after a cold attach– (no fill)84 s
Guest fio 4K write + fdatasync, while an upload runs427 per second112–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.1v0.2
Cold Firecracker boot, Ubuntu: attach until ssh answers7.46 s3.96 s
Cold boot of a disk after the 8 GiB fill8.2–9.5 s4.0 s
Warm boot, Ubuntu3.52 s2.37 s
Cold / warm boot, Alpine4.01 s / 2.64 s2.68 s / 1.51 s
Cold memory restore: attach / ssh answers / app answers3.02 / 3.27 / 8.04 s0.57 / 1.56 / 5.77 s
Warm memory restore: ssh / app answers0.21 / 0.67 s0.20 / 0.64 s
Memory disk upload996 MiB in 11.3 s361 MiB in 7.9 s
Host: attach and mount1.65 s0.18–0.25 s
Host: first byte of a 64 MiB file / 100 small files, cold0.21 s / 0.96 s0.03–0.10 s / 0.49–0.67 s
Host: 7.9 GiB write until all data is in S3119.9 s117.9 s
Guest: 7.9 GiB write until all data is in S3173.2 s147.5 s
Host: read and check all files, 1 reader, cold136.1 s140.0 s
Guest: read and check all files, 1 reader, cold149.6 s109.6 s
Read and check all files, 8 readers, cold (host / guest)85.7 / 89.4 s86.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 1388/s / 16.4k IOPS205/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:

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.

StepMetricBefore (v0.1)After
1. AttachAttach of one disk, p50 of 101,035 ms24 ms
1. Attach4 disks attached at the same time4,160–4,210 ms45–64 ms
1. AttachWarm Firecracker boot, attach until ssh answers3.29 s2.33–2.39 s
2. Small chunksCold Firecracker boot, attach until ssh answers4.61 s2.80 s
2. Small chunksS3 download during a cold boot≈ 250 MiB136 MiB
2. Small chunksCold boot after the 8 GiB fill5.5–7.9 s3.63 s
2. Small chunksHost: first byte of a 64 MiB file, cold0.16 s0.01 s
2. Small chunksHost: 100 small files one by one, cold0.57 s0.21–0.24 s
2. Small chunksHost: read and check all files, 1 reader, cold115.2 s98.9 s
2. Small chunksHost: write 7.93 GiB of files (to the page cache)13.9 s6.0 s
2. Small chunksGuest fio 4K random read / write, queue depth 3248k / 100k IOPS129k / 116k IOPS
2. Small chunksMemory restore, cold: attach / ssh answers / app answers2.43 / 2.38 / 6.71 s0.30 / 1.89 / 6.19 s
2. Small chunksMemory restore, cold: S3 download630 MiB555 MiB
2. Small chunksMemory disk upload (2 GiB memory file)996 MiB943 MiB
3. PrefetchHost: read and check all files, 1 reader, cold115.2 s87.3 s
3. PrefetchHost: whole disk local after a cold attach (background fill)–84 s (link limit)
3. PrefetchGuest: read and check all files, 1 reader, cold92.3 s88.3 s
3. PrefetchClone: read and check all files, 8 readers26.5 s23.2 s
3. PrefetchMemory restore, cold: app answers / S3 download6.71 s / 630 MiB6.60 s / 652 MiB (worse than step 2)
4. CompressionCold Firecracker boot (Ubuntu), attach until ssh answers4.61 s2.35 s
4. CompressionMemory disk upload (2 GiB memory file)996 MiB in 10.8 s358 MiB in 4.6 s
4. CompressionMemory restore, cold: ssh answers2.38 s0.55 s
4. CompressionMemory restore, cold: app answers / S3 download6.71 s / 630 MiB4.66 s / 451 MiB
4. Continuous checkpointsGuest: start of the 8 GiB write until all data is in S3169.5 s139.3 s
4. Continuous checkpointsGuest fio: 4K write + fdatasync427 per second263 per second (worse)
8. Local storageHost: random 4K writes to saved chunks (before: v0.2a)1,174 per second25,611 per second
8. Local storageHost: same, with fdatasync after each (before: v0.2a)217 per second712 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.

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.

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.

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.

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.

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:

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.

Guest fio after these fixes, 1 ms round trip. "Nothing waits for upload" runs mica disk checkpoint before each test:

Guest fiov0.1v0.2, nothing waits for uploadv0.2, while a 4 GiB upload runs
4K random read, queue depth 32166.5k IOPS159.7k IOPS47.3k IOPS
4K random read, queue depth 126.8k IOPS27.8k IOPS20.9k IOPS
4K random write, queue depth 32124.8k IOPS110.0k IOPS100.0k IOPS
4K random write, queue depth 116.5k IOPS11.5k IOPS12.8k IOPS
4K write + fdatasync680 per second466 per second0 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:

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:

Restoressh answersApp answersS3 download
File backend, cold cache0.73 s4.82 s455 MiB
Uffd on demand, cold cache0.80 s5.29 s461 MiB
Uffd with profile, cold cache0.81 s4.77 s453 MiB
Uffd, fetch only, cold cache0.78 s4.61 s450 MiB
File backend, warm cache0.23 s0.77 s–
Uffd on demand, warm cache0.38 s3.19 s–
Uffd with profile, warm cache0.24 s0.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.

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.

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:

Testv0.1v0.2av0.2, data files
Write + fdatasync, idle disk1,301 per second1,629 per second1,317 per second
Write + fdatasync, during a 2 GiB upload1,150 per second554 per second636 per second
Random write + fdatasync to chunks the last checkpoint saved1,457 per second217 per second712 per second
Random write to chunks the last checkpoint saved, no sync32,762 per second1,174 per second25,611 per second
Random 4K read of saved local chunks, queue depth 1–56,326 per second84,853 per second
4 parallel 1 GiB writers, 1 MiB direct writes–1,651 MiB/s1,651 MiB/s
ext4 on mica: write 7.9 GiB of files (page cache), then sync–13.1 s + 17.0 s14.9 s + 13.3 s
ext4 on mica: read and hash all files, 16 readers, local data–7.5 s5.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.

ModeData folderRunsRuns that lost data
Power cutbtrfs60
Power cutXFS60
Power cutext460
Daemon killed, guest writes onbtrfs60

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%:

Testv0.2av0.2, data filesv0.2, data files + early writeback
Write + fdatasync, idle disk1,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 write3.6–4.6 s3.6–4.7 s0.46–0.95 s
Write + fdatasync, during the 2 GiB upload586 per second611 per second695 per second
Random write + fdatasync to chunks the last checkpoint saved265 per second437 per second584 per second
Random write to chunks the last checkpoint saved, no sync512 per second19,985 per second19,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:

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:

TestResult
Grow an attached 1 GiB disk to 3 GiBKernel device size went from 2,097,152 to 6,291,456 sectors at once
Make it smallerRefused
Data before and after growth, after checkpoint, detach and attachAll 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 budgetByte-exact. Cache stayed under 64 MiB. Local data fell to 20 KiB after the upload
Delete a diskIts 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:

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:

RunSequential 128 MiB, 1 MiB reads, queue depth 164 random 1 MiB reads, queue depth 1
Before67.1 MiB/s62 ms each
After72.2 MiB/s39 ms each
Before72.7 MiB/s61 ms each
After73.7 MiB/s46 ms each

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

Not done yet

Sources