mica storage design

How mica keeps a disk in an object store: the layout, the write path, the read path, transactions and snapshots. Each disk owns its objects. The node that has the disk attached cleans up after itself.

Design, not built. Fourth version, 2026-11-07.

1. Words

mica gives a virtual machine a block device. The data of the device lives in an object store with the S3 interface. The node keeps a cache and the newest writes on its local disk.

This document uses each word below with one meaning only.

WordMeaning
diskThe block device that mica gives to the guest.
guestThe virtual machine or program that uses the disk.
nodeA host that runs the mica service.
local diskThe disk of the node, where mica keeps its cache and the newest writes.
chunkA fixed part of the disk. 256 KiB by default, set when the disk is made.
pageA list of 1,024 chunk entries. One page covers 256 MiB of disk.
rootA list of page entries. It describes one full version of the disk.
entryThe place of an object and its hash. Section 3 shows its fields.
headThe object that names the current version, the owner and the snapshots.
transactionOne upload of the changes of the disk and one change of the head. It makes a new version. Each transaction has a number. The number grows by one each time.
birthThe number of the transaction that wrote an object.
snapshotA version that mica keeps, with a name. Other versions go away at the next transaction.
ownerThe one node that has the disk attached. Only the owner changes the head and deletes objects of the disk.
local chunkA chunk whose current content is on the local disk.
unsaved chunkA local chunk whose content is not in the object store yet.

2. Decisions

3. Layout in the object store

d/<disk>/head                                   the head
d/<disk>/<transaction>/root                      root of that version
d/<disk>/<transaction>/p/<page index>            a page
d/<disk>/<transaction>/c/<chunk index>           chunk data

Each transaction writes its objects into its own folder. The folder name is the transaction number in 16 hexadecimal digits, so folders sort by number. mica never writes a key two times. A key names one object for its whole life.

Example for disk web-1, after transaction 42 changed chunk 7 in page 0:

d/web-1/head
d/web-1/000000000000002a/root
d/web-1/000000000000002a/p/0
d/web-1/000000000000002a/c/7
d/web-1/0000000000000011/c/1500      written by transaction 17, still current

Entry

A page holds 1,024 chunk entries. A root holds one page entry for each page of the disk. An entry is 40 bytes:

birth   u64        number of the transaction that wrote the object
hash    [u8; 32]   SHA-256 of the plain data of the object

The key of an object comes from the disk, the birth and the position of the entry. An entry with all bytes zero is a zero chunk or a zero page. mica does not store zeros.

Chunk object

One zstd frame, level 1. mica decompresses it and compares its SHA-256 with the entry. If they differ, mica rejects the data.

Page object

magic     "MPAG"
count     u32               1024
entries   [Entry; 1024]

One zstd frame, level 3. Page k holds chunks 1024 × k to 1024 × k + 1023.

Root object

magic        "MICA"
version      u32
chunk_size   u32                 a power of two, 64 KiB to 4 MiB
disk_size    u64                 a multiple of chunk_size
transaction  u64                 the transaction that wrote this root
parent       u64                 the transaction before it, 0 for the first
time         u64                 Unix time of the transaction
pages        [Entry; page count]

A root of a 100 GiB disk is about 16 KiB. A root of a 1 TiB disk is about 160 KiB. The root is not compressed, so a range read can get the disk size.

Head object

{
  "transaction": 42,
  "next": 43,
  "owner": { "node": "node-23", "node_identifier": "...", "since": "2026-11-07T10:00:00Z" },
  "lost": [],
  "snapshots": {
    "before-upgrade": { "transaction": 41 },
    "nightly-11-06":  { "transaction": 30, "deleting": true }
  },
  "size": 107374182400,
  "limits": { "cache_bytes": 536870912, "unsaved_bytes": 4294967296 },
  "profile": [0, 1, 812, 813, 81920],
  "deleted": false
}
FieldMeaning
transactionThe current version. Its root is d/<disk>/<transaction>/root.
nextThe folder for the next transaction. Each transaction sets it, so a transaction needs only one write of the head.
ownerThe node that has the disk attached. Empty when the disk is detached.
lostFolders of transactions that never finished. The owner deletes them.
snapshotsName and transaction of each snapshot. deleting marks a snapshot whose objects mica is deleting now.
size, limitsDisk size, and the cache and unsaved data budgets. They follow the disk between nodes.
profileThe chunks that the guest read first in its last session. Attach downloads them first.
deletedThe disk is being deleted.

4. Layout on the node

/var/lib/mica/
  node-identifier                          random identifier of this node
  disk-cache/<disk>/<first 2 hex>/<hash>     clean chunks, by hash, with a budget for each disk
  disks/<disk>/
    state                                  attached or orphaned
    pages/<transaction>/<page index>        pages, not compressed
    dirty/data-<n>                         sparse data files: local chunks at their disk offset, 1 GiB each
    dirty/log-a, dirty/log-b               the dirty log: which chunks are local
