Home/Labs/Redis Structure Costs
All 280 Labs
INTERACTIVE LAB⚡

Redis Structure Costs Lab (Interactive)

Size leaderboards, visitor counts, and rate limits per structure, then pick persistence. Compare ZSet vs string, Set vs HyperLogLog, and sliding-window rate limiter RAM and work as cardinality grows — and see exactly what each persistence mode loses on crash.

Redis Data-Structure Economics & Persistence

Same feature, different structures: RAM and CPU differ by orders of magnitude — then decide what survives a restart.

ZSET (skiplist + dict) 🏆720 MB
ZREVRANGE 0 9 → O(log N + 10): touches ~rank+10 nodes
work per query ≈ 3e+1 ops · ratio 100% of worst RAM
STRING scores + app sort 300 MB
SMEMBERS-equivalent scan pulls ALL scores to the client, then sorts
work per query ≈ 2.3e+8 ops · ratio 42% of worst RAM
Persistence engine (what survives a kill -9?)
Crash data loss
50,000 writes
Recovery time
0.2 min AOF replay
fsync IOPS cost
1/s
Single-thread note
KEYS * on 10M keys = 1000 ms frozen loop — use SCAN

append every write, fsync once per second. AOF rewrite compacts 1,000 INCRs into one SET — the same trick every append-only log uses.

How It Works Under the Hood

Redis serves 100k+ QPS per core by running a single-threaded epoll loop over RAM-native structures, so structure choice is architecture. A ZSet keeps a skip list for O(log N) ranking — the never-rescrambling leaderboard; a JSON string forces an app-side sort. HyperLogLog holds cardinality in fixed 12KB with ~0.81% error where a Set spends ~30 bytes per member. Persistence is orthogonal: RDB forks snapshots for fast restarts, AOF everysec loses up to a second, AOF always fsyncs every command. And KEYS * blocks the one thread everything waits on.

Core Architectural Principles

  • ZSets give O(log N) insert and top-K reads via skip lists — the only sane structure for a live leaderboard.
  • HyperLogLog holds cardinality in a fixed 12KB with ~0.81% error where a Set burns RAM per member.
  • RDB vs AOF everysec vs AOF always span the recovery-time versus lost-writes trade, while KEYS * blocks the single-threaded event loop.
Interview Round Script

When asked to design a leaderboard, rate limiter, or unique-visitor counter on Redis, name the structure and its complexity before any config. Quote HyperLogLog fixed 12KB and error rate, ZSET sliding-window limiting with ZREMRANGEBYSCORE, and the single-threaded event loop. For pure caches say persistence can be skipped; for stateful data reason RDB versus AOF explicitly.

Key Trade-Offs

Rich in-memory structures deliver microsecond semantics at RAM prices; persistence modes just choose which loss you accept — recent writes or slow warm boots.

Related Curriculum Chapter

Key-Value Stores (Redis, DynamoDB, In-Memory Mechanics)

Read Full Chapter Blueprint

Explore More Interactive Labs

View All 280 Labs