Redis Overview: Features, Data Types, Commands, Persistence, Replication, Pub/Sub and Transactions
This article provides a comprehensive introduction to Redis, covering its high‑performance key‑value architecture, supported data structures (string, hash, list, set, sorted set), core commands, persistence mechanisms, replication, sentinel monitoring, sharding, pub/sub messaging and transaction handling, with practical command‑line examples.
Redis Overview
Redis is a free, open‑source, BSD‑licensed, high‑performance key‑value database.
Key Advantages
Extremely fast: read speed up to 110,000 ops/s, write speed up to 81,000 ops/s.
Rich data structures: strings, lists, hashes, sets, and sorted sets.
Atomic operations: each command is atomic; multi‑command transactions are supported via MULTI and EXEC.
Additional features: publish/subscribe, key expiration, etc.
Data Types
String : binary‑safe, can store any data (e.g., images, serialized objects) up to 512 MB per key.
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"Hash : a collection of field‑value pairs, similar to a map; ideal for representing objects.
# hash‑key example
127.0.0.1:6379> hset hash-key sub-key1 value1
(integer) 1
127.0.0.1:6379> hset hash-key sub-key2 value2
(integer) 1
127.0.0.1:6379> hgetall hash-key
1) "sub-key1"
2) "value1"
3) "sub-key2"
4) "value2"List : ordered collection of strings, implemented as a doubly‑linked list; supports left/right push and range queries.
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> lrange list-key 0 -1
1) "v1"
2) "v2"Set : unordered collection of unique strings, backed by a hash table; O(1) add, remove, and membership checks.
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> smembers k1
1) "v2"
2) "v1"Sorted Set (Zset) : like a set but each member has 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> zrange zset-key 0 -1 withscores
1) "member1"
2) "728"Basic Commands
Refer to the official Redis command reference for a full list.
Publish/Subscribe
Redis Pub/Sub enables message broadcasting: publishers send messages to a channel, and all subscribed clients receive them.
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 a failure of one command does not roll back earlier ones.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED
127.0.0.1:6379> GET book-name
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"Persistence
Redis offers two persistence strategies:
RDB : snapshots of the dataset at intervals; fast recovery but may lose recent writes.
AOF : appends every write command to a log; configurable sync policies (always, everysec, no) balance durability and performance.
Replication
Using SLAVEOF host port, a server can become a replica of another. Replication follows a three‑step process: master creates an RDB snapshot and streams it, replica loads the snapshot, then both exchange subsequent write commands.
Sentinel
Sentinel monitors a Redis cluster and automatically promotes a replica to master if the current master fails.
Sharding
Sharding distributes data across multiple nodes. Approaches include range‑based sharding, hash‑based sharding (e.g., CRC32 modulo node count), and deployment models such as client‑side sharding, proxy sharding, or server‑side sharding via Redis Cluster.
(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.
