Appearance
Redis High Availability
Redis Sentinel (Automatic Failover)
Architecture:
Sentinel 1 ──┐
Sentinel 2 ──┼──> Monitor ──> Primary Redis ──> Replica Redis 1
Sentinel 3 ──┘ ┗> Replica Redis 2
(Quorum = 2)Setup:
bash
# Chạy 3 Redis (1 primary + 2 replica)
redis-server --port 6379
redis-server --port 6380 --replicaof localhost 6379
redis-server --port 6381 --replicaof localhost 6379
# Chạy 3 Sentinel, mỗi file một port khác (26379, 26380, 26381)
redis-sentinel sentinel1.conf
redis-sentinel sentinel2.conf
redis-sentinel sentinel3.confNội dung sentinel1.conf (file 2 và 3 chỉ đổi port); chú thích chỉ trên dòng riêng vì Sentinel báo lỗi khi gặp chú thích cùng dòng:
conf
port 26379
# Sentinel mặc định không phân giải tên host: dùng hostname (như localhost) cần dòng này, hoặc ghi IP 127.0.0.1
sentinel resolve-hostnames yes
# quorum = 2: số Sentinel phải đồng ý để đánh dấu primary lỗi
sentinel monitor mymaster localhost 6379 2
# mặc định down-after-milliseconds là 30000 và failover-timeout là 180000; ở đây hạ xuống để thử
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000Failover process:
1. Primary không phản hồi
2. Sentinel đánh dấu lỗi sau down-after-milliseconds (ví dụ này 5 s)
3. Đủ quorum (2/3 Sentinel đồng ý primary lỗi)
4. Bầu Sentinel leader (cần đa số Sentinel: quorum chỉ dùng để phát hiện lỗi)
5. Promote một replica thành primary
6. Cấu hình lại replica còn lại
7. Thông báo client (ứng dụng kết nối lại)
Đo: pause container primary tới khi Sentinel báo primary mới mất 6,7 s (3 Sentinel, down-after 5000, Docker; đổi theo máy).Client connection:
javascript
// Node.js (ioredis)
const Redis = require('ioredis')
const redis = new Redis({
sentinels: [
{ host: 'localhost', port: 26379 },
{ host: 'localhost', port: 26380 },
{ host: 'localhost', port: 26381 }
],
name: 'mymaster',
password: 'password'
})
// Client auto-discovers primary from sentinelsRedis Cluster (Horizontal Scaling)
Architecture:
Redis Cluster (16,384 slots)
├── Node 1 (slots 0-5460) + Replica 1a
├── Node 2 (slots 5461-10922) + Replica 2a
└── Node 3 (slots 10923-16383) + Replica 3aKey distribution:
bash
# Hash slot = CRC16(key) % 16384 (đo trên Redis 8.10.3, cụm 3 primary + 3 replica)
redis-cli -p 7000 CLUSTER KEYSLOT user:123 # 12893 (nút giữ slot 10923-16383)
redis-cli -p 7000 CLUSTER KEYSLOT user:456 # 4255 (nút giữ slot 0-5460)
# Hash tags: chỉ phần trong {} được băm, nên các key chung slot
redis-cli -c -p 7000 CLUSTER KEYSLOT '{user:123}:name' # 12893
redis-cli -c -p 7000 MSET '{user:123}:name' John '{user:123}:age' 30
redis-cli -c -p 7000 MGET '{user:123}:name' '{user:123}:age'
# MGET user:123 user:456 trả lỗi CROSSSLOT (khác slot)Setup cluster:
bash
# Chạy 6 Redis (3 primary + 3 replica), nền
for port in 7000 7001 7002 7003 7004 7005; do
redis-server --port $port --cluster-enabled yes --cluster-config-file nodes-$port.conf --daemonize yes
done
# Tạo cluster
redis-cli --cluster create \
127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 \
127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
--cluster-replicas 1 # 1 replica mỗi primaryClient connection:
javascript
// Node.js (ioredis)
const Redis = require('ioredis')
const cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: 7000 },
{ host: '127.0.0.1', port: 7001 },
{ host: '127.0.0.1', port: 7002 }
])
// Client auto-discovers all nodesResharding (add/remove nodes):
bash
# Add node
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000
# Chuyển slot sang nút mới (không tương tác: 1000 slot từ mọi primary, thay <node-id> bằng ID nút mới từ CLUSTER MYID)
redis-cli --cluster reshard 127.0.0.1:7000 --cluster-from all --cluster-to <node-id> --cluster-slots 1000 --cluster-yesPersistence Trade-offs
RDB (Snapshot):
conf
# Lưu mỗi 60 s nếu có từ 1000 key đổi trở lên
save 60 1000
# Pro: Fast restart, compact file
# Con: Data loss (last snapshot to crash)
# Best for: Caching (OK to lose recent data)AOF (Append-Only File):
conf
appendonly yes
# everysec | always | no
appendfsync everysec
# Pro: Durable (mất tối đa khoảng 1 giây ghi với everysec)
# Con: Larger files, slower restart
# Best for: Session store (can't lose data)Both (RDB + AOF):
text
Dùng RDB để restart nhanh + AOF để bền.
Best for: Production (cân bằng tốc độ và độ bền).Phải nhớ:
- Sentinel failover cần đa số Sentinel, quorum chỉ dùng để phát hiện lỗi; replication bất đồng bộ nên có thể mất ghi, giảm rủi ro bằng
min-replicas-to-write. - Redis Cluster có 16384 slot; MGET/transaction nhiều key cần cùng slot (hash tag
{...}), nếu không gặp CROSSSLOT. - Cluster dùng
cluster-node-timeout(mặc định 15000 ms) để phát hiện nút lỗi.