Why five million scores take less memory in Valkey 9.2
Changing how Valkey keeps scores in order saved 109 MB when entries arrived in sequence. Shuffling the same entries changed how tightly they packed.
A player’s name can take less space in a leaderboard than the bookkeeping needed to put it in the right place. In a Valkey test with five million entries, each 20-byte member carried an average of 50.3 additional bytes of overhead in the old ordered index. Valkey 9.2, released on November 19, brings that overhead down to 28.5 bytes when the entries arrive in order. The interesting part is what happens when they arrive out of order.
Two questions for the same leaderboard
“What is my score?” and “Who is immediately ahead of me?” look like adjacent features on a game screen. They ask different things of the database. Valkey can use a hash table to look up a particular member’s score. Finding the next ten players by rank calls for an index that keeps the scores in order. Version 9.2 changes the second structure for large sorted sets. The first is still there. How Valkey looks up an individual score.
Previously, the ordered index used a skiplist. Think of a chain of entries with shortcuts spanning different distances. Each member needs links and information about how many positions a shortcut skips. Those links cost memory. They also mean the processor may have to fetch one entry before it knows where the next one lives.
The replacement, a B+ tree, lets many members share the same navigation machinery. In Valkey’s implementation, a 512-byte leaf node holds up to 61 pointers to values. Nodes higher in the tree steer a search towards the correct leaf. Once there, a range query can continue into the neighbouring leaf without making the trip back to the top. The engineering account explains how those dimensions fit the allocator Valkey uses.
What changes when the scores come in shuffled
With the same five million members inserted in random order, overhead in the new index averaged 32.0 bytes per member. Random inserts left more leaves partly full. Compared with the old index, that still saved roughly 92 MB in the project’s test. Sequential insertion saved about 109 MB. Neither figure includes a reduction in the players’ names, which still take the same space. Nor does it measure the memory of the entire database. The merged implementation and benchmark comparison.
For an existing application, the most immediate compatibility check might actually be in its monitoring. A large sorted set now reports btree to OBJECT ENCODING. A small one can still report listpack, its compact representation. A dashboard that expects the literal string skiplist can therefore raise an alarm on a healthy upgrade. Valkey’s release notes describe what happens as a leaderboard shrinks. When members leave, Valkey can compact sparsely populated leaves and give memory back.
If you are estimating savings for your own leaderboard, start with how many members it contains and how scores arrive. A few long strings and millions of short player IDs can occupy similar amounts of memory today. Only one of those workloads gets to save a little bookkeeping several million times.