Introduction
In our last lesson, we dissected Bigtable's architecture, revealing how it leverages GFS/Colossus for durable storage and Chubby for coordination. We saw that when a client writes data, the mutation is first logged for durability and then inserted into an in-memory memtable. When this memtable grows too large, it is flushed to the underlying distributed file system as a new, immutable SSTable. This design choice is fundamental to Bigtable's high write throughput.
However, it also introduces a critical challenge: over time, a single tablet can become represented by one memtable and a growing number of SSTables. This lesson addresses the mechanism Bigtable uses to manage this complexity. Your objective is to explain the purpose and mechanics of minor and major compactions. Understanding this process is vital for appreciating how Bigtable maintains high read performance, reclaims disk space, and ensures data is eventually permanently deleted, all crucial aspects for a Principal Engineer to grasp.
The Write Path and the Accumulation of SSTables
Every write operation in Bigtable targets a tablet server. The server appends the mutation to a commit log stored in GFS/Colossus to ensure durability and then applies the change to the memtable. This memtable is a sorted data structure in memory, allowing recent writes to be served quickly.
When the memtable reaches a configured size threshold, a process called minor compaction is triggered. The tablet server freezes the current memtable, creates a new one to accept incoming writes, and writes the contents of the frozen memtable to a new SSTable file in GFS.
This leads to a situation where a single tablet consists of:
- The current, mutable
memtablein memory. - One or more immutable SSTables on GFS, with the most recent SSTable containing the newest flushed data.
To satisfy a read request, Bigtable must query the memtable and all the SSTables for that tablet, merging the results to find the correct version of the data for a given key. As the number of SSTables grows, this "read amplification" makes reads progressively slower. Furthermore, since SSTables are immutable, updated or deleted data is not removed from older files; it is simply superseded by a newer value or a deletion marker (a "tombstone") in a more recent SSTable. This "space amplification" wastes disk space.
Compactions are the solution to both of these problems.
Minor Compaction: Checkpointing Memory to Disk
Let's begin with the formal definition and goals of minor compaction, as described in the original Bigtable paper.
Bigtable: A Distributed Storage System for Structured Data
This section of the paper defines the process of flushing the memtable to an SSTable and explicitly states its primary goals.
Please read the first paragraph of Section 5.4, "Compactions." It describes the process when a memtable is frozen and converted to an SSTable, and lists its two main goals.
As you've just read, a minor compaction is the process of writing the in-memory memtable to a new SSTable on disk. Its two primary purposes are:
- To reduce the memory footprint of the tablet server. By flushing the
memtableto disk, the server frees up memory to buffer new writes. - To reduce recovery time. The commit log only needs to be replayed for writes that are not yet persisted in an SSTable. By creating a new SSTable, the minor compaction acts as a checkpoint, significantly shortening the list of log entries that must be replayed if the tablet server fails and another one has to take over its tablet.
The Bigtable paper also notes a clever use case for this process in Section 6, "Refinements." When the master needs to move a tablet from one server to another, it instructs the source server to perform a minor compaction first. This minimizes the state that exists only in the commit log, allowing the new server to load the tablet much faster without needing to replay many log entries.
Merging and Major Compactions: Taming the SSTable Stack
Minor compactions solve the memory and recovery-time problems, but they create the read-amplification and space-amplification problems by continuously creating new SSTables. To solve these, Bigtable employs further compactions that merge existing SSTables.
The paper describes two types of merging compactions:
- A merging compaction reads a few SSTables and the
memtableand merges them into a new, single SSTable. - A major compaction is a specific type of merging compaction that merges all SSTables for a tablet into a single, definitive SSTable.
Bigtable: A Distributed Storage System for Structured Data
Now, let's read the paper's description of how merging and major compactions manage the number of SSTables and handle deleted data.
Please read the rest of Section 5.4, "Compactions." Focus on the distinction between a merging compaction and a major compaction, and what a major compaction accomplishes regarding reclaiming resources.
A major compaction is a garbage collection and optimization process with three key outcomes:
- Improves Read Performance: By consolidating all data into a single SSTable, a read operation only needs to check that one file (and the
memtable), drastically reducing read amplification. - Reclaims Disk Space: It physically discards data that has been overwritten or deleted. During the merge, only the most recent value for each key is kept. If the most recent entry is a tombstone, both the tombstone and the old data are discarded, reclaiming space.
- Ensures Timely Deletion: For systems handling sensitive data, this process guarantees that deleted data is permanently removed from storage in a predictable timeframe, as Bigtable periodically runs major compactions on all tablets.
To see this in action, the following video provides a clear, practical example of how values from two SSTables are merged into one.
BigTable - One Database to Rule Them All?. | Distributed Systems Deep Dives With Ex-Google SWE
This segment from the "Jordan has no life" video on Bigtable visually walks through the merging of two SSTables, showing how updates and deletions are resolved.
Please watch the section from "major compactions", which demonstrates how a new table is formed with the latest values and how tombstones lead to the removal of data.
The Underlying Data Structure: Log-Structured Merge-Trees
The combination of an in-memory table for recent writes and a set of immutable, sorted files on disk that are periodically merged is not unique to Bigtable. This is a well-known data structure pattern called a Log-Structured Merge-Tree (LSM-Tree).
Given your background, recognizing this pattern is key. LSM-Trees are at the heart of many modern NoSQL databases like Apache Cassandra, RocksDB, and LevelDB (the latter two being open-source projects heavily inspired by Bigtable's design). They are designed to optimize for high write throughput by converting random write I/O into sequential I/O (appending to a log and flushing sorted files). The cost of this optimization is increased read complexity and space usage, which is then mitigated by the compaction process we've just discussed.
The following video from ByteByteGo provides an excellent, concise overview of the LSM-Tree concept from first principles.
The Secret Sauce Behind NoSQL: LSM Tree
This video explains the entire lifecycle of an LSM-Tree, from writes being buffered in memory to the problems that arise and how compaction solves them. It provides the general theory behind Bigtable's specific implementation.
Watch the video from the beginning until the recap. This will connect all the pieces we've discussed: memtables, SSTables, tombstones, and the role of compaction.
As the video mentions, there are different strategies for compaction. The Bigtable paper describes a simpler form, often called "size-tiered compaction." Modern systems frequently use a more sophisticated approach called "leveled compaction," where SSTables are organized into levels of exponentially increasing size. Compaction then involves merging a file from Level N with overlapping files in Level N+1. This strategy provides more predictable performance bounds for reads and space usage.

Conclusion
In this lesson, we have explored the critical role of compactions in Bigtable's storage engine. You have learned how Bigtable balances high write throughput with the need to maintain read performance and manage disk space efficiently.
Here are the key takeaways:
- Minor Compaction is the process of flushing the in-memory
memtableto a new SSTable on disk. Its primary goals are to free up memory and create a recovery checkpoint. - Major Compaction merges all existing SSTables for a tablet into a single new SSTable. This is a garbage collection process that improves read performance, reclaims disk space from old or deleted data, and ensures sensitive data is permanently removed.
- This entire mechanism is an implementation of the Log-Structured Merge-Tree (LSM-Tree), a data structure that prioritizes high write throughput by transforming random writes into sequential ones, with compactions handling the resulting read and space amplification.
We have now covered Bigtable's data model and its core architectural components, including the specifics of its storage layer. Bigtable represents a landmark in scalable NoSQL design. Next, we will shift our focus to another titan of Google's infrastructure: Spanner. In the upcoming lesson, we will begin to analyze Spanner's high-level architecture, a system that takes on the even more ambitious goal of providing global-scale, externally consistent, relational database semantics.