CASE STUDY · ORIGINAL
Case study: reading the Haystack photo storage paper
A learner’s reading of Facebook’s 2010 Haystack paper: why one-file-per-photo hit a metadata wall, how photos became needles in large volume files, and what to sketch for photo questions.
Most photo-storage answers in interviews stop at "put the bytes in object storage, put a CDN in front". That is a fine first sketch. The Haystack paper is worth reading because it explains what happens one layer down, when you are the team that has to build the object store and the plain approach falls over. It is short, concrete and full of numbers that drive decisions.
This is a learner's summary in my own words. Sections marked What the paper reports describe the paper's claims and figures as published in 2010. Sections marked Commentary are my reading of what matters for design practice today. Read the paper itself; it is linked below.
What the paper reports: the problem
At the time of writing, the authors report storing over 260 billion images (about 20 PB), with roughly one billion new photos (about 60 TB) uploaded each week and over one million images served per second at peak. Each uploaded photo was stored in four sizes.
The previous design kept each photo as a file on network-attached storage appliances mounted over NFS. Popular photos were served by CDNs. The paper's key observation is about the rest: social photo traffic has a long tail of older, less popular photos that miss in the CDN, and serving those requests was dominated by filesystem metadata work, not by reading photo bytes. To find a file, the system first had to read directory and inode information from disk. The paper reports that, with large directories, reads often took more than ten disk operations; after shrinking directories it was still about three per photo. Caching file handles in memcache helped only a little, because the long-tail photos that reached storage were exactly the ones unlikely to be cached.
The paper also notes that most of the per-file metadata (such as permissions) was never used for photos, so the cost bought nothing.
What the paper reports: the design
The goal was to keep all the metadata needed to locate a photo in main memory, so that reading a long-tail photo costs at most one disk operation. Haystack has three components.
Store: needles in volume files
Instead of one file per photo, each Store machine holds a few large physical volumes. The paper gives the example of a machine's 10 TB split into 100 volumes of 100 GB each. A volume is a single file: a superblock followed by a sequence of needles, one per stored photo. Each needle carries a header with a 64-bit key (the photo ID), a 32-bit alternate key (which of the four sizes it is), a cookie, flags, a size, the data and a checksum.
Each Store machine keeps an in-memory map per volume from (key, alternate key) to the needle's offset and size. A read is a memory lookup, one seek and one read of the whole needle. The paper reports about 10 bytes of memory per stored photo on average, compared with 536 bytes for a Linux XFS inode structure. They also leaned on XFS with large preallocated extents so that the filesystem's own block maps for the big volume files fit in memory.
Writes are appends. Physical volumes on different machines are grouped into a logical volume, and an upload is written to every physical volume in the group; the paper says photos are replicated in geographically distinct locations. Needles are never overwritten: a modified photo is appended as a new needle with the same keys, and the highest offset wins.
Deletes set a flag in memory and in the needle. The space is reclaimed later by compaction, which copies live needles into a new file and swaps it in. The paper reports that about 25% of photos are deleted over a year, with young photos the most likely to go.
To restart quickly without scanning terabytes, each volume has an index file, a checkpoint of the in-memory map. It is written asynchronously, so on restart the machine indexes any trailing needles that the index missed, and it learns about deletions lazily by checking the flag when it reads a needle.
Directory
The Directory holds the application-level metadata: which physical volumes make up each logical volume, which logical volumes are still writable, and whether a given photo should be served through the CDN or the internal Cache. Web servers ask it which logical volume to upload to and use it to build photo URLs. When a machine fills up, it is marked read-only. The paper describes the Directory as a replicated database behind a PHP interface with memcache.
Cache
The Cache is an internal distributed hash table keyed by photo ID that sits between CDNs or browsers and the Store. It caches a photo only if the request came directly from a browser (the paper argues that a request that already missed the CDN is unlikely to hit an internal cache) and if the photo lives on a write-enabled machine. The second rule exists because recently uploaded photos are both the most read and on the machines also busy taking writes, so shielding those machines helps most.
The URL as a routing plan
A photo URL encodes the route: which CDN, which Cache, which Store machine, and the logical volume plus photo ID. Each layer looks up the photo; on a miss it strips its own part of the URL and forwards to the next. The cookie in the URL, assigned randomly at upload and checked by the Store after reading the needle, stops people from guessing valid photo URLs.
Reported results and operations
The paper reports that each usable terabyte cost about 28% less and served about four times more reads per second than the NAS setup. Uploads are batched, since users tend to upload albums. A background checker marks unhealthy Store machines read-only; recovery from a lost machine is a bulk copy from a replica, which the authors say could take hours because of network bandwidth.
Commentary: the ideas worth keeping
Count metadata, not just bytes. The most transferable lesson is the estimate the authors effectively ran: at billions of small objects, the per-object overhead of a general filesystem dominates. Ten bytes versus hundreds of bytes per object is the difference between an index that fits in RAM and one that does not. Try the arithmetic: 1 billion photos at 500 bytes of metadata is 500 GB; at 10 bytes it is 10 GB. That changes the machine you buy and the number of disk seeks per read. Estimates that drive decisions is about exactly this habit.
Append-only plus an in-memory index is a reusable shape. Volume files with offsets in memory, flag-then-compact deletes, and a lazily written checkpoint for fast restart: this is the same skeleton you would draw for a simple log-structured key-value store. Seeing it in a photo system helps you recognize it elsewhere.
Separate placement metadata from object storage. The Directory knows which volume a photo lives in; the Store knows where in the volume. Each piece of metadata lives where it is used, and the hot path never asks a central service where bytes are on disk.
Cache with a reason. The Cache admission rules come from measured traffic, not habit. In your own sketch, ask what a cache layer will actually hit. A cache behind a CDN often sees mostly misses. Caching and hot keys covers the general version.
What the paper does not try to solve. It assumes an existing system for upload processing, social metadata and access control beyond the URL cookie. Replication is full copies, which f4 (in further reading) later revisits for photos that are rarely read. It is a 2010 design; today most teams get these properties from a managed object store, and you should say so in an interview. The paper is valuable because it shows why such stores pack small objects the way they do.
Commentary: what to sketch and where it fits
For practice, reproduce the read path from memory: URL to CDN to Cache to Store, then in-memory lookup to one disk read. Then add the write path: Directory picks a writable logical volume, web server appends to every replica. Finally add delete, compaction and restart from the index file.
| Option | Wins when | Costs |
|---|---|---|
| One file per photo on NAS or a filesystem | Object count is modest (millions), or the store is managed for you. | Per-file metadata and directory lookups cost extra disk operations on every cold read. |
| Needles in large volume files with an in-memory index | Billions of small, write-once objects with a long tail of cold reads. | You own compaction, index recovery, replica repair and a placement directory. |
| Managed object store plus CDN | You want the benefits without building storage machines. | Less control over cost per byte and cold-read latency; vendor limits on request rates. |
- Photo library: this is the closer fit. A private library is about storage efficiency over a huge long tail, multiple renditions per photo and deletes. Use the needle idea when the interviewer asks what happens below the object store, and the deletion rate to justify compaction.
- Photo sharing app: the feed and fan-out are the main event, but the CDN versus long-tail argument and the URL-as-route idea are good material for the "how do photos get to the viewer" follow-up.
- File sync: chunks are small, immutable objects too. The same packing and compaction reasoning applies to a chunk store.
TAKE THIS TO YOUR NEXT SKETCH
- At billions of small objects, metadata per object, not bytes, decides how many disk operations a cold read costs.
- Pack small immutable objects into large append-only files and keep offsets in memory; delete by flag and reclaim by compaction.
- Split placement metadata (which volume) from location within a volume (which offset), so the hot path needs no central lookup.
- Admit to a cache only what measured traffic says will hit; a cache behind a CDN often sees mostly misses.
- In interviews, a managed object store is a fine first answer; Haystack is how you explain what is underneath when asked.
PRACTICE WITH THIS
Now sketch one.
OFF-SITE READING · NOT WRITTEN BY US
Further reading
- Finding a needle in Haystack: Facebook’s photo storage
The primary source for everything summarized here; sections 2 and 3 carry the design and are readable in under an hour.
- Needle in a haystack: efficient storage of billions of photos
An earlier, shorter post from the team that introduced the system before the paper, useful as a quick orientation.
- f4: Facebook’s Warm BLOB Storage System
The follow-up that moves rarely read blobs out of Haystack to cut replication overhead, a good second read on what happens as a photo ages.