Systemsfor Aravinda
0 / 78 core
Week 12 · Production stories

Google Bigtable

1 hours · core path

Read first: Indexes: B-trees and LSM trees, Partitioning and sharding

In one sitting

Bigtable, published in 2006, is a sparse sorted map addressed by row, column family, qualifier, and timestamp. Tablets are row ranges served by tablet servers and assigned by a master whose lease and metadata live in Chubby. Writes land in a memory table and a log, then flush to immutable SSTables on GFS.

Bigtable is a map with a careful key. You name a row, a column, and a timestamp, and you get back bytes. Most of the names you could form were never written, and the store does not spend space on the holes. Chang and others published that design at OSDI in 2006. The idea to keep in your head is the map, cut into tablets by row range, with writes going to a memory table and a log.

The problem

Some workloads want a row that is wide, sparse, and kept in sorted order. A user has a handful of profile fields and a long tail of optional attributes. Another row has columns that exist only because someone wrote them. A relational table that materializes every possible column wastes space, and a pure key-value blob throws away the ability to scan a range of related rows on one server.

The access pattern that falls out of that data is specific. Point reads of one row. Scans along the row order, because nearby keys are the query. Writes that should not turn into a random page update of a B-tree if the workload is append-heavy. Access control that can cut the row into groups of columns, because not every reader may see every column.

Bigtable’s answer is to make the row key the unit of placement, the column family the unit of locality and of access control, and the on-disk file an immutable sorted run. You can redraw the system from those three choices. Everything else is how they are operated.

A new grad loop rarely asks you to recreate Google’s stack. The evening is worth it because the mistakes are portable. A monotonic row key melts one server in any range-partitioned store. A column you always read, packed beside a blob you almost never read, makes every common read expensive. Those two bugs are Bigtable bugs, and they are also bugs in systems that have never heard the name.

The design

The map. A cell is addressed by a row key, a column family, a qualifier inside that family, and a timestamp. The value is an uninterpreted string of bytes. Sparse means a missing cell costs nothing. You can have a huge space of qualifiers and store only the ones a writer actually used. Sorted means the row keys have an order, and that order is the order tablets use to draw their borders. The timestamp means one row and column can hold more than one version, side by side.

Tablets. Rows are split into tablets by key range. This is range partitioning. A tablet is a contiguous span of row keys, served by one tablet server. A scan whose start and end fall inside one span is one server’s job. A scan that crosses a border is more than one server, and you should expect that when you design the key.

The master assigns tablets to tablet servers. It does not serve the cell reads and writes. Clients talk to the tablet server that holds the range. Chubby, a lock service, holds the master lease and the metadata. The lease is the fencing idea from replication: one master is allowed to assign, and a second master does not also assign. The metadata is how an assignment has a place to live so a new master can learn it. Two masters assigning the same tablet to two servers is split brain. The lease is what stands in front of that failure.

The write path. A write goes to a memory table and to a log. The memory table is the recent data, in order, in RAM. The log is the durability. If the tablet server dies, the log is replayed to rebuild the memory table. When the memory table is flushed, it becomes an immutable SSTable on GFS. That is the LSM shape: absorb writes in memory, persist a log, flush sorted immutable files, compact those files later so a read does not open an ever-growing pile.

A point read looks in the memory table and then in the SSTables that might hold the key, until it finds a value or runs out of files. Several files per read is read amplification. Compaction rewrites files into fewer runs and spends write bandwidth and temporary space to buy that reduction back. You can push read amplification down by pushing write amplification and space amplification up. That trade is the LSM trade, and Bigtable is where a lot of people meet it.

A bloom filter is the later, standard way to skip an SSTable that cannot contain the key. It is an optimization on the read of those files. The 2006 design is the map, the tablet, and the log-and-flush path. Remembering only the filter leaves you unable to draw the system.

Column families. A family is stored together, so a read of the family returns those columns without the columns that live elsewhere. A family is also the unit of access control, so a permission boundary wants to be a family boundary. Those two jobs share one mechanism. If you need to separate them, you have to split the family, not attach a second policy to half of it.

Put columns that are read together in one family. Put a large, rarely read blob in another. A layout that places both in one family will drag the blob into the common read, because locality is the family, not the qualifier you wished you had asked for.

A key you can calculate. The sizes below are assumptions, so you can see the shape. They are not tablet sizes from the paper.

assumed table          1 billion rows
assumed rows per tablet 10 million
tablet count           1e9 / 1e7 = 100

row key = write timestamp
new keys are the largest keys
new writes land on the last tablet
that one server takes the write rate
the other 99 take none of it

Change only the key. Prefix it with something that spreads, such as a user id, and put time after that prefix. Writes for different users fall in different ranges, so they spread across the 100 tablets. A scan of one user’s rows is still a contiguous span, because that user’s keys share a prefix and the timestamp orders them inside it. The hot range did not move because you bought more tablet servers. It moved because the key stopped sending every new write to one end of the order.

