Valkey Swaps Skiplist for B+ Tree, Cuts Memory Overhead by 43%
Valkey 9.2 replaces the skiplist data structure with a B+ tree variant called fbtree for sorted sets exceeding a certain size threshold. The skiplist approach carried substantial memory overhead: each node required roughly 37 bytes for metadata (pointers and span values) before the member data itself, and nodes scattered across memory created expensive cache misses and dependent memory fetches that required multiple round trips through the CPU cache hierarchy.
fbtree addresses both problems through higher fanout—61 entries or pointers per node instead of the skiplist’s effective branching factor of 4—and contiguous memory layout. In testing with 5-million-member sets, the new structure reduced per-member memory overhead by 36 percent for random inserts and 43 percent for sequential inserts, translating to roughly 92–109 MB savings per test set. The B+ tree’s narrower depth also reduces the number of dependent fetches needed during lookups while enabling CPU prefetching of adjacent cache lines.
fbtree incorporates several optimizations for Valkey’s workload: it uses SIMD-accelerated feature-based lookups to identify child nodes, links leaf nodes together for efficient range scans, caches the leftmost and rightmost leaves for O(1) pop operations, and handles range deletions without rebuilding adjacent structure. The change is internal; sorted set commands behave identically, with OBJECT ENCODING returning “btree” instead of “skiplist” as the only visible difference.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.