Redis là kho dữ liệu trong RAM (in-memory) nên cực nhanh, lại có nhiều cấu trúc dữ liệu sẵn — không chỉ làm cache. Mỗi cấu trúc hợp một bài toán:
- Sorted Set (
ZADD,ZRANGE): bảng xếp hạng — tự sắp theo điểm, lấy top-N rất nhanh. - List (
LPUSH/RPOP): hàng đợi việc hoặc feed hoạt động. - Pub/Sub: thông báo/chat thời gian thực.
- Streams (
XADD,XREAD): event sourcing, message queue có consumer group. - HyperLogLog: đếm xấp xỉ số lượng duy nhất với bộ nhớ cố định.
Lưu bền (persistence): RDB chụp snapshot định kỳ (khởi động lại nhanh, có thể mất phần dữ liệu giữa hai snapshot); AOF ghi nhật ký mọi lệnh write (phục hồi tốt hơn, file to hơn). Kết hợp RDB+AOF cho cân bằng tốt nhất.
Độ sẵn sàng cao: Sentinel tự failover cho setup một master; Cluster sharding ngang tự động (16384 hash slot, tối thiểu 3 master).
Redis is an in-memory data store, so it's extremely fast, and it ships with many data structures — not just a cache. Each structure fits a problem:
- Sorted Set (
ZADD,ZRANGE): leaderboards — auto-sorted by score, top-N is very fast. - List (
LPUSH/RPOP): job queues or activity feeds. - Pub/Sub: real-time notifications/chat.
- Streams (
XADD,XREAD): event sourcing, message queues with consumer groups. - HyperLogLog: approximate unique counts with fixed memory.
Persistence: RDB takes periodic snapshots (fast restart, may lose data between snapshots); AOF logs every write (better recovery, larger file). Combining RDB+AOF gives the best balance.
High availability: Sentinel does automatic failover for a single-master setup; Cluster does automatic horizontal sharding (16,384 hash slots, minimum 3 masters).