Master Redis: Core Data Types, Commands, and Advanced Features Explained
This article introduces Redis as an open‑source, in‑memory data‑structure store, explains its role as a NoSQL database, details its five fundamental data types, common commands, expiration handling, pipelines, and transactions, and shows how it fits into modern scaling architectures.
What Is Redis
Redis is an open‑source, in‑memory data‑structure storage system that can serve as a database, cache, and message broker. It supports strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes.
Redis also provides replication, Lua scripting, LRU eviction, transactions, and various persistence levels, and offers high availability through Sentinel and automatic sharding with Cluster.
What Is NoSQL
Redis is a non‑relational NoSQL database. NoSQL emerged to address the limitations of single‑machine databases as traffic grows.
The Era of Single‑Machine Databases
When a website has low traffic, a single database can handle all requests.
Cache + Sharding
As traffic increases, a single database becomes insufficient. Adding a cache layer and splitting the database into clusters, optimizing its structure, and separating reads from writes improves performance.
In this context, caching is one use case of NoSQL; Redis, for example, can also act as a simple message queue, session store, counter, leaderboard, or friend‑relationship manager.
Redis General Commands
Below are common Redis commands independent of data type.
Key and Value Operations
Redis stores data as key‑value pairs, each key being unique.
For demonstration, string‑related commands are used.
keys
pattern– returns all keys matching the pattern (O(n), usually replaced by
scanin production).
dbsize – returns the number of keys stored.
exists
key– checks if a key exists.
del
key– deletes the specified key.
type
key– returns the data type of the key.
rename
key newkey– renames a key.
Expiration
Because many Redis values are used as cache, they often need an expiration time.
expire
key seconds– sets a timeout on a key.
ttl
key– shows remaining time;
-1means persistent,
-2means expired or non‑existent.
Redis Five Basic Data Types
<code>Redis has five fundamental data structures: strings, hashes, lists, sets, and sorted sets.</code>String
set
key value– set a value.
get
key– get a value.
mset
key1 value1 key2 value2– atomic batch set.
mget
key1 key2– atomic batch get.
incr
key– increment.
decr
key– decrement.
incrby
key value– increment by a number.
decrby
key value– decrement by a number.
incrbyfloat
key floatvalue– increment by a floating‑point number.
setnx
key value– set only if key does not exist (used for distributed locks).
set
key value xx– set only if key exists (update).
Hash
A hash can be seen as a small Redis, similar to a Java
HashMap, implemented with an array of linked lists.
During rehashing, both old and new hash tables coexist until migration completes.
hset
key field value– set a field.
hsetnx
key field value– set if field does not exist.
hmset
key field1 value1 …– batch set.
hget
key field– get a field.
hmget
key field1 field2 …– batch get.
hgetall
key– get all fields.
hdel
key field– delete a field.
hexists
key field– check existence.
hlen
key– number of fields.
hvals
key– all values.
hkeys
key– all fields.
hincrby
key field incr– increment a numeric field.
hincrbyfloat
key field incr– increment a floating‑point field.
List
Redis lists are implemented as doubly linked lists; insertion and deletion are fast, while index lookup is slower.
lpush
key item1 item2 …– push to the left (stack).
rpush
key item1 item2 …– push to the right.
lpop
key– pop from the left.
rpop
key– pop from the right.
lindex
key index– get element by index (O(n)).
lrange
key start end– get a range of elements (O(n)).
linsert
key before|after pivot value– insert relative to an element.
lrem
key count value– remove elements matching value.
ltrim
key start end– keep only a range.
lset
key index value– set element at index.
blpop
key timeout– block until element available (0 = forever).
brpop
key timeout– same for right side, useful for producer‑consumer.
Using left‑push/right‑pop implements a queue; left‑push/left‑pop or right‑push/right‑pop implements a stack.
Set
Redis sets behave like Java
HashSet: unordered, no duplicate elements, useful for deduplication and set operations.
sadd
key value– add element.
srem
key value– remove element.
sismember
key value– check membership.
srandmember
key count– get random elements without removal.
spop
key count– pop random elements (removes them).
smembers
key– all members (O(n)).
scard
key– cardinality.
sinter
set1 set2 …– intersection.
sdiff
set1 set2 …– difference.
sunion
set1 set2 …– union.
Sorted Set (zset)
Sorted sets store members with a score, enabling ordered collections such as leaderboards.
They are implemented with skip lists, which provide fast range queries while maintaining order.
Redis Transactions and Pipelines
Pipeline
Pipelines batch multiple commands into a single network round‑trip, reducing latency. Commands in a pipeline are not atomic; they may be interleaved with other clients' commands.
Transactions
Redis transactions guarantee atomicity and isolation (A and I of ACID). Persistence depends on RDB/AOF configuration, but Redis does not provide consistency guarantees or rollback.
multi – start transaction.
exec – execute transaction.
discard – cancel queued commands.
watch
key– monitor a key for changes before committing.
unwatch – stop monitoring.
Common Redis Commands Summary
For a concise reference, see the accompanying command summary image.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.