Skip to content

Sorted Set

Score rankings are hard.

Tens of thousands of users hit the ranking page in real time. ORDER BY in an RDBMS cannot keep up. The top 100 are easy with LIMIT. The painful part is "my rank." Calculating where you stand among everyone requires a full sort. The answer is Redis's Sorted Set. Insert members with their scores. Redis keeps them ordered. Your rank comes back in O(log N).

Tie-breaking is a classic problem. Same score, members sort lexicographically. Same score, different rank. The trick: embed a UNIX timestamp in the decimal portion of the score. Whoever reached it first ranks higher. Brute force, but it works. Going the other way — grouping ties into one rank — Sorted Set alone cannot do. You need a separate aggregation step.

Redis and Memcached are often compared, but they differ sharply. Redis is single-threaded. To use multiple cores you need sharding. Memory is halved in practice because fork doubles usage during persistence snapshots. Data structures are rich: strings, hashes, lists, sets, Sorted Sets. Persistence is built in. Less a key-value store, more a data store outright.

Memcached is multi-threaded. A pure KVS and nothing more. It manages memory with a slab allocator. Fixed-size chunks, data routed to the nearest matching slab. When the size distribution of your data mismatches the slabs, certain slabs overflow while others sit empty. Plenty of memory left, yet eviction fires. Simple, and for that reason, fast. People used to warm up slabs manually. Whether anyone still does, I cannot say.

AWS offers both Redis and Memcached as managed services under ElastiCache. The reason is that the use cases differ. Session stores and plain caches: Memcached is enough, and multi-threading makes it efficient. Data structures and persistence: Redis. Fewer and fewer junior engineers can explain this difference in performance characteristics.