Databases & Storage · Paper 04 / 05

Bigtable: A Distributed Storage System for Structured Data

Fay Chang, Jeffrey Dean, Sanjay Ghemawat, Wilson C. Hsieh, Deborah A. Wallach, Mike Burrows, Tushar Chandra, Andrew Fikes, Robert E. Gruber

2006 · OSDI · ~12 min read

Read the original paper

Bigtable stores a sorted map of rows, columns and timestamps, split by key range across thousands of servers with single-row atomicity. Read it to learn row-key design, which decides scan cost and hot spots in HBase, Cassandra and DynamoDB alike.

Google’s crawler in the early 2000s fetched billions of web pages and fetched them again as they changed. For each page the indexing pipeline wanted the latest contents, a few older versions to detect change, and every link pointing to the page from elsewhere on the web, with the anchor text of each. The pipeline read this data in enormous batch scans. Other teams wanted to read single rows of it while a user waited. The same company also needed to store satellite imagery for Google Earth, click streams for Analytics and per-user settings for personalised search.

GFS, Google’s distributed file system, could hold the bytes, but a file system offers no way to update one page’s record among billions or to find it by URL. Commercial databases of the time did not scale across thousands of cheap machines, and their licences would have cost a fortune at that size. So a team including Fay Chang, Jeff Dean and Sanjay Ghemawat built a storage system with a deliberately small feature set, described in this 2006 OSDI paper.

After reading this you should be able to design a Bigtable row key for a given access pattern and predict where its load will land, trace a read from a client to the right server, and explain what happens when a server holding part of the table dies. Those three skills carry directly to HBase, Cassandra, Cloud Bigtable and DynamoDB.

A map with three parts to its key

The paper describes Bigtable as a sparse, distributed, persistent, multi-dimensional sorted map. That phrase is denser than it looks, so let us take it apart with the paper’s own example, the Webtable that holds the crawl.

Each cell is addressed by three things: a row key, a column key and a timestamp. The value is an uninterpreted string of bytes. The row key for a page is its URL with the hostname reversed, so www.cnn.com/index.html becomes com.cnn.www/index.html. Columns are grouped into column families, which must be declared when the table is created and are meant to be few, a few hundred at most. Inside a family, any column name can be used without declaration. The Webtable has a contents family holding the page body, and an anchor family in which the column name is the referring site, so anchor:cnnsi.com holds the link text that cnnsi.com uses when it points to CNN. A popular page may have thousands of anchor columns; an obscure one has none. That is what sparse means: a row stores only the columns that were written to it.

row key (sorted)family: contentsfamily: anchorcom.cnn.wwwcom.cnn.www/worldorg.wikipedia.en/wiki/Bigtablecontents: · t6 “<html>…”contents: · t5, t3 (older versions)anchor:cnnsi.com · t9 “CNN”anchor:my.look.ca · t8 “CNN.com”(row, column, timestamp) → bytes · columns are created by writing to them · rows with nearby keys share a tablet
The Webtable row for CNN’s home page. Rows are sorted by key; contents and anchor are column families; each cell keeps several timestamped versions. A column exists because something was written to it.

Every cell can hold multiple versions, indexed by a 64-bit timestamp, stored newest first. Each column family has a garbage-collection setting: keep the last n versions, or keep versions newer than some age. The crawler writes a new contents version each time it fetches a page and the table keeps, say, the last three, so the indexer can tell whether the page changed.

Two properties of this model matter more than everything else in it. First, rows are kept in lexicographic order by row key, and a contiguous range of rows, called a tablet, is the unit of distribution. Second, every read or write of a single row is atomic, however many columns it touches, and nothing wider is. There are no multi-row transactions, no secondary indexes, no joins and no query language. If you want to find rows by something other than their key, you maintain a second table yourself.

Why the hostname is reversed

Sorting by row key means the row key is the only index and the only control over what is stored together. Reversing the hostname is a design decision about locality, and you can see its effect by changing it.

With plain URLs as keys, rows sort by the first label of the hostname. www.cnn.com sits among everything else starting with “www”, money.cnn.com sits among the m’s, sports.cnn.com among the s’s. A job that wants to process every page on cnn.com must read scattered ranges spread across many tablets on many servers. With reversed hostnames, com.cnn.money, com.cnn.sports and com.cnn.www are adjacent, and every page of the site is one contiguous range that usually falls in one tablet or a few neighbouring ones. Try both in the panel below.

Try itPick a row key, then scan one site

Rows live in key order and are split into tablets of four rows each. Ask for every page of one site and count the tablets the scan has to visit.

tablet 1
blog.example.com/post/1
de.wikipedia.org/wiki/Bigtable
en.wikipedia.org/wiki/Bigtable
maps.google.com/
tablet 2
money.cnn.com/markets
news.bbc.co.uk/weather
shop.example.com/cart
sports.cnn.com/nba
tablet 3
www.bbc.co.uk/news
www.bbc.co.uk/sport
www.cnn.com/index.html
www.cnn.com/world
tablet 4
www.example.com/
www.google.com/search
tablets touched2of 4
scan isscatteredrows for one site are spread by subdomain

