Redis Data Structures and Common Commands Overview
This article introduces Redis's fundamental and advanced data structures, explains how each is represented internally, and provides detailed examples of essential commands such as KEYS, EXISTS, DEL, EXPIRE, TTL, and TYPE, highlighting their usage, complexity, and best‑practice considerations.
Redis Data Structures
Redis supports multiple data structures, including five basic types and several advanced types, each suited for different application scenarios.
Basic Data Structures
String: the basic string type, foundation for other structures.
Hash: a hash table of field‑value pairs.
List: a linked list of strings.
Set: an unordered collection of unique strings.
Sorted Set (ZSet): a set ordered by a floating‑point score.
Advanced Data Structures
Bitmaps: bit‑level operations on strings for space‑efficient storage.
HyperLogLog: probabilistic structure for estimating cardinality of a set.
Geo: geospatial index based on sorted sets for radius queries.
Bloom Filter: probabilistic membership test.
Commonality of All Structures
Every Redis data structure is stored as a key and a value. The key is any valid string; the value differs according to the structure type. For example, a String stores a plain string value, while a Hash stores a hash table.
Illustrations of internal representations (original images omitted for brevity).
Redis Common Commands
Redis groups its commands into 15 functional categories. The KEYS group applies to all data structures and is essential to learn before using type‑specific commands.
KEYS
The keys command lists all keys matching a pattern. Its time complexity is O(N), so it should never be used on production servers with many keys.
# key command, time complexity O(N)
keys pattern # pattern may include *, ?, [a-z] etc.Example:
> mset hello_test1 one hello_test2 two helloa a hellob b
> keys hello*
1) "hello_test1"
2) "hello_test2"
3) "helloa"
4) "hellob"
> keys heelo?
1) "helloa"
2) "hellob"
> keys hello[a-z]
1) "helloa"
2) "hellob"EXISTS
Checks whether one or more keys exist; returns the count of existing keys.
# exists command, time complexity O(1)
exists key [key ...]Example:
> set test1 t1
> exists test1 test2
(integer) 1 # only one key exists
> exists test3 test4
(integer) 0 # no keys existDEL
Deletes one or more keys; returns the number of keys actually removed.
# del command, time complexity O(N)
del key [key ...]Example:
> set test t
> del test
(integer) 1
> mset test1 2 test2 1
> del test1 test2 test3
(integer) 2 # two keys deleted
> del test1 test2 test3
(integer) 0 # nothing left to deleteEXPIRE / PEXPIRE
Set a key to expire after a given number of seconds ( expire) or milliseconds ( pexpire). Returns 1 on success, 0 on failure.
# expire command, time complexity O(1)
expire key seconds
# pexpire command, time complexity O(1)
pexpire key millisecondsExample:
> mset test test_value test1 test1_value
> expire test 10 # expires in 10 seconds
(integer) 1
> pexpire test1_value 10000 # expires in 10 000 ms
(integer) 1
> expire ttt 100 # key does not exist
(integer) 0TTL / PTTL
Return the remaining time to live of a key in seconds ( ttl) or milliseconds ( pttl). Returns -2 if the key does not exist, -1 if the key exists but has no expiration.
# ttl command, time complexity O(1)
ttl key
# pttl command, time complexity O(1)
pttl keyExample (ttl shown; pttl works similarly):
> set test test
> expire test 100
> ttl test
(integer) 98 # 98 seconds left
> set test1 test1 # permanent
> ttl test1
(integer) -1 # permanent
> ttl test2
(integer) -2 # does not existEXPIREAT / PEXPIREAT
Set a key to expire at an absolute Unix timestamp (seconds for expireat, milliseconds for pexpireat).
# expireat command, time complexity O(1)
expireat key timestamp
# pexpireat command, time complexity O(1)
pexpireat key milliseconds-timestampExample:
> set test test
> expireat test 1560873600 # 2019‑06‑19 00:00:00
(integer) 1
> set test1 test1
> pexpireat test1 156087360000 # same moment in ms
(integer) 1PERSIST
Remove the expiration from a key, making it permanent. Returns 1 if the timeout was removed, 0 otherwise.
# persist command, time complexity O(1)
persist keyExample:
> set test test
> ttl test
(integer) -1 # permanent
> persist test
(integer) 0 # already permanent
> expire test 10
(integer) 1
> persist test
(integer) 1 # expiration removedTYPE
Return the data type of the value stored at a key (string, list, set, hash, zset). Complex structures such as Geo, HyperLogLog, and Bitmaps are implemented on top of these basic types.
# type command, time complexity O(1)
type keyExample:
> set test test
> type test
string
> hset htest field value
> type htest
hashConclusion
The commands covered above are the most frequently used generic Redis commands. Although they are simple, mastering their syntax, time complexity, and proper usage is essential for any developer working with Redis.
(End)
Signed-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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
