Redis data types.

Lists

LPUSH q "item1" "item2"
RPUSH q "tail"
LPOP q
RPOP q
BLPOP q 5              # block 5s waiting
BRPOP q 5
LLEN q
LRANGE q 0 -1          # all
LRANGE q 0 9           # first 10
LINDEX q 0
LREM q 0 "value"       # remove all
LTRIM q 0 99           # keep first 100
LSET q 0 "new"
LINSERT q BEFORE "x" "y"
LMOVE src dst LEFT RIGHT     # atomic move
BLMOVE src dst LEFT RIGHT 5  # blocking

Use for queues, recent items.

Hashes

HSET user:1 name "Alice" email "[email protected]"
HGET user:1 name
HGETALL user:1
HMGET user:1 name email
HDEL user:1 email
HLEN user:1
HKEYS user:1
HVALS user:1
HEXISTS user:1 name
HINCRBY user:1 logins 1
HINCRBYFLOAT user:1 score 1.5
HSETNX user:1 created NOW
HSCAN user:1 0 MATCH "addr*"

For object-like values.

Sets

SADD tags:1 "go" "rust"
SREM tags:1 "go"
SMEMBERS tags:1
SISMEMBER tags:1 "go"
SCARD tags:1
SPOP tags:1
SRANDMEMBER tags:1 3
SUNION tags:1 tags:2
SINTER tags:1 tags:2
SDIFF tags:1 tags:2
SINTERSTORE result tags:1 tags:2
SSCAN tags:1 0 MATCH "*"

Unique unordered collections.

Sorted sets (zsets)

ZADD lb 100 "alice"
ZADD lb 95 "bob" 87 "carol"
ZADD lb GT 110 "alice"     # update if greater
ZINCRBY lb 5 "alice"
ZRANGE lb 0 -1 WITHSCORES
ZRANGE lb 0 9 REV WITHSCORES        # top 10 desc
ZRANGEBYSCORE lb 80 100
ZRANGEBYSCORE lb -inf +inf LIMIT 0 10
ZRANK lb "alice"
ZREVRANK lb "alice"
ZSCORE lb "alice"
ZCARD lb
ZCOUNT lb 80 100
ZREMRANGEBYSCORE lb -inf 50
ZREMRANGEBYRANK lb 0 99       # bottom 100
ZUNIONSTORE result 2 lb1 lb2 WEIGHTS 1 0.5
ZPOPMIN lb 1
ZPOPMAX lb 1
BZPOPMIN lb 5                  # blocking

Leaderboards, time-series indexes, priority queues.

Bitmaps

SETBIT online:2026-01-15 12345 1    # user 12345 online
GETBIT online:2026-01-15 12345
BITCOUNT online:2026-01-15
BITOP AND today_and_yesterday online:today online:yesterday
BITPOS key 1                         # first set bit
BITCOUNT online:2026-01-15 0 -1 BYTE

Massively compact for boolean per-user data (~125MB for 1B users).

HyperLogLog

PFADD users:today user1 user2
PFCOUNT users:today
PFMERGE users:weekly users:mon users:tue users:wed

Cardinality estimation with ~0.81% error in 12KB. For counting unique visitors etc.

Geo

GEOADD pois 13.4 52.5 "Brandenburg Gate"
GEOPOS pois "Brandenburg Gate"
GEODIST pois "a" "b" km
GEOSEARCH pois FROMMEMBER "Brandenburg Gate" BYRADIUS 5 km
GEOSEARCH pois FROMLONLAT 13.4 52.5 BYBOX 10 10 km ASC COUNT 10

Backed by zset; supports radius/box queries.

Streams

XADD stream * key1 v1 key2 v2
XADD stream MAXLEN 10000 * key v
XLEN stream
XRANGE stream - +
XRANGE stream 1700000000-0 +  COUNT 100
XREAD COUNT 10 STREAMS stream 0
XREAD BLOCK 5000 STREAMS stream $
XGROUP CREATE stream grp $ MKSTREAM
XREADGROUP GROUP grp consumer1 COUNT 10 STREAMS stream >
XACK stream grp message-id
XPENDING stream grp

Reliable pub/sub with consumer groups. Use over pub/sub for new systems.

JSON (RedisJSON)

JSON.SET user:1 $ '{"name":"Alice","age":30}'
JSON.GET user:1
JSON.GET user:1 $.name
JSON.SET user:1 $.age 31
JSON.ARRAPPEND user:1 $.tags '"go"'
JSON.DEL user:1 $.email

Requires redis-stack (Redis Stack or Redis 8+).

Probabilistic (Redis Stack)

BF.ADD filter item                  # Bloom filter
BF.EXISTS filter item
CF.ADD filter item                  # Cuckoo
TOPK.ADD topk item
CMS.INCRBY cms item 1               # Count-min sketch

SCAN over each type

SCAN 0 MATCH "user:*"
HSCAN hash 0 MATCH "addr*"
SSCAN set 0
ZSCAN zset 0

Non-blocking iteration.

Choosing the right type

NeedType
Counterstring + INCR
Cachestring with TTL
Sessionhash
Queuelist (lpush/brpop) or stream
Recent itemslist (LPUSH + LTRIM)
Unique setset
Leaderboardzset
Tagsset or zset
Time-seriesstream or zset
Pub/substream (preferred)
BloomBF.*
Online/offline trackingbitmap
Geogeo (zset)

Common mistakes

  • Storing JSON as string when hash is better.
  • Using lists for unique items (use sets).
  • KEYS in prod.
  • Forgetting to TTL cache entries.
  • Big values (>1MB) bloating memory.

Read this next

If you want my Redis recipes, they’re at rajpoot.dev .


Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .