Learn Labs
Internals

Volumes, Needles & File IDs

Haystack-style storage: assigning ids, writing to volume servers, and reading replicas

Underneath the S3 API, SeaweedFS stores needles in volumes, the model from Facebook's Haystack paper. labs/needles.py skips the S3 gateway and the filer and talks to the master and the volume servers directly.

docker compose -f compose_prod.yaml run --rm labs python needles.py

assign -> fid 4,01def127163a on volume-1:8080
volume id 4, needle key + cookie 01def127163a (cookie stops id guessing)
upload: {'name': 'hello.txt', 'size': 19, 'eTag': 'f1d919b5', 'mime': 'text/plain'}
volume 4 is on ['volume-1:8080', 'volume-3:8080']  (replication 010: two copies, different racks)
GET http://volume-1:8080/4,01def127163a -> hello from a needle
GET http://volume-3:8080/4,01def127163a -> hello from a needle
rack1/volume-1:8080: volume 4 size 171184 files 4 replication {'rack': 1}
rack3/volume-3:8080: volume 4 size 171184 files 4 replication {'rack': 1}

The file id

fid 4,01def127163a4volume,01dekeyf127cookie163acookiethe master knows where volume 4 lives
  • Volume id (4): which volume. The master maps it to servers.
  • Needle key (01de): the file's number inside the volume, the key of the volume's in-memory index.
  • Cookie (f127163a): 32 random bits stored with the needle and checked on every read. Without it, anyone could read every file by counting keys upward.

The four steps

GET master/dir/assign-> {"fid": "4,01def127163a", "url": "volume-1:8080"}POST volume-1:8080/4,01def127163a (bytes)volume-1 appends, replicates to volume-3, then answersGET master/dir/lookup?volumeId=4-> [volume-1:8080, volume-3:8080]GET volume-3:8080/4,01def127163aany replica can serve it

An application can use this layer directly and store the fid in its own database. That's the fastest way to use SeaweedFS (no filer lookup per read) and how it's used for very large photo or thumbnail stores. Most applications are better served by S3, which adds names, buckets, permissions and portability.

Inside a volume

/data/4.datneedles appended one after another: header, key, cookie, data, checksum/data/4.idxkey -> (offset, size), loaded into memory at startup/data/4.vifvolume info: replication, version/data/app-data_7.data volume in the app-data collection (one per S3 bucket)

Writes are appends. Deleting a file writes a tombstone and leaves the bytes in place; overwriting a key appends a new needle. The space comes back only when the volume is vacuumed (compacted): copied without the dead needles. The master triggers vacuum when a volume's garbage passes -garbageThreshold (30% by default), and you can run volume.vacuum in weed shell.

Volumes fill up

A volume becomes read-only when it reaches the size limit, and the master grows new volumes to keep enough writable ones per collection. A server with -max=30 can hold 30 volumes. When every server is full, assign fails and so do writes, even though plenty of other volumes still have free space for reads. The Writable volumes by collection panel on the dashboard watches exactly this.

Lab

  1. Run needles.py twice. Do both runs get the same volume?
  2. Change one character of the cookie in the fid and GET it. What do you get?
  3. In weed shell (lock first), run volume.list and find volume 4 on both servers. Delete some objects, then run volume.vacuum -garbageThreshold=0.0001 and compare sizes.

On this page