Databases 15 min read

Redis Essentials: Data Types, Persistence, Pub/Sub, Transactions, and Replication

This guide introduces Redis as a high‑performance key‑value store, covering its core data structures, advantages, persistence mechanisms, publish/subscribe model, transaction handling, and replication strategies with practical command examples.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Redis Essentials: Data Types, Persistence, Pub/Sub, Transactions, and Replication

Redis is a free, open‑source, BSD‑licensed, high‑performance key‑value database. It distinguishes itself from other caches by offering data persistence, a rich set of data structures (strings, hashes, lists, sets, sorted sets), and master‑slave replication.

Key Features

Persistence: data can be saved to disk and reloaded after a restart.

Rich data types: strings, hashes, lists, sets, sorted sets.

Master‑slave replication for data backup.

Additional capabilities such as publish/subscribe, key expiration, and atomic operations.

Data Types Overview

String : basic key‑value pair, binary‑safe, up to 512 MB. Comparable to a Java Map entry.

Hash : a collection of field‑value pairs under a single key, ideal for representing objects.

List : ordered collection of strings, supporting left/right push and pop operations.

Set : unordered collection of unique strings, O(1) add/remove/lookup.

Sorted Set (Zset) : unique strings associated with a double score, sorted by score; scores may duplicate.

Command Examples

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
# Hash example
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 example
127.0.0.1:6379> rpush mylist a b c
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "a"
2) "b"
3) "c"
# Set example
127.0.0.1:6379> sadd myset x y z
(integer) 3
127.0.0.1:6379> smembers myset
1) "x"
2) "y"
3) "z"
# Zset example
127.0.0.1:6379> zadd scores 100 user1 200 user2
(integer) 2
127.0.0.1:6379> zrange scores 0 -1 withscores
1) "user1"
2) "100"
3) "user2"
4) "200"

Publish/Subscribe

Redis Pub/Sub enables a publisher to send messages to a channel and multiple subscribers to receive them. Example:

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 output shows the message.

Transactions

Redis transactions group multiple commands between MULTI and EXEC. Commands are queued and executed atomically as a batch, but Redis does not roll back if a command fails; other commands still run.

redis> MULTI
OK
redis> SET key1 "value1"
QUEUED
redis> INCR counter
QUEUED
redis> EXEC
1) OK
2) (integer) 1

Important notes:

All commands are queued before EXEC.

If a command fails, the rest are still processed.

Redis guarantees atomicity for individual commands, not for the whole transaction.

Persistence

Two main mechanisms:

RDB : snapshots of the dataset at intervals; fast loading but may lose recent writes.

AOF : appends every write command to a log; configurable sync policies ( always, everysec, no) balancing durability and performance.

Replication

Using SLAVEOF host port, a slave copies data from a master. The replication process:

Master creates an RDB snapshot and streams it to the slave.

After the snapshot, the master streams queued write commands.

Slave discards old data, loads the snapshot, then applies incoming writes.

Subsequent writes are sent to the slave in real time.

Replication can be chained to form a hierarchy, improving scalability.

Sentinel and Sharding

Sentinel monitors Redis instances and automatically promotes a slave to master if the current master fails.

Sharding distributes data across multiple nodes. Approaches include range sharding, hash sharding (e.g., CRC32 modulo), and deployment models: client‑side sharding, proxy‑based sharding, or Redis Cluster (server‑side sharding).

Overall, Redis provides a versatile platform for caching, real‑time messaging, and durable data storage, with a rich command set and flexible deployment options.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

RedisPersistenceReplicationData StructuresTransactionspub/subkey-value store
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.