ML
Redis

Redis Data Structures Beyond Strings: When to Use What

Sorted sets, streams, hyperloglog, bitmaps — picking the right one is usually a 10x win on memory or latency.

September 22, 20258 min readRedisData Modeling

Strings are the lazy default. The fastest way to make Redis sing is to match your problem to the right structure.

Sorted set — ranked leaderboards, time-series, schedulers

ZADD leaderboard 1420 player:1
ZADD leaderboard 1388 player:2
ZREVRANGE leaderboard 0 9 WITHSCORES  # top 10

Also perfect for delayed queues: score = executeAt, worker runs ZRANGEBYSCORE 0 now LIMIT 0 1.

Hash — one record, many fields

Instead of user:42:name, user:42:email, user:42:age, use HSET user:42 name Minh email minh@x age 30. Below hash-max-listpack-entries (default 128) Redis packs it in a listpack — a dense array that's dramatically smaller than individual strings.

Stream — a log inside Redis

A persistent append-only log with consumer groups and acknowledgements. Poor-man's Kafka when you're already on Redis and latencies are short.

XADD orders * customer 42 amount 99
XREADGROUP GROUP workers c1 COUNT 10 BLOCK 5000 STREAMS orders >

HyperLogLog — cardinality in 12 KiB

Counting unique visitors over 30 days? PFADD/PFCOUNT gives you cardinality with 0.81% standard error using 12 KiB per HLL, merge-able across keys.

Bitmap — per-user flags at scale

SETBIT active:2026-04-24 USER_ID 1
BITCOUNT active:2026-04-24

A million active-user flags for a day fits in 125 KiB. BITOP OR across days gives monthly actives.

GEO — indexed geospatial

GEOADD locations 106.7 10.77 "office"
GEOSEARCH locations FROMMEMBER office BYRADIUS 2 km ASC

How to pick

If you ever find yourself keeping a sorted index "on the side," using INCR+SET pairs, or serializing a JSON blob just to update one field — step back. There's a dedicated structure waiting to be a 10× win.

SharePostLinkedIn

Reader Discussion

9 replies// weighed in

TopNewestAuthor
Add to the thread
Disagree, agree harder, or share your own experience…
Email instead →markdown okbe kind
  1. Highlighted by author
    Elena Ricci· Platform Eng · Booking infraFrom experience

    XFetch quietly killed our daily cache stampede. 6h TTL on a product catalog, three-instance API, used to brown-out for 90 seconds every refresh. Shipped XFetch on a Friday afternoon and forgot it existed. That's the highest praise I can give a fix.

    Sep 24, 2025·2 days later
  2. Huyền Lê· Software EngineerAgrees

    viết postmortem tiêu đề 'WAIT did not wait' xong 1 tuần sau gặp đoạn này trong post. cười ra nước mắt. cái phần WAIT không phải consensus primitive cần tô đỏ trong docs official.

    Sep 25, 2025·3 days later
  3. Amir Shah· InfraAsks

    Q: pre-warm hot keys — internal cron inside the app vs external scheduler (k8s cronjob etc)? We've shipped both. Internal is simpler but you fight clock skew across replicas; external is reliable but adds a moving piece.

    Sep 26, 2025·4 days later
    • ML
      Minh LeAuthor

      External, every time. The number of "why is the warmup not running" tickets I've seen with internal crons is not funny anymore. Make it boring infra.

      Sep 27, 2025
    • Carla Pérez· Backend

      we do external + a redis lock so only one instance actually runs the warmup. simple and observable.

      Sep 28, 2025
  4. Mark Vandermeer· Infra EngineerPushback

    RDB + AOF on the same instance is not a 'belt and suspenders' move btw — fsync-on-rewrite collisions can make latency vibrate. Pick one and tune it.

    Sep 30, 2025·1 week later
  5. Yuki Tanaka· Senior EngineerAgrees

    pipelining is so cheap and so under-used. converted a hot ticker loop from 30k cmd/sec to 30k cmd/sec but in 800 round-trips/sec instead of 30k. p99 dropped 4x. should be the first optimisation people reach for.

    Sep 25, 2025·3 days later
  6. Isabella Costa· Junior EngineerKind words

    saved this. sharing at standup tomorrow — we've had exactly this problem for 2 sprints and nobody on the team had framed it this way 🙏

    Sep 24, 2025·2 days later
  7. Kenta Yamada· Tech LeadAsks

    would love a war-story follow-up. principles are clear; the actual debugging session is where the interesting stuff lives. there's a real shortage of "here's the dashboard, here's the thread we pulled, here's where we got stuck for 90 mins" content.

    Sep 26, 2025·4 days later

Worked on something similar? Email ducminhldm@gmail.com — I read every one. The good ones become future posts.

Comments seeded · live discussion via email