Understanding Redis: Core Data Structures and Essential Commands
This article explains why Redis’s single‑threaded design achieves high performance, details its five fundamental data types—string, list, set, hash, and sorted set—their internal implementations, and introduces advanced structures such as Geo, Bitmap, and HyperLogLog with practical command examples.
1. Why single‑threaded Redis is fast
Most Redis operations execute entirely in memory using efficient structures such as hash tables and skip lists. Redis also uses I/O multiplexing (select/epoll), allowing a single thread to handle many client connections concurrently, which yields high throughput.
2. Basic Redis data structures
Redis provides five fundamental types: string , list , set , hash and sorted set (zset) . All keys are stored in a hash table similar to Java’s HashMap, with hash collisions resolved by chaining. Redis employs progressive (incremental) rehashing: the old and new tables coexist, and each processed command copies a small portion of entries from the old table to the new one, avoiding a pause‑time resize.
2.1 String
String is the simplest type, implemented as a dynamic string similar to Java’s ArrayList with pre‑allocated spare space. Expansion doubles the buffer up to 1 MiB, then grows by 1 MiB increments; the maximum length is 512 MiB. Strings are byte arrays, enabling bitmap usage.
127.0.0.1:6379> set key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT timestamp|KEEPTTL] [NX|XX] [GET]
127.0.0.1:6379> set str helloworld
OK
127.0.0.1:6379> get str
"helloworld"
127.0.0.1:6379> exists str # check existence
(integer) 1
127.0.0.1:6379> keys st* # pattern search (may block on many keys)
1) "str"
127.0.0.1:6379> scan 0 match st* count 1000
1) "0"
2) "str"
127.0.0.1:6379> strlen str
(integer) 10
127.0.0.1:6379> incr number
(integer) 1
127.0.0.1:6379> setex str4 10 ab
OK
127.0.0.1:6379> ttl str4
(integer) 5
127.0.0.1:6379> setnx lock uuid # set if not exist (used for distributed lock)
(integer) 12.2 List
Redis list behaves like a linked list (not an array). Push and pop are O(1); index lookup is O(n). Internally Redis uses a hybrid structure called quicklist : small lists are stored as a contiguous ziplist ; when the list grows, it switches to a linked list of ziplist nodes, combining fast insertion with low memory overhead.
127.0.0.1:6379> lpush books java
(integer) 1
127.0.0.1:6379> lrange books 0 -1
1) "java"
127.0.0.1:6379> rpush books golang
(integer) 2
127.0.0.1:6379> lpop books
"java"
127.0.0.1:6379> lrem books 1 java
(integer) 0
127.0.0.1:6379> llen books
(integer) 12.3 Hash
Hash is an unordered dictionary implemented like Java’s HashMap (array + linked list). It allows field‑level access, saving bandwidth compared with storing a whole object as a string, but consumes more memory per entry than a plain string.
127.0.0.1:6379> hset user:1 id 1
(integer) 1
127.0.0.1:6379> hset user:1 name xiaoming
(integer) 1
127.0.0.1:6379> hget user:1 name
"xiaoming"
127.0.0.1:6379> hgetall user:1
1) "id"
2) "1"
3) "name"
4) "xiaoming"
...2.4 Set
Set corresponds to a hash‑set: unordered, unique elements. Internally it is a special dictionary where each value is a placeholder (NULL). Sets are useful for deduplication, e.g., storing IDs of users who have won a prize.
127.0.0.1:6379> sadd set1 a b c d
(integer) 4
127.0.0.1:6379> smembers set1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> sdiff set1 set2
1) "a"
2) "d"
127.0.0.1:6379> sinter set1 set2
1) "b"
2) "c"
127.0.0.1:6379> sunion set1 set2
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"2.5 Sorted Set (zset)
Zset combines a set (unique values) with a score that determines ordering. Internally it uses a skip list. Typical uses include ranking fans by follow time or sorting students by exam score.
127.0.0.1:6379> zadd myzset 99 java
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1
1) "java"
127.0.0.1:6379> zrevrange myzset 0 -1
1) "java"
127.0.0.1:6379> zrank myzset java
(integer) 0
127.0.0.1:6379> zcard myzset
(integer) 13. Advanced Redis data structures
3.1 Geo (Geohash)
Since Redis 3.2, a GEO module stores location data in a sorted set, enabling nearby queries for use cases such as bike‑sharing or restaurant search. In a clustered environment a single GEO key larger than ~1 MiB can cause migration stalls; therefore GEO data is often deployed on a dedicated instance.
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 500 km withcoord withdist withhash
1) 1) "chongqi"
2) "341.9374"
3) (integer) 4026042091628984
4) 1) "106.49999767541885376"
2) "29.52999957900659211"3.2 Bitmap
Bitmap stores bits compactly, useful for boolean data such as daily sign‑in flags. One bitmap can represent 365 days with only 46 bytes, saving massive space when tracking billions of users.
127.0.0.1:6379> setbit s 1 1
(integer) 0
127.0.0.1:6379> bitcount s
(integer) 7
127.0.0.1:6379> get s
"he"
127.0.0.1:6379> bitpos s 1
(integer) 13.3 HyperLogLog
HyperLogLog provides approximate distinct‑counting with a standard error of 0.81 %. It is ideal for UV statistics where exact counts are unnecessary but memory efficiency is critical. Redis stores small cardinalities in a sparse representation and switches to a dense 12 KB structure only when the count grows large.
127.0.0.1:6379> pfadd hylog 1 2 3 4 5 6
(integer) 1
127.0.0.1:6379> pfcount hylog
(integer) 6
127.0.0.1:6379> pfmerge hylog2 hylog hylog1
OK
127.0.0.1:6379> pfcount hylog2
(integer) 10Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Shepherd Advanced Notes
Dedicated to sharing advanced Java technical insights, daily work snippets, and the power of persistent effort.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
