Back

Blog details

How IPFS Shards Large Directories: The HAMT, Not a B-Tree

AIOZ Network
7 min readAugust 21, 2026
aioz-pin
AIOZ Pin cover image for How IPFS Shards Large Directories with a HAMT

A HAMT (hash array mapped trie) is the structure IPFS uses to split one large directory into many smaller blocks, and it is not a B-tree. Kubo switches a directory to a HAMT once its listing passes about 256 KiB, and you never call anything different to make that happen.

TL;DR:

  • IPFS shards large directories with a HAMT, a tree of shard nodes keyed by a hash of each file name, not with a B-tree
  • Kubo converts a basic directory to a HAMT when its estimated size goes over Import.UnixFSHAMTDirectorySizeThreshold, which defaults to 256 KiB
  • Each shard node has 256 buckets by default (Import.UnixFSHAMTDirectoryMaxFanout), and names are hashed with murmur3-x64-64
  • Sharding changes the directory's CID: the UnixFS spec test vectors show one directory at 262,144 bytes staying basic and one at 262,145 bytes becoming a HAMT
  • With 10-character names and CIDv0 links, the 5,958th file tips a directory over the threshold, and AIOZ Pin takes the folder the same way at any size

Updated Nov 30, 2026: removed docs-gap lines, added a settings table, a worked entry count and the Kubo 0.40 threshold fix, trimmed repeated FAQs.

What Is a HAMT?

A hash array mapped trie is a tree where each node holds a fixed number of buckets. You hash the key, read a few bits of the hash, and those bits pick the bucket at the current level. If the bucket holds the entry, you are done, and if it holds a pointer to another node, you go down one level and read the next few bits.

In IPFS the key is a file name inside the directory, and every current implementation hashes names with `murmur3-x64-64`. With the default fanout of 256, each level consumes 8 bits of that hash, so the bucket names run from 00 to FF. This post follows one real example throughout: the spec's test directory of 1,000 files, and the lookup for 470.txt inside it.

A B-tree works differently: it keeps keys sorted and splits or merges nodes to stay balanced. A HAMT never sorts anything and routes by hash, so its depth depends on how many bits it takes before names stop colliding. If you saw "B-tree" in an older answer about IPFS directories, the specs and the Kubo code both call this structure a HAMT.

Why a Directory Needs Sharding

A small IPFS directory is one dag-pb block that lists every entry as a name and a CID. That works until the listing gets big. As of Nov 2026, peers on the public swarm tend to ignore requests for blocks over 2 MiB, so one giant listing block can become impossible to fetch.

A single block also makes every change expensive, because adding one file to a 50,000-entry listing means rewriting and re-hashing the whole block. With a HAMT, the same change rewrites one shard plus the path of shards above it.

When Does IPFS Shard a Directory?

In Kubo, the switch is controlled by `Import.UnixFSHAMTDirectorySizeThreshold`, which affects ipfs add and the daemon. The default is 256 KiB and may change between releases, so check your version before you rely on it.

The size is an estimate, not the exact block size. By default (Import.UnixFSHAMTDirectorySizeEstimation set to links), Kubo adds up the byte length of each link name plus each CID. The newer block mode measures the full serialized block instead, and the unixfs-v1-2025 import profile turns it on.

Here are the related keys with their default values. This is a fragment of the config file, not command output:

"Import": {
  "UnixFSHAMTDirectorySizeThreshold": "256KiB",
  "UnixFSHAMTDirectoryMaxFanout": 256,
  "UnixFSHAMTDirectorySizeEstimation": "links",
  "UnixFSDirectoryMaxLinks": 0
}

How many files fit in 256 KiB? In links mode each entry costs its name plus its binary CID, and a CIDv0 is 34 bytes. A 10-character name therefore costs 44 bytes, and 262,144 / 44 = 5,957.8, so the 5,958th such file tips the directory into a HAMT. Longer names reach it sooner. A second trigger, UnixFSDirectoryMaxLinks, can force the switch by entry count, but it is off by default.

The boundary is strict: under IPIP-499, a directory converts only when the estimate is greater than 262,144 bytes. Its test vectors include a directory at exactly 262,144 bytes that stays basic and one at 262,145 bytes that becomes a HAMT.

How a Lookup Walks the Shards

Diagram of a lookup for the file 470.txt in a HAMT sharded IPFS directory with fanout 256: the root shard reads the first 8 bits of the murmur3 hash and follows bucket 00 to a sub-shard, which reads the next 8 bits and finds the entry named 6E470.txt that links to the file CID

Back to the spec's 1,000-file test directory. To find 470.txt, you hash the name with murmur3, and the first 8 bits give bucket 00 at the root. That link points to a sub-shard, because several names landed in the same bucket.

The sub-shard reads the next 8 bits, which give 6E. There it finds a link named 6E470.txt, the bucket prefix followed by the original name. That link points straight at the file's CID, so the lookup touched two shard blocks instead of a list of 1,000 entries.

Here is the root shard as the spec shows it, trimmed:

// Root HAMT shard (bafybeidbclfqleg2uojchspzd4bob56dqetqjsj27gy2cq3klkkgxtpn4i)
Data = {
  Type = HAMTShard        // Type = 5
  Data = 0xffffff...      // Bitmap: bits set for populated buckets
  hashType = 0x22         // murmur3-x64-64
  fanout = 256            // 256 buckets (8-bit width)
}
Links = [
  { Hash = bafybeiaebmuestgbpqhkkbrwl2qtjtvs3whkmp2trkbkimuod4yv7oygni, Name = "00" },
  { Hash = bafybeia322onepwqofne3l3ptwltzns52fgapeauhmyynvoojmcvchxptu, Name = "01" },
  // ... more buckets as needed up to "FF"
]