/run/mica/sessions/<data folder>/<disk>.recent   one bit for each chunk that became local since the last log write

In memory, mica keeps one slot for each chunk of each page that the guest has used:

struct ChunkSlot {
    base: Entry,       // the chunk in the current version, zero for zeros
    local: bool,       // the data file holds the current content
    unsaved: bool,     // the local content can differ from base
    staged: bool,      // the running transaction read the chunk
    changed: bool,     // the guest wrote the chunk after that read
    verify: bool,      // found in the dirty log after a restart
}

5. The owner and the head

The head is the only object that mica writes again. Each write of the head is conditional:

If a conditional write fails, another node changed the head. The write does not happen.

Rules

  1. A disk has one owner at a time.
  2. Only the owner changes the head of an attached disk. A request from another node gets the answer "disk web-1 is attached on node-23". The controller sends the request to that node.
  3. Only the owner deletes objects of the disk.
  4. To change a detached disk, a node first becomes the owner. It gives the disk back after.

Rule 2 means that a failed conditional write always means a takeover. The owner then stops, as section 13 explains.

Claim

  1. Read the head and its ETag.
  2. If owner names another node, stop with an error. A forced claim continues. Use it only when the other node is off.
  3. If the folder next holds objects, a transaction did not finish. Add next to lost.
  4. Write the head with our owner and next + 1 as the new next, with If-Match.

Step 4 makes sure that the new owner never writes into the folder of the old owner. After a forced claim, the old owner can upload into its folder for a short time. Its write of the head fails, so it can never commit. The new owner deletes that folder at the claim and again at its detach.

The owner reads the head once a minute. So an idle disk also sees a takeover.

6. Write path

A guest write goes to the local disk. It never waits for the object store, except in one case in step 3.

  1. Split the request by chunk.
  2. Lock the chunk.
  3. If the chunk is not local, put its current content into the data file first. Copy it from the cache, or download it. Skip this if the base is zero, or if the write covers the full chunk.
  4. Mark the chunk unsaved. If the running transaction read it, mark it changed.
  5. Write the data into the data file.
  6. If the dirty log does not list the chunk, set its bit in the recent bitmap.
  7. Answer the guest. Do not sync.

Flush

The device tells the kernel that it has a volatile write cache. So the kernel sends a flush when the guest needs its writes to be durable.

  1. Sync the data files that changed since the last flush.
  2. Append the new local chunks to the dirty log, and sync the log.
  3. Answer the guest.

A flush never touches the object store. After a flush, a power loss of the node loses nothing that the guest flushed. A loss of the whole local disk loses the unsaved chunks: up to one transaction interval of writes, or more when uploads are behind.

A write with the forced-unit-access flag does the same steps for its own chunks only. After each 16 MiB of writes, mica starts the write-back of the data files early, so a later flush has less to wait for.

Discard

7. Transaction

A transaction makes the object store hold a new version of the disk. It collects all writes since the last transaction. It is not one write: a chunk that the guest writes 1,000 times between two transactions is uploaded one time.

When a transaction starts

An idle disk makes no transactions and sends no requests. One transaction runs at a time for each disk. A busy disk makes about 2 transactions each minute.

Steps

  1. Take the unsaved chunks. A timed transaction leaves out chunks written in the last second, because the guest will probably write them again.
  2. Upload each chunk, with up to 64 MiB in flight for each disk:
    1. Lock the chunk, read it into memory, mark it staged, and unlock it. The guest can write it again at once.
    2. Compute its SHA-256.
    3. If the data is all zeros, the new entry is zero. Upload nothing.
    4. If the hash equals the hash of the base, keep the base entry. Upload nothing.
    5. Else compress it and write d/<disk>/<next>/c/<index>. The new entry is (next, hash).
  3. Upload each changed page to d/<disk>/<next>/p/<index>.
  4. Upload the root to d/<disk>/<next>/root, with the current transaction as its parent.
  5. Commit. Write the head with transaction = next, next + 1, the new profile, and If-Match. This one write makes the new version current.
  6. Finish on the node. Each staged chunk that the guest did not change since step 2 is now saved. Its base becomes the new entry.
  7. Free. Delete the garbage with the free rule (section 8), in the background.

If the commit fails

If a transaction fails for a network error, the chunks stay unsaved on the local disk, and mica tries again later. If the oldest unsaved write is older than 5 minutes, mica shows the disk as behind.

Cost of one transaction

One write for each changed chunk, one for each changed page, one for the root, and one for the head. Then one DeleteObjects request for each 1,000 deleted keys. A 4 KiB write to a 100 GiB disk makes a transaction of one chunk, one page, a root of 16 KiB, and the head.

8. Free rule