With plain URLs, sorting is by the first label, so sports.cnn.com sits far from www.cnn.com. A scan for one site walks tablets it mostly does not need. Nothing in the system is wrong; the key just does not match the question you ask.

The benefit goes beyond scans. Bigtable compresses data per block within each SSTable, and pages from the same site share navigation bars, boilerplate and scripts. Because the reversed key puts them next to each other, the compressor sees the repetition. The paper reports roughly a ten-to-one reduction for Webtable contents with its two-pass compression scheme, and attributes much of that to similar pages being stored together. The row key, chosen for locality, also bought a large storage saving.

Predict first

A monitoring team stores one reading per host per second in Bigtable with the row key timestamp#hostname, for example 20061106T120000#web-0412. They have 50,000 hosts and 400 tablet servers. At any moment, how many tablet servers are receiving writes? How would you change the key?

Show the worked answer

Essentially one. Every write in a given second has nearly the same timestamp prefix, so all 50,000 rows sort into the same narrow range at the end of the table, which belongs to the last tablet. Bigtable will split that tablet as it grows, but the new writes always land past the latest split, on the tablet holding the newest timestamps. One server takes the entire write load while 399 sit idle.

Put the host first: web-0412#20061106T120000. Writes now spread across the whole key space because 50,000 hosts are spread across it, and the question “readings for host web-0412 in the last hour” is a single contiguous scan. The cost is that “all hosts at noon” becomes a scan over every host’s range. If that question matters, a second table keyed by time bucket and host, written alongside the first, answers it. Some designs also reverse the timestamp (subtract it from a large constant) so that the newest reading for a host sorts first. Google’s Cloud Bigtable documentation still warns against row keys that begin with a timestamp, for this reason.

Finding the server that holds a row

A large table is split into many tablets, each roughly 100 to 200 MB by default, and each tablet is served by one tablet server at a time. Tablets split when they grow too large, and the master moves them between servers to balance load. A client that wants row com.cnn.www/index.html must find which server currently holds the tablet whose range covers that key.

Bigtable answers this with a three-level lookup that is itself stored in Bigtable. A file in Chubby, Google’s Paxos-replicated lock service, records the location of the root tablet. The root tablet holds the locations of all the METADATA tablets. Each METADATA tablet holds the locations of a set of user tablets, in rows keyed by table name and the tablet’s end row. The paper works out that with 128 MB METADATA tablets this scheme can address 234 tablets. The root tablet is never split, so the hierarchy never grows a fourth level.

clientChubbylocks · root tabletmasterassigns tablets, balancestablet server 1tablet server 2tablet server 3GFS · SSTables and commit logs, replicatedrarelyreads, writes
Who talks to whom. Clients send reads and writes straight to tablet servers. The master assigns tablets and balances load but is not on the data path. Chubby holds the locks and the root tablet’s location; GFS stores every SSTable and commit log.

Clients cache tablet locations. With an empty cache, a lookup costs three network round trips, one of them to Chubby. With a stale cache, the client discovers the miss when the server it contacts no longer holds the tablet, and walks back up the hierarchy, which the paper says can take up to six round trips. Clients also prefetch neighbouring METADATA entries when they read one. The effect is that the master is almost never contacted by clients, so its load does not grow with traffic. A single master is acceptable precisely because it is off the read and write path.

Inside a tablet server: the LSM-tree from the previous paper

Each tablet server stores its tablets the way the LSM-tree paper suggests, with names that stuck. A write is checked for authorisation, appended to a commit log in GFS, and then inserted into an in-memory sorted buffer called the memtable. When the memtable grows past a threshold, it is frozen and written to GFS as an SSTable, an immutable file of sorted key-value pairs in blocks with an index at the end. The paper calls this a minor compaction. A merging compaction combines a few SSTables and the memtable into one new SSTable. A major compaction rewrites all of a tablet’s SSTables into exactly one, and it is the only kind guaranteed to remove deleted data, because only then can it be sure no older copy is hiding in another file.

A read is executed on the merged view of the memtable and the tablet’s SSTables. To keep that affordable, the paper adds several refinements that later systems copied almost unchanged. Clients can group column families into locality groups, each stored in its own SSTables, so that a scan of the small anchor data never reads the large page contents. A locality group can be marked to live in memory, which Bigtable uses for the location columns of the METADATA table. And clients can ask for a Bloom filter per locality group, so that a lookup for a row or column that does not exist rarely touches disk.

When a tablet server dies

