FastATP
The repo's tree

The order never changes. The shape does.

Write the same notes into a fresh repo twice and count its blocks. You may get a different number of tree nodes each time. Nothing is wrong, because "order" and "shape" are two separate facts about the tree.

Order: always the same Records are kept sorted by key. The keys are timestamps, so that is the order they were written in. Every run.
Shape: differs per run How many tree nodes there are, and which node a record sits in, is worked out from a hash of each key. New keys, new shape.
Step 1

A hash is a fingerprint of some bytes

Feed any bytes to SHA-256 and you get a short, fixed-size number back. The same bytes always give the same number. Change one byte and the number is completely different, in a way nobody can predict or steer.

In the repo, every block is stored under the hash of its own bytes. That hash is the block's CID.

Step 2

"Merkle" means each block holds the hashes of the blocks under it

This part is live and needs JavaScript.

That is the whole "Merkle" idea. Notice it says nothing about how many tree nodes there are. Any shape works, as long as each block holds the hashes of the ones under it.

Why have tree nodes at all? Without them the index would be one long list of every key in a single block. Every write would rewrite and re-hash the whole list, and proving one record is in your repo would mean sending someone all of it. With a tree, a write changes only the nodes between that record and the commit.

Step 3

"Search tree" means the entries are kept sorted by key

Each record is filed under a key: its collection name, a slash, and a record key, such as com.example.tutorial.note/3mz2b56hcz64m. The tree keeps those keys in sorted order so a record can be found without reading everything.

What the tree promises is key order. These notes take their record keys from the clock, so here key order is also the order they were written. A record filed under a key you chose yourself sits wherever that key sorts. This is the part that is constant. Read the tree left to right and you always get the first note, then the second, then the third.

Step 4

The surprise: a key's hash decides how high it sits

A tree needs some rule for which entries sit near the top and which sit lower. atproto's rule is: hash the key, then count how many pairs of zero bits the hash starts with. That count is the entry's layer.

Hash starts withLayerHow often
01…, 10… or 11…0 (the bottom)3 keys in 4
00 then a non-zero pair1about 1 in 5
00 00 then a non-zero pair2about 1 in 20

Think of a ruler. Every tick sits at its exact place along the ruler, but some ticks are tall and most are short. The key is the place; the layer is only the tick's height.

The height behaves like rolling a die for each key. And because a record key contains the time down to the microsecond, every run rolls fresh dice, even though the note says the same thing.

  • Neighbouring entries on the same layer share one tree node.
  • An entry one layer up sits in a node above them, and splits them: the entries before it go in one node under it, the entries after it in another.
  • If the gap is two layers or more, empty nodes fill it.
Try it

Alice writes three notes. Roll the keys again.

This runs the real rule in your browser: real record keys from the clock, real SHA-256, and the same tree-building steps the library uses.

This part is live and needs JavaScript.

Empty nodes look wasteful here, and they only show up in tiny repos. A key reaches layer 5 about once in a thousand; with three records nothing fills the layers under it. In a repo of a few thousand records every layer is occupied and there are no gaps.

Why build it this way

Because the shape depends on nothing but the keys

The dice are not random in the usual sense. Press "Rebuild from the same keys" and you get the identical tree every time. The shape does not depend on who built it, when, or in what order the records were added.

That is the payoff. Two machines that hold the same records always arrive at the same tree, byte for byte, and so at the same top hash. They can tell whether they agree by comparing one hash, and when they differ they can walk down to find exactly which records are missing. A tree whose shape depended on the order of writes could not do that.

The hash also keeps the tree balanced with no extra work. It scatters any keys evenly, even perfectly sequential ones, so about one entry in four is promoted at each layer.

Same on every runDifferent on every run
The notes and their textThe record keys (they contain the time)
The order of the entriesEach key's layer
One commit, one block per recordHow many tree nodes, and their sizes
Every block holds the hashes of those under itEvery hash, since the keys and times differ
Walk it

Finding one record in a big repo

To find or check one record you start at the top node and walk down. At each node the keys are sorted, so you can see which gap your key falls in, and you follow that one pointer. Everything else in the repo is never read.

The cost is the length of that walk, not the size of the repo. Because about one key in four is promoted to each higher layer, the walk grows by roughly one node each time the repo gets four times bigger.

This part is live and needs JavaScript.

Checking the record afterwards is the same walk in reverse: hash the record and compare it with the hash its node holds, hash that node and compare with its parent, and so on up to the commit, whose signature you check once.

Read more

Where to go from here

  • atproto's repository spec: the section on the MST gives the layer rule in a few paragraphs.
  • Merkle Search Trees, Auvolat and Taïani, 2019: the paper the design comes from.
  • In FastATP, fastatp/repo/mst.py: key_height is the layer rule in ten lines, and _build_node is the whole builder.