Redis Overview: Introduction, Data Types, Commands, Persistence, Replication, and Transactions
This article provides a comprehensive introduction to Redis, covering its key features, supported data structures, core commands, persistence mechanisms, replication, sentinel monitoring, sharding strategies, and transaction handling with practical code examples.
Redis Overview
Redis is a completely free, open‑source, high‑performance key‑value database that follows the BSD license.
Key Features
Supports data persistence (RDB snapshots and AOF) so data survives restarts.
Provides multiple native data structures: strings, hashes, lists, sets, and sorted sets.
Offers master‑slave replication, Sentinel for automatic failover, and optional sharding via Redis Cluster.
Includes Pub/Sub messaging, Lua scripting, and transaction support (MULTI/EXEC).
Data Types
String – binary‑safe value up to 512 MB; analogous to a map entry where a key maps to a single value.
Hash – a map of field‑value pairs stored under one key, ideal for representing objects.
List – an ordered collection of strings that can be pushed or popped from both ends.
Set – an unordered collection of unique strings; operations are O(1) thanks to an underlying hash table.
Sorted Set (Zset) – similar to a set but each member carries a double‑precision score used for automatic sorting; scores may repeat while members are unique.
Basic Commands
127.0.0.1:6379> SET hello world
OK
127.0.0.1:6379> GET hello
"world"Hash Commands
127.0.0.1:6379> HSET user:1 name "Alice"
(integer) 1
127.0.0.1:6379> HGETALL user:1
1) "name"
2) "Alice"List Commands
127.0.0.1:6379> RPUSH mylist a b a
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "a"
2) "b"
3) "a"
127.0.0.1:6379> LPOP mylist
"a"Set Commands
127.0.0.1:6379> SADD colors red green blue
(integer) 3
127.0.0.1:6379> SMEMBERS colors
1) "red"
2) "green"
3) "blue"Sorted Set Commands
127.0.0.1:6379> ZADD scores 100 alice 200 bob
(integer) 2
127.0.0.1:6379> ZRANGE scores 0 -1 WITHSCORES
1) "alice"
2) "100"
3) "bob"
4) "200"Pub/Sub
Publish/Subscribe enables message broadcasting: a publisher sends a message to a channel, and all subscribed clients receive it.
127.0.0.1:6379> SUBSCRIBE chat
Reading messages... (press Ctrl‑C to quit)
1) "subscribe"
2) "chat"
127.0.0.1:6379> PUBLISH chat "hello world"
(integer) 1
-- subscriber output --
1) "message"
2) "chat"
3) "hello world"Transactions
Redis transactions group multiple commands between MULTI and EXEC. Commands are queued and executed atomically as a batch, but individual command failures do not roll back earlier commands.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> SET book "Redis Guide"
QUEUED
127.0.0.1:6379> SADD tags "database" "cache"
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) (integer) 2Persistence
Redis offers two persistence strategies:
RDB – periodic snapshots of the dataset saved to disk.
AOF – appends every write command to a log file; configurable sync policies (always, everysec, no).
Replication
Using SLAVEOF host port, a server becomes a replica of a master. The master first creates an RDB snapshot, sends it to the replica, then streams subsequent write commands.
Sentinel
Sentinel monitors a set of Redis instances, detects master failures, and automatically promotes a replica to master, ensuring high availability.
Sharding (Partitioning)
Data can be partitioned across multiple nodes to achieve linear scalability. Common approaches include:
Range‑based sharding (e.g., user IDs 0‑1000 on node R0).
Hash‑based sharding using CRC32 modulo the number of nodes.
Client‑side sharding, proxy sharding, or server‑side sharding via Redis Cluster.
These mechanisms allow Redis to handle large datasets and high request volumes efficiently.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
