Databases 24 min read

Master Redis: Explore All 8 Data Types and Essential Commands

This guide walks through Redis’s eight core data structures—String, List, Set, Hash, Sorted Set, Geospatial, HyperLogLog, and Bitmap—detailing their commands, practical usage scenarios, and example interactions to help developers ace interview questions and optimize NoSQL solutions.

Programmer DD
Programmer DD
Programmer DD
Master Redis: Explore All 8 Data Types and Essential Commands

NoSQL development often involves Redis, a key‑value store that appears frequently in interview questions.

Recent interviews have highlighted the need to master its data structures; this article consolidates essential Redis knowledge.

Redis’s Eight Data Types

String (字符串)

Basic key‑value type. Supports set, get, append, incr/decr, range, setrange, setex, setnx, mset/mget, getset, etc.

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name xxx
OK
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> exists name   # 判断key是否存在 (integer) 1
127.0.0.1:6379> move name 1   # 将key移动到指定db (integer) 1
127.0.0.1:6379> expire name 10   # 设置key的过期时间,单位秒 (integer) 1
127.0.0.1:6379> ttl name   # 查看剩余过期时间 (integer) 7
127.0.0.1:6379> type age   # 查看key类型,返回string

Typical scenarios: counters, statistics, fan counts, object caching.

List (列表)

Ordered collection, usable as stack, queue, or blocking queue. Most commands start with l.

127.0.0.1:6379> lpush list one   # left push
127.0.0.1:6379> lpush list two
127.0.0.1:6379> lpush list three
127.0.0.1:6379> lrange list 0 -1   # view all elements
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> rpush list right   # right push
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> lpop list   # pop from left
"three"
127.0.0.1:6379> rpop list   # pop from right
"right"
127.0.0.1:6379> lindex list 1   # get element by index
"two"
127.0.0.1:6379> llen list   # length
(integer) 2
127.0.0.1:6379> lrem list 1 world   # remove specific value
(integer) 1
127.0.0.1:6379> ltrim mylist 1 2   # keep elements 1~2

Key points: list is a linked list; insertion at both ends is O(1); empty list is considered non‑existent.

Set (集合)

Unordered collection of unique strings.

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
127.0.0.1:6379> sismember myset hello
(integer) 1
127.0.0.1:6379> scard myset   # cardinality
(integer) 2
127.0.0.1:6379> srem myset hello   # remove element
(integer) 1

Set operations: union, intersection, difference, random member, move between sets.

Hash (哈希)

Map of field‑value pairs stored under a single key.

127.0.0.1:6379> hset myhash field xxx
(integer) 1
127.0.0.1:6379> hget myhash field
"xxx"
127.0.0.1:6379> hmset myhash field1 hello field2 world
OK
127.0.0.1:6379> hgetall myhash
1) "field"
2) "xxx"
3) "field1"
4) "hello"
5) "field2"
6) "world"
127.0.0.1:6379> hdel myhash field1
(integer) 1
127.0.0.1:6379> hincrby myhash field3 1   # increment numeric field
(integer) 6

Ideal for storing mutable objects; strings are better for plain text.

Sorted Set (zset, 有序集合)

Set of unique members with a floating‑point score, enabling range queries by score.

127.0.0.1:6379> zadd myset 1 one
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 xiaoming
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "xaiozhang"
2) "500"
3) "xiaohong"
4) "2500"
5) "xiaoming"
6) "5000"
127.0.0.1:6379> zrem salary xiaohong   # remove member
(integer) 1
127.0.0.1:6379> zcard salary   # cardinality
(integer) 2

Geospatial

Introduced in Redis 3.2, stores longitude/latitude as a sorted set.

127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
(integer) 1
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
127.0.0.1:6379> geodist china:city beijing shanghai km
"1067.3788"
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "chongqing"
2) "xian"

Underlying implementation uses a sorted set, so all ZSET commands apply.

HyperLogLog

Probabilistic data structure for cardinality estimation with fixed 12 KB memory.

127.0.0.1:6379> pfadd mykey a b c d e f g h i j
(integer) 1
127.0.0.1:6379> pfcount mykey
(integer) 10
127.0.0.1:6379> pfadd mykey2 i j z x c v b n m
(integer) 1
127.0.0.1:6379> pfcount mykey2
(integer) 9
127.0.0.1:6379> pfmerge mykey3 mykey mykey2
OK
127.0.0.1:6379> pfcount mykey3
(integer) 15

Bitmap

Bit‑level representation for boolean flags, useful for daily sign‑in, UV counting, etc.

127.0.0.1:6379> setbit sign 0 1   # Monday signed
(integer) 0
127.0.0.1:6379> setbit sign 3 1   # Thursday signed
(integer) 0
127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> bitcount sign
(integer) 4   # total signed days
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseredisData TypesNoSQLcommands
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.