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