A transaction replaces some entries. The old entry of each changed chunk and page, and the old root, are now replaced. The transaction has these old entries in memory.

Free rule. After a commit, delete each replaced object whose birth is after the newest snapshot. Keep each replaced object whose birth is at or before the newest snapshot. That snapshot uses it.

ZFS uses the same rule for its blocks. It needs no reads from the object store. mica deletes up to 1,000 keys with one DeleteObjects request. It deletes the replaced root last.

B is deleted at once. A and C stay because a snapshot uses them. A delete of S2 frees C (section 11).

Why it is correct: an object is in one continuous range of versions. It appears in the transaction that wrote it. It stays in each later version until the guest writes that chunk again. It never comes back, because a key is never used again and there is no rollback. So if a replaced object has its birth after the newest snapshot, no snapshot has it.

Deletes can lag behind. Transaction 43 can commit while the deletes of 42 still run. mica deletes old roots in order, oldest first. Section 14 shows how the next owner finishes deletes after a crash.

9. Read path

  1. Split the request by chunk.
  2. If the chunk is local, read it from the data file.
  3. If the base entry is zero, return zeros.
  4. If the cache of this disk has the hash, read the cache file.
  5. If the cache of another disk on the node has the hash, link that file into the cache of this disk, and read it.
  6. Else download d/<disk>/<birth>/c/<index>. Decompress it, check its SHA-256, write it to the cache without a sync, and read it.

The page of a chunk loads on first use: from disks/<disk>/pages/ on the node, else from the object store. The queue thread never waits for a page. A read whose page is not loaded goes to the engine, which loads the page.

Downloads before the guest asks

Guest reads always go first. The node allows 64 downloads for guest reads, 16 for the profile and read-ahead, and 32 for the fill, with at most 128 in total.

10. No duplicate transfers

Uploads

CaseWhat mica does
The guest writes a chunk many times between two transactionsOne upload, in the next transaction.
The guest is still writing a chunkA timed transaction leaves out chunks written in the last second.
The guest writes the same data againThe hash equals the base. The entry stays. No upload.
The chunk is all zeros, or was discardedA zero entry. No upload.
A page did not changeNo upload. The root keeps its old page entry.
A transaction fails and tries againIt deletes its own uploads first. Nothing stays two times.

Downloads

CaseWhat mica does
Any cache on the node has the hashLink the file. No download.
Many reads miss the same chunk at the same timeOne download. The other reads wait for it.
The guest writes a full chunkNo download of the old content.
A restore on the same nodeThe copies have the same hashes, so the cache has their chunks already.
Many disks from one image on one nodeEach chunk is downloaded one time for the node.

The object store keeps equal data one time for each disk. The node keeps it one time for each hash, while a cache has it.

11. Snapshots

A transaction makes a new version, and the free rule then removes the old version. A snapshot is a name in the head that keeps one version. A snapshot holds no data of its own. It changes only the free rule: objects with a birth at or before the newest snapshot stay.

transaction 40   commit                      version 40 is current
transaction 41   commit                      replaced objects with birth after the newest snapshot are deleted
transaction 42   snapshot "before-upgrade"   the head now has before-upgrade = 42
transaction 43   commit                      replaced objects with birth 42 or older stay
transaction 44   commit                      replaced objects with birth 43 are deleted

Create

A snapshot is a transaction whose write of the head also adds the name. So the new version and its name appear in one write. If no chunk changed, the transaction uploads nothing and only writes the head.

The owner makes the snapshot. For a detached disk, a node claims the disk first and gives it back after.

Delete

The snapshots and the current version form one list, ordered by transaction. To delete snapshot S, find the snapshot before it (P), and the next snapshot or the current version (N).

Merge rule. Delete each object of S that P and N do not have.

  1. Write the head with deleting set on S. A restore from S is no longer possible.
  2. Compare the roots of P, S and N. Equal page entries mean equal pages, so read only the pages that differ.
  3. Delete each chunk, page and root of S that P and N do not have.
  4. Write the head without S.

Why it is correct: an object is in one continuous range of versions. If another snapshot has the object, the neighbor on that side has it too. So the two neighbors are enough.

If the node crashes after step 1, the head still marks S as deleting. The next owner does steps 2 to 4 again. A delete can run two times without harm.

A controller can keep its own database of snapshots. It removes a snapshot from its database first, then asks the owner to delete it. The head stays the truth about which objects exist.

Restore

mica has no rollback. A restore makes a new disk from a snapshot. The old disk and all its snapshots stay as they are.

  1. The owner of the source disk does the restore. For a detached source, the node claims it first.
  2. Create the head of the new disk with If-None-Match: *, with this node as owner.
  3. Copy each chunk, page and root of the snapshot into the new disk with CopyObject. Keep the same transaction folders. The data stays inside the object store.
  4. Write the head of the new disk: the transaction of the snapshot as current, and no snapshots.

