Skip to content

Log-structured storage: packing small objects into large files

Append many small objects into a few large files and keep a small in-memory index from key to file and offset, so a read costs about one disk operation. Deletes become tombstones that compaction reclaims later.

On this page

What it is

Log-structured storage appends new data at the end of a file instead of writing it in place. Applied to small objects, it packs many of them into a few large files and finds them again through an index kept mostly in memory. The design exists because a filesystem charges per file. Facebook's engineering blog describes its earlier photo setup, one image per file on NFS appliances: the directories and inodes added up to far more metadata than the storage tier could cache, so a single photo read or upload turned into multiple I/O operations, and that metadata overhead, not the photo data, was the bottleneck of the whole serving path.

The layout. A Haystack store is an append-only file: an 8 KB superblock, then a sequence of records called needles, each made of a header, the data and a footer, and identified by its offset, key, alternate key and cookie. The photo server in front of it keeps an in-memory index of the offset of each photo, holding the least metadata that still locates an image, because a node holds hundreds of millions of photos and the index has to fit in memory. Bitcask applies the same shape to a key-value store: an append-only, write-once file format plus an in-memory hash table whose entries point straight at the location of each value on disk, with the condition that the machine has enough RAM for the whole key space. LinkedIn's Ambry preallocates one on-disk log per replicated store, appends both puts and deletes to it, and keeps an index from blob id to log offset along with delete flags and TTLs.

SystemThe big fileThe indexDelete and reclaim

Haystack

Store file: 8 KB superblock, then needles (header, data, footer)

In-memory map of photo offsets, loaded at startup from an index file that is usually under 1% of the store's size

A deleted bit set in the needle's flags; compaction copies live needles into a new store and swaps it in

Ambry (LinkedIn)

Preallocated on-disk log per store

Sorted index segments; the newest in memory, older ones memory-mapped and searched newest first, each with a Bloom filter

Delete record appended; found before the put during the newest-first search

SeaweedFS

Append-only volume files, 40 bytes of metadata per file on disk

16-byte index entry per blob in memory

Vacuum run on demand from the shell or a maintenance worker

From an object id to the bytes. A read first has to find the right machine: in SeaweedFS clients keep a cached copy of which server holds which volume and go to that volume server directly, so the master stays out of the read path. On the server the lookup is in memory. Haystack's photo server looks the key up in its index to get the needle's offset, then asks the store for that offset, key, alternate key, cookie and data size; the store adds the header and footer lengths, reads the whole needle in one request, and returns it if the key, alternate key and cookie match, the checksum validates, and the deleted bit is clear. Bitcask's read path is the same idea with a hash table: the entry points at the value's location, so a read needs at most one disk seek, and the operating system's file cache often saves even that.

Noticing a flipped bit. The record's checksum is what catches silent corruption: a Haystack read fails unless the data passes checksum validation. Ceph's BlueStore, in the Squid release, checksums all data it writes, with crc32c as the default algorithm and usually a 4-byte checksum for each 4 KB block, or a larger block when a client hints that it reads and writes sequentially. The same documentation gives the odds a random error slips through: about 1 in 4 billion with a 32-bit checksum, 1 in 65,536 with 16 bits and 1 in 256 with 8 bits, which is the price of saving metadata. Our advice for a design round: on a mismatch, fail the local read, serve the object from another replica or rebuild it from erasure-coded fragments, and repair the bad copy; a background scrub that reads and verifies records the clients haven't touched in a while catches the rot that no GET would.

Coming back after a crash. The map lives in memory, so a restart has to rebuild it, and scanning the whole store is slow. Haystack appends each needle to the store synchronously, then writes the matching index record asynchronously and flushes the index file periodically, so the index can lag the store. Its recovery discards any partial needle at the tail, truncates the store to the last valid one, and writes index records for the trailing orphans; a delete doesn't touch the index record either, so a freshly loaded index may hold deleted photos, and the first read of one fails on the deleted bit and zeroes the entry. In Bitcask the risk is bounded the same way: the items that can be lost are partly written records at the tail of the last open file, and recovery checks the checksums of the last record or two. In Ambry the data log doubles as the transaction log: recovery scans from the last checkpointed offset, and because a delete record sits after the put it refers to, replaying in order marks the blob deleted without extra work.

When to use it

