Master Redis: Core Data Types, Commands, and Advanced Features Explained
Redis is a high‑performance, open‑source key‑value store offering persistence, rich data structures, replication, pub/sub, transactions, and clustering; this guide explains its fundamentals, data types (string, hash, list, set, sorted set), essential commands, and advanced features such as persistence options, replication, Sentinel, and sharding.
Introduction
Redis is a completely open‑source, BSD‑licensed, high‑performance key‑value database. It distinguishes itself from other caches by supporting data persistence, a rich set of data structures, and master‑slave replication.
Advantages
Very high performance – read speed up to 110,000 ops/s and write speed up to 81,000 ops/s.
Rich data types – strings, lists, hashes, sets, and sorted sets.
Atomic operations – every command is atomic; multi‑command transactions are supported via MULTI and EXEC.
Additional features – publish/subscribe, key expiration, etc.
Core Data Types
String
A binary‑safe key‑value pair where a single key maps to a single value. Strings can store any binary data (e.g., images, serialized objects) up to 512 MB.
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"Hash
A collection of string fields and values, similar to a map or object. Ideal for storing objects where individual fields can be updated without rewriting the whole value.
# Create a hash with three fields
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> hset hash-key sub-key3 value3
(integer) 1
# Retrieve all fields
127.0.0.1:6379> hgetall hash-key
1) "sub-key1"
2) "value1"
3) "sub-key2"
4) "value2"
5) "sub-key3"
6) "value3"List
An ordered collection of strings implemented as a doubly linked list. Elements can be added to the head or tail, and the list supports 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
An unordered collection of unique strings backed by a hash table, offering 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 element has an associated double‑precision score. Elements are ordered by score, enabling range queries based on rank or 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"Publish/Subscribe
Redis provides a lightweight messaging system where 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) 1
# Subscriber receives:
1) "message"
2) "redisChat"
3) "hello world"Transactions
Redis transactions batch multiple commands between MULTI and EXEC. Commands are queued and executed atomically, but Redis does not roll back if a command fails.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> SET book-name "Mastering C++"
QUEUED
127.0.0.1:6379> SADD tag "C++" "Programming"
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) (integer) 2Persistence
RDB
Periodically snapshots the entire dataset to disk. Fast to load but can lose data since the last snapshot.
AOF
Appends every write command to an append‑only file. Sync options: always – every command is flushed to disk (high latency). everysec – sync every second (good trade‑off). no – let the OS decide (fastest, riskier).
Replication
Using SLAVEOF host port, a slave replicates data from a master. Replication is asynchronous; a slave can only have one master and master‑master replication is not supported.
Sentinel
Sentinel monitors master instances and automatically promotes a slave to master if the original master fails, providing high availability.
Sharding
Data can be partitioned across multiple Redis nodes to achieve linear scalability. Common strategies:
Client‑side sharding – the client decides the node using consistent hashing.
Proxy/agent sharding – a proxy routes requests to the correct node.
Server‑side sharding – Redis Cluster handles slot allocation and routing.
Conclusion
Redis combines in‑memory speed with durability options, a versatile set of data structures, and features such as pub/sub, transactions, replication, Sentinel, and sharding, making it suitable for caching, real‑time analytics, message queues, and many other use cases.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