A snapshot delete waits for restores from it. Both run on the owner, so no other lock is needed.

An estimate for 2 GiB of written data at 256 KiB chunks: about 8,200 copies, about 3 seconds with 64 copies at a time. The time grows with the data in the snapshot, not with the disk size.

Why no rollback: a rollback would put old objects back into the current version. Then an object could be in an old snapshot and in the current version, but not in the snapshots between them. A merge could then delete it. A copy has new keys, so this cannot occur.

A version that is not a snapshot is gone after the next transaction. To restore to a point in time, take a snapshot at that time.

12. Delete a disk

  1. Claim the disk. Write the head with deleted set.
  2. List d/<disk>/ and delete everything except the head, 1,000 keys for each request.
  3. Delete the head.

The snapshots of the disk go with it. If the node crashes, the head with deleted stays. mica disk ls shows the disk as "deleting", and the same command finishes it.

13. Attach and detach

Attach

  1. Claim the disk (section 5).
  2. Read the root of the current version.
  3. Open disks/<disk>/dirty/. Each chunk in the dirty log, or in a trusted recent bitmap, is local.
  4. Make the ublk device and the link /dev/mica/<disk>.
  5. Start the transaction loop, the head watcher, the profile download and the fill.
  6. In the background: delete the folders in lost, finish unfinished deletes (section 14), and finish snapshots marked deleting.

Detach

  1. Remove the device. It must not be open or mounted.
  2. Run transactions until no chunk is unsaved.
  3. Wait for the deletes of the free rule.
  4. Delete the folders in lost again, to catch late uploads of an old owner.
  5. Write the head without an owner.
  6. Delete the local folder of the disk. The cache stays.

When the owner loses the disk

The owner learns it from a failed conditional write, or from the watcher. Then it stops all transactions and fails all reads and writes of the device. It keeps the local data and marks the disk as orphaned. An operator copies that data out or deletes it.

14. Crashes

Crash duringWhat is leftWho repairs it, and how
A write, before a flushData that the guest did not flushNothing to repair. A disk can lose writes that were not flushed.
The mica service, with the node upLocal chunks, the dirty log, the recent bitmapThe new service takes over the ublk devices. The guest pauses for about 1 second.
A power loss of the nodeLocal chunks and the dirty logAt start, mica reads the dirty log and punches holes in all other ranges. The guest keeps all flushed writes.
Uploads, before the commitObjects in the folder nextThe next claim adds next to lost. The owner deletes it.
Deletes, after a commitReplaced objects and their old rootThe owner walks back from the current root by parent. Each old root that still exists and is not a snapshot has unfinished deletes. The owner compares it with the next root and deletes again.
A snapshot deleteThe snapshot marked deletingThe owner runs the merge again.
A disk deleteThe head with deleted, and some objectsmica disk delete again.
A restoreA new disk with some copiesThe new head has no current version yet. mica disk delete removes it.

15. Settings and commands

TodayNew
checkpoint_interval_secs = 30transaction_interval_seconds = 30
checkpoint_after_mib = 512transaction_after = "512M"
gc_keep_checkpoints, gc_grace_hoursRemoved
s3.conditional_writesRemoved. Conditional writes are required.
mica disk checkpoint <disk>mica disk commit <disk>
mica gcRemoved
mica snapshot create <disk> <name>The same. Names are unique only inside a disk.
mica disk create <new disk> --from-snapshot <name>mica disk restore <disk> <snapshot> <new disk>

16. What changes from today

TodayNew
KeysContent hash, shared by all disksDisk, transaction and position
Small requests in each transaction, besides dataAbout 81 conditional write of the head
GarbageStays until a global collection, plus 24 hoursDeleted right after the transaction
Collection over 10,000 disksAbout 1 hour and several GB of memory (estimate from the code)None
Owner checkA separate attached object, read before each commitThe head, with conditional writes
Commit after a forced claimPossible one timeNot possible
New disk from a snapshotInstant, shares chunksA copy inside the object store, a few seconds for each few GiB
Kept versionsThe last 5, until a collectionOnly snapshots
mica disk ls details3 reads for each disk1 read for each disk

Code that goes away: src/gc.rs, the checks of collection activity in each transaction, the repair step, held copies and the hold file, the attached object, the claim without conditional writes, and the page store shared by all disks.

17. Limits

18. Steps and checks

  1. Check the object stores. Conditional writes, DeleteObjects and CopyObject on RustFS and Hetzner.
  2. The head as the lock. Owner, next and lost in the head. Remove the attached object.
  3. New keys, transactions and the free rule. Remove the global collection, the repair step and held copies.
  4. Cache lookup by hash over all disks of the node.
  5. Snapshots in the head: create, the merge rule, and restore by copy.
  6. Delete a disk by folder.