Here is a failure walked through step by step, because it is where the pieces fit together. When a tablet server starts, it creates a uniquely named file in a Chubby directory and takes an exclusive lock on it. The master watches that directory. Suppose the server holding the tablet for com.cnn.* is cut off by a network fault.

Its Chubby session expires and it loses its lock. A server that has lost its lock stops serving its tablets, so it cannot accept a write that the system would then forget. The master, periodically asking each server about its lock, gets no answer from this one. It then tries to take the lock on the server’s file itself. If it succeeds, Chubby is healthy and the server is the problem, so the master deletes the file, ensuring the server can never serve again under that identity, and puts its tablets in the set of unassigned tablets.

A new server is assigned the cnn tablet. It reads the tablet’s list of SSTables from METADATA, which is fast because SSTables are already in GFS. The writes that were only in the dead server’s memtable exist in its commit log, and here a design decision complicates recovery. Each server keeps one commit log for all its tablets, not one per tablet, because one log means sequential appends to one GFS file instead of many. So the dead server’s log interleaves entries for many tablets, and each of those tablets may now be on a different new server. If every new server read the whole log, the log would be read many times over. The paper’s answer is to sort the log first, in parallel across servers, by table, row and log sequence number. After sorting, each new server reads only the contiguous part for its tablets.

The cost of that choice falls in the recovery path, which happens rarely, while its benefit, a single sequential log, applies to every write. That is the paper’s typical trade. When a tablet is moved deliberately for load balancing, the old server does a minor compaction first so the new server has almost no log to replay.

Choice in the paperWhat it buysWhat it costsWho later revisited it
Single-row transactions onlyNo distributed commit; every write is handled by one serverSecondary indexes and cross-row invariants are the application’s jobPercolator, Megastore, then Spanner
One master, off the data pathSimple placement and balancing logicA master failure pauses tablet moves until a new master takes the Chubby lockLargely kept; HBase has the same shape
Dependence on ChubbyCorrect membership and master election without writing consensus codeIf Chubby is down for long, Bigtable is unavailableKept; the paper measures the effect
One commit log per serverSequential writes, fewer filesRecovery must sort the log before replayHBase kept a shared write-ahead log per region server
Storage in GFSReplication and durability come for free; tablets move without copying dataEvery read that misses cache is a remote readGFS replaced by Colossus inside Google

What the authors left out, and what came after

The paper’s lessons section is worth reading for its candour. The authors say they had planned general-purpose transactions and dropped them after finding that most applications needed only single-row atomicity, and that the remaining needs, such as secondary indexes, could be met with narrower mechanisms. They also describe how many failures in practice were not the clean crashes their design assumed: memory and network corruption, large clock skew, hung machines, bugs in other systems they depended on. Their advice, to delay a feature until it is clear how it will be used, is the reasoning behind the small API.

Google’s own later systems show which omissions hurt. Percolator, published in 2010, built multi-row transactions on top of Bigtable to let the search index be updated incrementally. Megastore, in 2011, added transactions within groups of rows and synchronous replication across data centres. Spanner, in 2012, provided externally consistent distributed transactions and a SQL interface, and its paper is direct that some teams had found Bigtable hard to use for applications needing complex, evolving schemas or strong consistency across regions.

Outside Google, HBase reimplemented Bigtable on Hadoop with regions instead of tablets and ZooKeeper instead of Chubby. Cassandra combined Bigtable’s data model and storage engine with Dynamo’s leaderless replication. Accumulo added cell-level security labels. Google later sold Bigtable itself as Cloud Bigtable, with an HBase-compatible API. DynamoDB is a different design internally, but its partition key and sort key ask the same question this paper made central: which rows will be read together, and where will the writes land?

That points to when not to choose this model. If the application needs transactions across rows, ad-hoc queries on fields other than the key, or joins, a wide-column store will make the team build those features badly in application code. Bigtable suits large data with a small number of well-understood access patterns: time series keyed by entity, crawl and event data, per-user state, and analytic scans by key range.

How to read the original

Read the data model section and the API section first; together they are only a few pages, and the Webtable example in them is the best way into the system. Then read the implementation section on tablet location, tablet assignment and tablet serving, with the failure walk-through above in mind. The refinements section, covering locality groups, compression, caching, Bloom filters, the shared commit log and recovery speed-ups, is where most of the ideas that other systems copied live, so read it slowly.

The performance evaluation measures sequential and random reads and writes and scans as the number of tablet servers grows. Do not look for headline numbers; look at how the authors explain the curves. Aggregate throughput rises with more servers but far from linearly, and the paper attributes this to load imbalance and, for random reads, to fetching a whole SSTable block from GFS to return one small value. That explanation tells you more about the system than the figures do. Then read the section on real applications, which describes how a handful of Google products used the system, and the lessons section, which may be the most useful three pages in the paper.

The next paper, Kafka, takes the commit log, which Bigtable treats as a recovery detail, and makes it the product: a durable, partitioned log that many systems read at their own pace.