Master Redis: Core Concepts, Data Types, and Advanced Features Explained
This article provides a comprehensive overview of Redis, covering its high‑performance key‑value architecture, persistence options, replication, sharding, data types such as strings, hashes, lists, sets and sorted sets, as well as advanced features like transactions, publish/subscribe, and Sentinel monitoring, all illustrated with practical command examples.
Redis Overview
Redis is a fully open‑source, BSD‑licensed high‑performance key‑value database.
Its three distinguishing features are:
Supports data persistence, allowing in‑memory data to be saved to disk and reloaded after a restart.
Provides richer data structures beyond simple strings, such as lists, sets, sorted sets, and hashes.
Offers master‑slave replication for data backup.
Advantages
Extremely fast – read throughput up to 110 000 ops/s and write up to 81 000 ops/s.
Rich data types: strings, lists, hashes, sets, sorted sets.
All operations are atomic; multi‑command transactions are supported via MULTI/EXEC.
Additional features like publish/subscribe, key expiration, etc.
Data Types
Redis supports five primary data types: string, hash, list, set, and sorted set (zset).
String
A string is the simplest type, a binary‑safe value up to 512 MB. It can store any data, e.g., images or serialized objects.
Understanding: a string is analogous to a Java Map entry – one key maps to one value.
Hash
A hash is a collection of key‑value pairs stored under a single string key, ideal for representing objects.
Difference from string: a string holds a single value, while a hash holds multiple field‑value pairs.
List
Lists are ordered collections of strings, supporting insertion at the head or tail.
Elements may repeat, similar to Java’s List.
127.0.0.1:6379> rpush list-key v1
(integer) 1
127.0.0.1:6379> rpush list-key v2
(integer) 2
127.0.0.1:6379> rpush list-key v1
(integer) 3
127.0.0.1:6379> lrange list-key 0 -1
1) "v1"
2) "v2"
3) "v1"Set
Sets are unordered collections of unique strings, implemented with hash tables (O(1) add, remove, lookup).
127.0.0.1:6379> sadd k1 v1
(integer) 1
127.0.0.1:6379> sadd k1 v2
(integer) 1
127.0.0.1:6379> sadd k1 v1
(integer) 0
127.0.0.1:6379> smembers k1
1) "v3"
2) "v2"
3) "v1"Sorted Set (Zset)
Zsets store unique strings each associated with a double‑precision score; members are ordered by score.
127.0.0.1:6379> zadd zset-key 728 member1
(integer) 1
127.0.0.1:6379> zadd zset-key 982 member0
(integer) 1
127.0.0.1:6379> zrange zset-key 0 -1 withscores
1) "member1"
2) "728"
3) "member0"
4) "982"Publish/Subscribe
Redis Pub/Sub allows a publisher to send messages to a channel that any number of subscribers can receive.
127.0.0.1:6379> SUBSCRIBE redisChat
Reading messages... (press Ctrl‑C to quit)
1) "subscribe"
2) "redisChat"
127.0.0.1:6379> PUBLISH redisChat "hello world"
(integer) 1Transactions
Redis transactions batch multiple commands between MULTI and EXEC. Commands are queued and executed atomically, but failures do not roll back earlier commands.
redis> MULTI
OK
redis> SET book-name "Mastering C++ in 21 days"
QUEUED
redis> GET book-name
QUEUED
redis> SADD tag "C++" "Programming" "Mastering"
QUEUED
redis> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering"
2) "C++"
3) "Programming"Persistence
Redis can persist data to disk using RDB snapshots or the Append‑Only File (AOF) log.
RDB creates point‑in‑time snapshots; fast but may lose recent writes.
AOF records every write command; sync policies: always (slow), everysec (balanced), no (fast but riskier).
Replication
Master‑slave replication copies data from a master to one or more slaves using RDB snapshots followed by streaming of write commands.
Sentinel
Sentinel monitors a Redis cluster and automatically promotes a slave to master if the current master fails.
Sharding
Sharding distributes keys across multiple Redis instances. Methods include range sharding, hash sharding (e.g., CRC32 modulo), and deployment models such as client‑side sharding, proxy sharding, and Redis Cluster.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
