Login_oct15_05_bender
A B”-tree is an example of a write-optimized data structure and can be used to organize on-disk storage for an application, such as a database or file system.
A B”-tree provides a key- value API, similar to a B-tree, but with better performance, particularly for inserts, range queries, and key-value updates.
After finishing this article, a reader should have a basic understanding of how a B”-tree works, its performance characteristics, how it compares to other key-value stores, and how to design applications to gain the most performance from a B”-tree.
B”-trees were proposed by Brodal and Fagerberg [1] as a way to demonstrate an asymptotic performance tradeoff curve between B-trees [2] and buffered repository trees [3]. Both data structures support the same operations, but a B-tree favors queries, whereas a buffered repository tree favors inserts.
Researchers, including the authors of this article, have recognized the practical utility of a B”-tree when configured to occupy the “middle ground” of this curve—realizing query per- formance comparable to a B-tree but insert performance orders of magnitude faster than a B-tree.
A B”-tree is a B-tree-like search tree for organizing on-disk data,
as illustrated in Figure 1. Both B-trees and B”-trees export a key-value store API:
◆ insert(k, v)
◆ delete(k)
◆ v = query(k)
◆ [v1
, v2,…] = range-query(k1
, k2
)
Like a B-tree, the node size in a B”-tree is chosen to be a multiple of the underlying storage device’s block size. Typical B”-tree node sizes range from a few hundred kilobytes to a few megabytes.
In both B-trees and B”-trees, internal nodes store pivot keys and child pointers, and leaves store key-value pairs, sorted by key. For simplicity, one can think of each key-value or pivot-pointer pair as being unit size;
Thus, a leaf of size B holds B key-value pairs, which we call items below.
The distinguishing feature of a B”-tree is that internal nodes also allocate some space for a buffer, as shown in Figure 1. The buffer in each internal node is used to store messages, which encode updates that will eventually be applied to items in leaves under this node.
buffer is not an in-memory data structure; it is part of the node and is written to disk, evicted from memory, etc., whenever the node is.
The value of “, which must be between 0 and 1, is a tuning parameter that selects how much space internal nodes use for pivots ( ≈ B” ) and how much space is used as a buffer ( ≈ B − B” ).
Insertions are encoded as “insert messages,” addressed to a particular key and added to the buffer of the root node of the tree.
When enough messages have been added to a node to fill the node’s buffer, a batch of messages are flushed to one of the node’s children. Generally, the child with the most pending messages is selected.
Over the course of flushing, each message is ultimately delivered to the appropriate leaf node, and the new key and value are added to the leaf.
When a leaf node becomes too full, it splits, just as in a B-tree. Similar to a B-tree, when an interior node gets too many children, it splits and the messages in its buffer are distributed between the two new nodes.
Moving messages down the tree in batches is the key to the B”-tree’s insert performance. By storing newly inserted messages in a buffer near the root, a B”-tree can avoid seeking all over the disk to put elements in their target locations.
The B”-tree only moves messages to a subtree when enough messages have accumulated for that subtree to amortize the I/O cost. Although this involves rewriting the same data multiple times, this can improve performance for smaller, random inserts, as our analysis in the next section shows.
B”-trees delete items by inserting “tombstone messages” into the tree.
These tombstone messages are flushed down the tree until they reach a leaf. When a tombstone message is flushed to a leaf, the B”-tree discards both the deleted item and the tombstone message.
Thus, a deleted item, or even entire leaf node, can continue to exist until a tombstone mes- sage reaches the leaf. Because deletes are encoded as messages, deletions are algorithmically very similar to insertions.
A high-performance B”-tree should detect and optimize the case where a large series of mes- sages all go to one leaf. Suppose a series of keys are inserted that will completely fill one leaf. Rather than write these messages to an internal node only to immediately rewrite them to each node on the path from root to leaf, these messages should flush directly to the leaf, along with any other pending messages for that leaf.
Point and range queries. Messages addressed to a key k are guaranteed to be applied to k’s leaf or in some buffer along the root-to-leaf path towards key k.