Reach for this layout when objects are small and numerous, written once and read many times: thumbnails, avatars, attachments, chunks of larger files. The Haystack paper's abstract gives 260 billion images and more than 20 petabytes, with a billion new photos a week, and the design's point is that the storage machines resolve all metadata lookups in main memory and spend disk operations on data. The question to answer in the round is whether the index fits in memory. SeaweedFS keeps a 16-byte entry per blob, and the Haystack blog post describes a per-photo entry trimmed to the fields needed to locate the image, kept in a sparse hash table with about 2 bits of overhead per entry. As an example calculation, not a target: a node holding 100 million small objects at 16 bytes each needs roughly 1.6 GB of RAM for its index, which is comfortable, and ten times that many objects needs ten times the memory.

It also suits a write-heavy ingest path on spinning disks or on SSDs that prefer large writes. Appending to one open file keeps the writes sequential: a Bitcask write is a seek to the end of the open file and an append, with little disk-head movement, so a stream of random keys can saturate the disk's bandwidth. Haystack sits on XFS, an extent-based filesystem, and preallocates large chunks when growing a store file to reduce fragmentation; a file that stays contiguous can have a block map small enough to sit in its inode. Ambry preallocates its log with fallocate to keep its sequential writes fast, and SeaweedFS describes the writes into its packed volume files as SSD friendly.

When not to

Large objects. A multi-gigabyte video gains little from being packed: one inode next to gigabytes of data is noise, and a huge record that is still live gets copied whole each time its volume is compacted. SeaweedFS treats a small file as one blob but splits a large file into chunks of a few MB, each stored as its own blob, so the packing applies to the chunks rather than to the whole file. For the upload side of large files see Chunked and resumable uploads.

Data that changes in place. Each update appends a fresh copy and the old one stays on disk until compaction. Haystack doesn't overwrite a needle at its offset; a changed photo is written again under the same key, alternate key and cookie, and among duplicates the one at the largest offset counts as current, so the dead copies accumulate until an online compaction copies the live needles into a new store. For rows that are updated often, counters, or anything needing transactions with its metadata, a database engine is the better fit; see Choosing a database.

More keys than RAM. Bitcask keeps all keys in memory, so the machine needs room for the entire key space on top of the operating system's file buffers. If a node will hold more keys than that, either shrink the per-key entry, shard across more nodes, or move to an index that lives mostly on disk: Ambry keeps its newest index segment in memory and memory-maps the older, sorted segments, consulting a Bloom filter per segment before searching it. At the other end of the scale, a system whose file metadata still fits in the filesystem's cache doesn't have the problem this layout solves; Haystack's builders hit the wall when their metadata outgrew what the storage tier could cache, and a smaller system is simpler without the custom store.

Compared

LayoutRead cost, small objectIndex memoryDeletesWhen it's the right choice

One file per object on a filesystem

Several disk operations unless the file's metadata is cached

An inode and directory entry per file, cached by the filesystem

Handled by the filesystem's own unlink; no compaction pass to run

Fewer files than the inode cache can hold, large objects, or a team that wants the simplest operations

Large volume files with an in-memory hash index (Haystack, Bitcask, SeaweedFS)

About one disk read

A fixed-size entry per object, 16 bytes in SeaweedFS

Flag or tombstone, then compaction

Hundreds of millions of small, write-once objects per node whose keys fit in RAM

Large log with sorted, memory-mapped index segments (Ambry)

A data read, which may be a random disk I/O, after a newest-first search through segments, each guarded by a Bloom filter

The newest segment in RAM; older ones paged in as needed

Delete record appended after the put

More keys than RAM holds comfortably while keeping appends sequential

Blobs inside a database

Depends on the engine's index and cache

Managed by the engine

In place or by the engine's own compaction

Objects that mutate often or need transactions with their metadata, and small total volume

In a picture

appendupdatePUTVolume serverVolume fileIn-memory map

Step 1 of 3

Write: append and index

A PUT reaches the volume server, which appends the record to the end of the open volume file and then updates the in-memory map with the new offset.

How to say it in an interview

The common mistake

Quiz

Why can a log-structured volume serve a small object with about one disk operation?
A client deletes an object. When does the node get the disk space back?
The index file is appended asynchronously and a node crashes right after writing several objects. What does it do on restart?
A design shortens each record's checksum from 32 bits to 8 bits to save metadata. What changes?

Questions that use this

Related

Sources

  1. Engineering at Meta: Needle in a haystack: efficient storage of billions of photos
  2. OSDI '10 session abstracts: Finding a Needle in Haystack: Facebook's Photo Storage
  3. Wikipedia: Bitcask
  4. Ambry wiki: Store
  5. SeaweedFS README
  6. Ceph Squid documentation: BlueStore Configuration Reference
Sign in to report a mistake