{
  "id": 20995015,
  "title": "From Skiplists to B+ Trees: Making Valkey Sorted Sets More Memory Efficient",
  "url": "https://urgent.news/2026/11/21/from-skiplists-to-b-trees-making-valkey-sorted-sets-more-memory",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-21T04:31:29.000Z",
  "source": {
    "name": "Lobsters",
    "slug": "lobsters",
    "url": "https://valkey.io/blog/from-skiplist-to-b+tree/"
  },
  "original_language": "en",
  "account": "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.\n\nTests 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.\n\nTraditionally, 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.\n\nThe 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.\n\nThe 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.\n\nfbtree 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.",
  "summary": null,
  "key_points": [],
  "editors_take": "Replacing skiplists with the fbtree structure shifts Valkey toward a high-fanout architecture that prioritizes cache efficiency and reduced pointer overhead to optimize memory usage for large sorted sets.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}