Three details from the spec matter if you read raw blocks. The bitmap in Data tells a reader which buckets exist without scanning every link. Bucket prefixes must be uppercase hex, and empty intermediate shards are typically collapsed on deletion, so the same set of names produces the same tree whatever order you added them in.

Why 256 Buckets Instead of 32

Many textbook HAMTs use 32 buckets per node, while IPFS defaults to 256. The spec allows any power of two that is a multiple of 8, up to 1,024. In the 1,000-file directory, 256 root buckets hold about 4 names each on average, so most root buckets point to a sub-shard, as the one for 470.txt does. With 32 buckets, about 31 names would share each root bucket. A wider node means fewer levels and fewer block fetches, but each block is bigger and every change rewrites more bytes.

The spec caps fanout at 1,024 for safety, since larger values could exhaust memory when a node allocates its bucket array, a problem tracked as CVE-2023-23625. You can change the fanout with Import.UnixFSHAMTDirectoryMaxFanout, but a different fanout gives you a different CID for the same files.

Does Sharding Change a Directory's CID?

Yes. A HAMT root is a different block from a basic directory block, so the same files get a different directory CID once sharding kicks in. The two IPIP-499 vectors above show it: one extra byte of estimated size turns a basic node into a HAMT root with a new CID. Adding a 1,001st file to the test directory changes the root CID too, but rewrites only the shards on its bucket's path.

This is why two tools could hand you different CIDs for the same folder. The IPIP-499 survey found Kubo v0.39 switching at >= the threshold while Helia used >, and early js-ipfs counted entries instead of bytes. Kubo 0.40 moved to `>`, and IPIP-499 named import profiles, so you can pin down the settings when you need CIDs to match. Pick the settings by what you need:

Your goal Setting What it costs
The same folder gives the same CID in every tool that applies this IPIP-499 profile unixfs-v1-2025 import profile CIDs differ from older CIDv0 imports of the same folder
Test code that reads sharded directories, using small folders Import.UnixFSHAMTDirectorySizeThreshold = 1B Every directory becomes a HAMT, so its CID changes
Folders with tens of thousands of files Leave the 256 KiB default Nothing to change; each listing block stays far below 2 MiB
Keep a directory flat on purpose Import.UnixFSHAMTDirectorySizeEstimation = disabled A huge listing can pass 2 MiB, and public peers may not fetch it

How AIOZ Pin Handles It

The takeaway: sharding is an import detail, so a folder's size can change its CID but never the call you make. You pin a folder on AIOZ Pin the same way at any size. The CLI on the AIOZ Pin SDKs page takes a directory path:

pinning pin --file ../some/where

The Node.js SDK does the same with pinFolderToIPFS({ sourcePath }), and the pin record carries the folder's root cid and an is_dir field. Need that root to match a CID you built locally, say with the unixfs-v1-2025 profile? Import the folder yourself and pin the result with the Pinning API's `pinByHash`, which takes a CID you already have.

Frequently Asked Questions

Does IPFS use a B-tree for directories? No. Large IPFS directories are sharded with a HAMT (hash array mapped trie), while small directories stay a single flat block.

Which hash function does an IPFS HAMT use? murmur3-x64-64 (multihash code 0x22). It is fast, not cryptographic, and that is fine: the CIDs inside each shard already secure the content.

Do I need to do anything different to pin a large folder on AIOZ Pin? No. An NFT metadata folder of 10,000 files named 1.json to 10000.json holds about 429,000 bytes of CIDv0 links, well past the threshold. The CLI and pinFolderToIPFS() still take it the same way as a 10-file folder.

References

We only send updates when meaningful changes ship, and you can unsubscribe anytime

Related Content

blog thumbnail

ENS IPFS Contenthash: Point an .eth Name at a Website

An ENS IPFS contenthash points your .eth name at a site on IPFS. See the byte format, a real encoding, ipfs vs ipns records, gas used, and common mistakes.

7 min readDecember 04, 2026
blog thumbnail

ERC-721 Metadata JSON: Fields, Images and ipfs:// URIs

ERC-721 metadata JSON explained: the three fields EIP-721 defines, OpenSea attributes and animation_url, ipfs:// image links, and folder CIDs for collections.

8 min readDecember 02, 2026
blog thumbnail

Merkle DAG vs Merkle Tree: What IPFS Changes and Why

Merkle DAG vs Merkle tree explained: a tree gives each node one parent and short proofs, while the DAG IPFS uses shares blocks, carries data and links by CID.

8 min readNovember 30, 2026
blog thumbnail

What Is IPNI? How the IPFS Network Indexer Finds Providers

IPNI is the IPFS network indexer behind cid.contact. See how index providers publish signed advertisement chains and how one HTTP query finds providers.

8 min readNovember 27, 2026
blog thumbnail

On-Chain vs Off-Chain NFT Metadata: Where the Data Lives

On-chain vs off-chain NFT metadata: what the contract stores, what lives on IPFS or a web server, what each costs in gas, and how to check any token yourself.

6 min readNovember 25, 2026
blog thumbnail

What Is a Kademlia DHT? The Routing Algorithm Behind IPFS

A Kademlia DHT finds peers by XOR distance. See how k-buckets, k = 20 and iterative lookups let IPFS locate who holds a CID, and how to watch it on Kubo.

6 min readNovember 23, 2026