Master Redis: Core Concepts, Commands, and Data Types Explained
This article introduces Redis as an open‑source in‑memory data‑structure store, explains its role as a NoSQL database, covers fundamental data types, common commands, expiration handling, and advanced features like pipelines and transactions, providing a comprehensive guide for developers.
What is Redis
Redis is an open‑source, in‑memory data‑structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes.
Redis also provides built‑in replication, Lua scripting, LRU eviction, transactions, and different levels of persistence, and offers high availability through Sentinel and automatic sharding with Cluster.
What is NoSQL
Redis is a NoSQL (non‑relational) database. The article explains why NoSQL emerged and its role.
The era of single‑node databases
When a website has low traffic, a single database can handle the load.
Cache + Sharding
As traffic grows, a single database is insufficient. Adding a cache layer and splitting the database into clusters, optimizing schema, and using read/write separation improves performance.
Here the cache is a NoSQL solution; caching is only one function of NoSQL. Redis, for example, can also implement simple message queues, session sharing, counters, leaderboards, friend relationships, etc.
Redis generic commands
First, ignoring data types, some generic Redis commands are introduced.
Operating keys and values
Redis is a key‑value store; every piece of data has a unique key.
For demonstration, string‑related commands are used.
keys [pattern] – returns all keys matching the pattern (O(n)). Not recommended in production; use scan instead.
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 need expiration. Redis provides powerful expiration commands.
expire key seconds – set expiration time.
ttl key – view remaining time; returns a positive number, -1 for permanent, -2 for expired or non‑existent.
Redis’s five basic data types
Redis provides five basic data structures: strings, hashes, lists, sets, and sorted sets.String
String is the most common type.
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 if not exists (used for distributed locks).
set key value xx – set only if key exists (update).
Hash
A hash can be seen as a small Redis; internally similar to Java’s HashMap, using an array of linked lists.
In Redis, dictionary values are strings and rehashing is progressive.
During rehash, both old and new hash tables coexist; queries check both until migration finishes.
Basic hash operations:
hset key field value – set field.
hsetnx key field value – set if not exists.
hmset key field1 value1 … – batch set.
hget key field – get field.
hmget key field1 field2 – batch get.
hgetall key – get all fields.
hdel key field – delete field.
hexists key field – check existence.
hlen key – number of fields.
hvals key – all values.
hkeys key – all fields.
hincrby key field incrValue – increment field.
hincrbyfloat key field floatValue – increment field by float.
List
Redis list is similar to Java’s LinkedList (doubly linked list). Insert/delete are fast, index lookup is slow.
Lists can implement stacks or queues.
Basic list operations:
lpush key item1 item2 … – push to the left (stack).
rpush key item1 item2 … – push to the right.
lpop key – pop from left.
rpop key – pop from right.
lindex key index – get element by index (O(n)).
lrange key start end – get range of elements (O(n)).
linsert key before|after pivot element – insert before/after pivot.
lrem key count value – remove count occurrences of value (count=0 removes all; >0 left‑to‑right; <0 right‑to‑left).
ltrim key start end – keep only specified range.
lset key index newValue – set element at index.
blpop key timeout – block until element available (0 = forever).
brpop key timeout – same for right side; can implement producer‑consumer.
In summary, left‑push/right‑pop implements a queue; left‑push/left‑pop or right‑push/right‑pop implements a stack.
Set
Redis set is like Java’s HashSet (unordered, no duplicates). Useful for deduplication and set operations such as intersection, union, difference.
Basic set operations:
sadd key value – add element.
sdel key value – remove element.
sismember key value – test membership.
srandmember key count – get random elements without removal.
spop key count – pop random elements (removes them).
smembers key – get all elements (O(n)).
scard key – get cardinality.
sinter set1 set2 … – intersection.
sdiff set1 set2 … – difference.
sunion set1 set2 … – union.
Sorted Set (zset)
Sorted set maintains elements ordered by a score, useful for leaderboards, ranking, etc.
Sorted sets are implemented with skip lists, which allow fast ordered insertion and range queries.
Redis transactions and pipelines
Pipeline
Pipeline batches multiple commands in a single network round‑trip, reducing overhead. Commands in a pipeline are not executed atomically; they may be interleaved with other clients.
Transaction
Redis guarantees atomicity and isolation for transactions, but not full ACID consistency; persistence depends on RDB/AOF configuration, and rollback is not supported.
A transaction is essentially a pipeline with atomic execution.
multi – start transaction.
exec – execute.
discard – cancel queued commands.
watch key – monitor key for changes before exec.
unwatch key – stop monitoring.
Summary of common Redis commands
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.
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.
