From Skiplists to B+ Trees: Making Valkey Sorted Sets More Memory Efficient
Sorted sets in Valkey 9.2 can consume more memory describing their order than actually storing the elements. To address this, the skiplist has been replaced by an optimized B+ tree variant called fbtree. This new structure incorporates several features tailored to Valkey's workload and memory management, such as feature-based lookups, SIMD searches, linked leaves, and fast paths for sequential insertions and removals.
Tests on a set of five million elements showed that fbtree cut per-member memory usage by 36–43%, potentially saving up to 109 megabytes. The transition occurred when sorted sets exceeded a certain size, switching from a compact listpack representation to a B+ tree.
Traditionally, sorted sets are backed by a skiplist paired with a hashtable. The hashtable offers O(1) lookup times for individual elements, while the skiplist enables O(log n) ordered lookups by rank or score. However, the skiplist incurs a significant memory overhead, storing one element per node with scattered allocations connected by pointers.
Each node carries a backward pointer (8B), an average of 1.33 forward pointers, and span values, totaling around 37B of pointers and span overhead per node before including the score. This heavy memory usage stems from the probabilistic nature of skiplists, where each node has a 25% chance of getting an additional level.
The fbtree, or Feature B+ Tree, mitigates these memory concerns by using a high fanout approach. Each node can hold up to 61 entries or child pointers, significantly reducing the per-member memory overhead. Leaf nodes store up to 61 value pointers, sharing a node with up to 61 members, compared to each member getting its own node in the skiplist. Scores and member data are packed together, eliminating an extra level of pointer indirection.
The fbtree's 61-way fanout offers an effective branching factor of 61, compared to skiplist's 4, but both structures maintain O(log n) search complexity. The B+ tree's fewer levels to traverse result in fewer memory fetches, which is crucial as modern CPUs use cache hierarchies to optimize frequent data access. By keeping data in contiguous cache lines, fbtree reduces the need for expensive memory fetches and takes advantage of hardware prefetching.
fbtree further optimizes specific operations. For range commands like ZRANGE, it links leaf nodes in a doubly-linked list, allowing sequential access without climbing back up the tree. When working with variable-length strings, fbtree's inner nodes store shared prefixes separately from the anchor and use a four-byte feature to differentiate children, enabling parallel comparisons using SIMD instructions to potentially identify the correct child node before fetching it.
This parallelism can speed up operations such as ZPOPMIN, which targets the ends of the set, maintaining O(1) complexity for finding leftmost and rightmost leaves.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.