TRT
Sign in

Expert · Module 27 · Lesson 2 of 5

Redis sorted sets and skip lists

Inside Redis sorted sets: why ZSCORE is O(1) while ZRANK is O(log n), how the listpack-to-skip-list conversion works, and how randomly rolled levels make ranks cheap without any rebalancing.

Pro — members onlyAbout 13 minutes

By the end you can

  • State why a sorted set keeps both a member dict and a skip list, and name the operations each one serves
  • Trace a ZRANK walk through the levels and compute the rank it returns
  • Relate the level-promotion probability to the O(log n) expected cost of rank and range queries

Builds on Red-black trees, Choosing an ordered structure.

01 · A question first

ZADD one member and Redis updates two structures at once. ZSCORE on a set with ten million members returns instantly, while ZRANK walks a chain of pointers whose levels were rolled with a die at insert time. Nothing in the structure is ever rotated or rebalanced — so what keeps the walk short?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • One command, two structures
  • The bottom level and the levels above it
  • The trace above: four hops to a rank
  • Why random levels beat rotations
  • What each command costs
  • Pseudocode & Multi-Language Implementations (C++, Java, Python)
  • Complexity Derivation, Edge Cases & Common Mistakes

03 · Practice

Something wrong with this lesson? Report a problem