flowchart TD
  client[Client]
  tablet[Tablet server]
  mem[Memory table]
  log[Commit log]
  files[SSTables on GFS]
  master[Master]
  chubby[Chubby lease and metadata]
  client --> tablet
  tablet --> mem
  tablet --> log
  mem --> files
  chubby --> master
  master --> tablet

The client path ends at the tablet server. The master path is assignment. Mixing those paths, by sending every read through the master, would make the master the hot range of the whole cluster.

What they traded

Choice What you gain What you spend
Tablets as row ranges A range scan stays on one server when the keys are neighbors A monotonic key piles every new write onto the last tablet
Master assigns, tablet servers serve The data path does not funnel through one assigner You operate assignment, leases, and metadata as their own failure domain
Chubby holds the lease and the metadata One fenced master, and a place for assignments to live Progress of assignment depends on the lock service
Memory table plus a log, flushed to SSTables Fast writes and a crash story that is “replay the log” Reads touch memory and several files until compaction rewrites them
Column family as locality and access control Common reads stay small, and permissions have a boundary A bad family layout, or a permission split that does not match the read, fights the storage layout
Immutable files on GFS A crash does not tear a half-updated page Space until compaction deletes the files you rewrote

The hot-range failure is the one to tell in a design review. The key looks harmless because timestamps are unique and ordered. Uniqueness is not spread. Ordered-by-now means “the rightmost tablet.” That server’s memory table fills, its log is the only log that grows at the full write rate, and its flushes produce the SSTables everyone then reads. Adding tablet servers gives you empty ranges on the left and the same fire on the right.

The split-brain failure is the lease. Suppose the master loses its Chubby session and a replacement starts assigning, while the old master still believes it may assign. One tablet is handed to two servers. Both accept writes into their own memory tables and their own logs. Readers see a range that depends on which server they ask. The lease exists so the old master is fenced before the new one acts. A generation or a lease that you do not check is the same bug as two primaries.

The locality failure is quieter. A page-view read needs a counter. The counter’s family also holds a blob written once a year. Every page view reads the blob off GFS because the family is the unit of locality. The read amplification you measure is not compaction. It is the schema. Splitting the family fixes it. A bloom filter, even once you have one, only skips files that cannot contain the key. It does not skip a blob that really is in the file you had to open.

What to steal

Steal the row key as an explicit design, not as an id you happened to have. Write down the scan you need. If it is “this user’s recent rows,” the user belongs in the prefix and time belongs after it. If you put time first, you have chosen a single-server write path, and you should say that out loud before you are surprised by it. The same sentence applies to any store that partitions by range, which is why the 2006 paper still pays rent.

Steal the family split. Columns read on every request live apart from columns read on rare requests. If access control cuts the row differently from the read pattern, notice that a family is both boundaries at once. You will have to pick a split that honors the stricter of the two, or you will enforce permissions in the application and still pay the bytes.

Steal the write path only if you are actually building an LSM or explaining one. Memory table, log, immutable sorted files, compaction. That is the whole story a new grad needs. A team of five should not stand up a tablet master, a lock service, and a distributed filesystem to get it. Use a database that already has a primary and an LSM or a B-tree. Then spend the afternoon on the key. A bad key in a hosted range-partitioned table produces the same hot tablet the paper would have produced, and the vendor’s master will not save you.

Treat bloom filters as a later optimization on those files. They are the standard way to avoid opening an SSTable that cannot hold the key. They are a good tool in an LSM you operate. They are not the answer to “what is Bigtable,” and they do not fix a monotonic row key or a family that packs a blob next to a counter.

Check yourself

Row keys are the write timestamp, and tablets are ranges of that key. Why do new writes pile onto one tablet server?

Tablets split the key order into spans. A timestamp key makes every new write larger than the previous ones, so it falls in the rightmost span. That one tablet server takes the write rate. Extra servers own other spans and sit idle on writes. Spread requires a key whose prefixes fall in different ranges. The pile-up is the key, not a shortage of machines.

What does Chubby hold for Bigtable, and what failure appears if two masters assign tablets anyway?

Chubby holds the master lease and the metadata. The lease fences the master so only one assigner is valid. Two masters assigning the same tablet to two servers is split brain: both accept writes, and readers see whichever server they reach. The tablet servers are what serve the data. The master is what assigns.

A frequent read of a small counter also pulls back a large blob. What did the column-family layout get wrong?

A column family is the unit of locality. The counter and the blob were placed in the same family, so the common read of the counter reads the blob too. Put the rarely read blob in another family. A family is also the unit of access control, so the split has to satisfy both jobs.

A tablet server crashes after accepting a write and before flushing an SSTable. Where is the write, and what brings it back?

The write went to the memory table and to the log. The memory table died with the process. The log is durable, so recovery replays it and rebuilds the memory table. The SSTable is the later flush of that memory, an immutable file on GFS, not the only copy of a write that just happened.

Based on: Bigtable, OSDI 2006, Chang and others.