跳到主要内容

15 篇博文 含有标签「redis」

查看所有标签

Deep Dive into Redis VI: Redis vs ZooKeeper vs etcd

· 阅读需 4 分钟

A Deep, Conceptual Comparison for Distributed Systems​


1. High-Level Positioning​

SystemCore IdentityPrimary Purpose
RedisIn-memory data platformFast data access, caching, counters, queues
ZooKeeperDistributed coordination serviceStrong consistency, coordination, metadata
etcdDistributed key-value storeCluster configuration & service discovery

One Sentence Summary​

  • Redis: “Give me data now.”
  • ZooKeeper: “Coordinate distributed systems safely.”
  • etcd: “Store and watch cluster state consistently.”

2. Fundamental Design Philosophy​

Redis Philosophy​

Speed > Everything
Memory-first
Eventual consistency (cluster)
Application-driven logic

Redis optimizes for latency and throughput.


ZooKeeper Philosophy​

Correctness > Performance
Strong consistency
Coordination first
Centralized control plane

ZooKeeper optimizes for correct distributed behavior.


etcd Philosophy​

Simplicity + Consistency
Raft-based consensus
Cloud-native control plane
Observability-friendly

etcd optimizes for operational clarity and reliability.


3. Core Architecture Comparison​

Redis Architecture (Cluster Mode)​

Client
|
┌───────────┴────────┐
│ Redis Cluster │
└───────────┬────────┘
┌───────────┴───────────┐
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ Master1 │ │ Master2 │ │ Master3 │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
Replicas Replicas Replicas

Key points:

  • Hash slots (16,384)
  • Sharding for scale
  • Replication for availability
  • Not consensus-based

ZooKeeper Architecture​

Client
|
┌────────┴──────────┐
│ ZooKeeper Quorum │
└────────┬──────────┘
┌──────────┼──────────┐
┌────┴────┐┌────┴────┐┌────┴────┐
│ Leader ││Follower ││Follower │
└─────────┘└─────────┘└─────────┘

Key points:

  • Single leader
  • Zab protocol
  • Linearizable writes
  • Hierarchical namespace

etcd Architecture​

Client
|
┌────────┴────────┐
│ etcd Cluster │
└────────┬────────┘
┌──────────┼──────────┐
┌────┴────┐┌────┴────┐┌────┴────┐
│ Leader ││ Follower││ Follower│
└─────────┘└─────────┘└─────────┘

Key points:

  • Raft consensus
  • Strong consistency
  • Flat key-value space
  • Watch-based design

4. Consistency Model (Critical Difference)​

FeatureRedisZooKeeperetcd
ConsistencyEventualStrongStrong
Consensus❌ZabRaft
Split-brainPossiblePreventedPrevented
Linearizable writes❌✅✅

Visual Consistency Comparison​

Redis:
Write → Master → Async replicas
(possible stale reads)

ZooKeeper / etcd:
Write → Leader → Majority ACK → Commit
(always consistent)

5. Data Model Comparison​

Redis Data Model​

Key → Value
├── String
├── Hash
├── List
├── Set
├── ZSet
└── Stream

Optimized for application data structures.


ZooKeeper Data Model​

/
├── app
│ ├── leader
│ ├── config
│ └── locks
  • Tree-like (filesystem style)
  • Znodes (small metadata)
  • Ephemeral nodes

etcd Data Model​

/services/app1/instances/1
/services/app1/instances/2
/config/db/timeout
  • Flat KV
  • Prefix-based grouping
  • First-class WATCH support

6. Failure Handling​

Redis Failure Model​

Master crashes
↓
Replica promoted
↓
Possible data loss window
  • Fast
  • Not transactional
  • Best-effort durability

ZooKeeper Failure Model​

Leader crashes
↓
Election
↓
New leader with consistent state
  • No data loss (committed)
  • Slower failover
  • Strong correctness

etcd Failure Model​

Leader crashes
↓
Raft election
↓
New leader
  • Guaranteed state safety
  • Designed for automation

7. Persistence & Storage​

AspectRedisZooKeeperetcd
Primary storageRAMDiskDisk
PersistenceRDB / AOFWAL + snapshotWAL + snapshot
Data volumeLargeSmallMedium
Restart recoveryFastModerateModerate

Redis Persistence​

RAM
│
├─ RDB (snapshot)
└─ AOF (append)

ZooKeeper / etcd Persistence​

Client writes
↓
WAL (log)
↓
Snapshot

8. Typical Use Cases (MOST IMPORTANT)​


Redis – When You SHOULD Use It​

✔ Caching
✔ Rate limiting
✔ Distributed counters
✔ Session storage
✔ Queues / Streams
✔ Real-time analytics
✔ Leaderboards

Diagram:

User → App → Redis → Response (sub-ms)

ZooKeeper – When You SHOULD Use It​

✔ Leader election
✔ Distributed locks
✔ Metadata management
✔ Kafka / HBase coordination
✔ Strong ordering guarantees

Diagram:

Services → ZooKeeper → Coordination

etcd – When You SHOULD Use It​

✔ Kubernetes state
✔ Service discovery
✔ Feature flags
✔ Distributed config
✔ Cloud-native control planes

Diagram:

Nodes → etcd → Watch → React

9. Watch / Notification Capability​

FeatureRedisZooKeeperetcd
WatchPub/SubWatcherNative Watch
ReliabilityBest-effortStrongStrong
Replay❌❌✅

etcd Watch Model​

Client sets watch
↓
Key changes
↓
Guaranteed event delivery

10. Scalability Characteristics​

AspectRedisZooKeeperetcd
Horizontal scaleExcellentPoorLimited
Data sizeVery largeVery smallSmall-medium
ClientsMillionsThousandsThousands

Why ZooKeeper Does NOT Scale Like Redis​

All writes → Leader
All coordination → Leader

This is intentional.


11. Operational Complexity​

AspectRedisZooKeeperetcd
SetupEasyHardMedium
DebuggingMediumHardMedium
TuningMany knobsMany knobsFew knobs
Cloud-nativePartialNoYes

12. Security & Access Control​

FeatureRedisZooKeeperetcd
AuthACL / AUTHACLTLS + RBAC
EncryptionTLSTLSTLS
Multi-tenancyLimitedLimitedStrong

13. What NOT to Do (Very Important)​

❌ Do NOT use Redis for:​

  • Distributed locks requiring strict correctness
  • Leader election with safety guarantees
  • Source of truth metadata

❌ Do NOT use ZooKeeper for:​

  • Large datasets
  • High-QPS application reads
  • Caching

❌ Do NOT use etcd for:​

  • Hot data paths
  • Large object storage
  • High-frequency counters

14. Decision Matrix (Quick Guide)​

Need speed? ───────────▶ Redis
Need correctness? ─────▶ ZooKeeper
Need cloud control? ───▶ etcd

15. Mental Model Summary​

Redis = Data Plane
ZooKeeper = Coordination Plane
etcd = Control Plane

They complement, not replace, each other.


16. Final Takeaway​

  • Redis is fast but not authoritative
  • ZooKeeper is authoritative but heavy
  • etcd is authoritative and cloud-native

The best systems often use all three, each for what it does best.


Deep Dive into Redis VII: Commands

· 阅读需 11 分钟

📊 Document Overview​

Organized by usage frequency and modern patterns

🏆 Top 10 Most Used Redis Commands​

1. SET - Store Key-Value Pair​

Purpose: Store string value with optional expiration

Syntax:

SET key value [EX seconds|PX milliseconds] [NX|XX]

Real-life Scenarios:

  • User session storage
  • Cache with TTL (Time-To-Live)
  • Feature flag storage
  • Temporary data storage

Best Practices:

# Cache with 60-second expiration
SET user:123:profile "{name: 'John', email: 'john@email.com'}" EX 60

# Set only if key doesn't exist (for locks)
SET lock:order:456 "processing" EX 30 NX

# Set only if key exists (for updates)
SET user:123:status "active" XX

Diagram:

┌─────────────────────────────────────────────┐
│ SET Command Flow │
├─────────────────────────────────────────────┤
│ Client Redis Server │
│ │ │ │
│ │ SET key value EX 30 │ │
│ │──────────────────────────>│ │
│ │ │ │
│ │ "OK" │ │
│ │<──────────────────────────│ │
│ │ │ │
│ │ (30 seconds later) │ │
│ │ GET key │ │
│ │──────────────────────────>│ │
│ │ (nil) │ │
│ │<──────────────────────────│ │
└─────────────────────────────────────────────┘

2. GET - Retrieve Key Value​

Purpose: Get value of a key

Syntax: GET key

Real-life Scenarios:

  • Retrieve cached data
  • Read configuration
  • Check feature flags

Examples:

# Basic get
GET user:456:profile

# Use with EXISTS check first
EXISTS user:456:profile
GET user:456:profile

# Pattern for cache miss handling
GET product:789
# If nil, fetch from database and SET with EX

3. DEL - Delete Keys​

Purpose: Remove one or more keys

Syntax: DEL key [key ...]

Best Practices:

# Delete single key
DEL user:123:session

# Delete multiple keys
DEL user:123:profile user:123:preferences user:123:cart

# Safer deletion with EXISTS check
EXISTS important:config
DEL important:config

# Use UNLINK for large keys in production (non-blocking)
UNLINK large:dataset:456

4. INCR/DECR - Atomic Counters​

Purpose: Increment/decrement integer values atomically

Real-life Scenarios:

  • Page view counters
  • Rate limiting
  • Inventory management
  • Unique ID generation

Examples:

# Basic counter
SET page:views 0
INCR page:views # Returns 1
INCR page:views # Returns 2

# Rate limiting pattern
INCR rate:limit:192.168.1.1
EXPIRE rate:limit:192.168.1.1 60

# Decrement inventory
SET product:789:stock 100
DECR product:789:stock # Returns 99

# Increment by specific amount
INCRBY user:123:score 50
DECRBY user:123:score 20

Rate Limiting Diagram:

┌─────────────────────────────────────────────────┐
│ Rate Limiting Pattern │
├─────────────────────────────────────────────────┤
│ Minute 1 │ Minute 2 │ Minute 3 │
│ ┌─────────┐ │ ┌─────────┐ │ ┌─────────┐ │
│ │Request 1│ │ │Request 1│ │ │Request 1│ │
│ │INCR key │ │ │INCR key │ │ │INCR key │ │
│ │EXPIRE │ │ │EXPIRE │ │ │EXPIRE │ │
│ └─────────┘ │ └─────────┘ │ └─────────┘ │
│ │Request 2│ │ │Request 2│ │ │
│ │INCR→2 │ │ │INCR→2 │ │ │
│ └─────────┘ │ └─────────┘ │ │
│ │Request 3│ │ │Request 3│ │ │
│ │INCR→3 │ │ │INCR→3 │ │ │
│ └─────────┘ │ └─────────┘ │ │
│ │Request 4│ │ │Request 4│ │ │
│ │INCR→4 │ │ │GET→4 │ │ │
│ │IF >3 │ │ │IF >3 │ │ │
│ │Reject │ │ │Reject │ │ │
│ └─────────┘ │ └─────────┘ │ │
└─────────────────────────────────────────────────┘

5. EXPIRE - Set Key Expiration​

Purpose: Set timeout on key

Syntax: EXPIRE key seconds

Modern Alternatives:

# Set with expiration in one command (preferred)
SET session:abc123 "user_data" EX 3600

# Update existing key's TTL
EXPIRE cache:key 300
EXPIREAT cache:key 1672531200 # Unix timestamp

# Check remaining TTL
TTL session:abc123
# Returns: -2 (key doesn't exist)
# Returns: -1 (key exists, no expire)
# Returns: >0 (seconds remaining)

# Remove expiration
PERSIST important:config

6. HSET/HGET - Hash Operations​

Purpose: Store field-value pairs within a key

Real-life Scenarios:

  • User profiles
  • Product information
  • Object storage
  • Partial updates

Examples:

# Store user profile
HSET user:123 name "John Doe" email "john@email.com" age 30

# Get specific fields
HGET user:123 name # "John Doe"
HGET user:123 age # "30"

# Get all fields
HGETALL user:123

# Update single field
HSET user:123 age 31

# Check if field exists
HEXISTS user:123 email

# Increment numeric field
HINCRBY user:123 visits 1

# Get multiple fields
HMGET user:123 name email phone

Hash vs String Storage:

┌─────────────────┐ ┌─────────────────┐
│ String Storage │ │ Hash Storage │
├─────────────────┤ ├─────────────────┤
│ │ │ │
│ user:123 │ │ user:123 │
│ ──────────── │ │ ──────────── │
│ { │ │ name: "John" │
│ "name": │ │ email: "john@" │
│ "John", │ │ age: "30" │
│ "email": │ │ │
│ "john@...", │ │ │
│ "age": 30 │ │ │
│ } │ │ │
│ │ │ │
│ Memory: 100B │ │ Memory: 80B │
│ │ │ (more efficient│
│ │ │ for many flds)│
└─────────────────┘ └─────────────────┘

7. LPUSH/RPUSH - List Operations​

Purpose: Add elements to lists

Real-life Scenarios:

  • Message queues
  • Activity feeds
  • Recent items
  • Job queues

Examples:

# Message queue pattern
LPUSH messages:queue "{job: 'process', data: {...}}"
LPUSH messages:queue "{job: 'email', data: {...}}"

# Worker processing
RPOP messages:queue # Gets oldest item

# Recent activities
LPUSH user:123:activity "Logged in"
LPUSH user:123:activity "Viewed product:789"
LPUSH user:123:activity "Purchased item"

# Get range (last 10 activities)
LRANGE user:123:activity 0 9

# Fixed-length list (news feed)
LPUSH feed:global "News item 1"
LTRIM feed:global 0 99 # Keep only 100 items

Message Queue Pattern:

┌─────────────┐ LPUSH ┌─────────────┐ RPOP/BLPOP ┌─────────────┐
│ Producer │───────────>│ Redis │<───────────────│ Consumer │
│ │ │ List │ │ │
│ │ │ ┌───────┐ │ │ │
│ │ │ │ Msg 3 │ │ │ │
│ │ │ │ Msg 2 │ │ │ │
│ │ │ │ Msg 1 │ │ │ │
│ │ │ └───────┘ │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
│ │ │
│ (FIFO Queue) │
└──────────────────────────────────────────────────────────┘

8. SADD/SMEMBERS - Set Operations​

Purpose: Store unique unordered elements

Real-life Scenarios:

  • Tags/categories
  • Unique visitors
  • Friends/followers
  • Vote tracking

Examples:

# Store tags
SADD article:456:tags "redis" "database" "nosql" "cache"

# Check membership
SISMEMBER article:456:tags "redis" # Returns 1 (true)

# Get all members
SMEMBERS article:456:tags

# Set operations (powerful!)
SADD users:online "user1" "user2" "user3"
SADD users:active "user2" "user3" "user4"

# Intersection (users both online AND active)
SINTER users:online users:active # ["user2", "user3"]

# Union (all unique users)
SUNION users:online users:active # ["user1","user2","user3","user4"]

# Difference (online but not active)
SDIFF users:online users:active # ["user1"]

# Random element (lottery, sampling)
SRANDMEMBER users:online
SPOP users:online # Removes and returns random

9. ZADD/ZRANGE - Sorted Set Operations​

Purpose: Store sorted unique elements with scores

Real-life Scenarios:

  • Leaderboards
  • Time-series data
  • Priority queues
  • Range queries

Examples:

# Leaderboard
ZADD leaderboard 1000 "player1"
ZADD leaderboard 850 "player2"
ZADD leaderboard 920 "player3"

# Get top 3 players
ZREVRANGE leaderboard 0 2 WITHSCORES

# Get player rank (0-indexed from top)
ZREVRANK leaderboard "player2"

# Increment score
ZINCRBY leaderboard 50 "player1"

# Range queries (scores 800-900)
ZRANGEBYSCORE leaderboard 800 900

# Time-series pattern
ZADD temperature:nyc 1672444800 "38.5" # timestamp, value
ZADD temperature:nyc 1672448400 "39.2"
ZRANGEBYSCORE temperature:nyc 1672444800 1672448400

Leaderboard Architecture:

┌─────────────────────────────────────────────────┐
│ Redis Sorted Set Leaderboard │
├─────────────────────────────────────────────────┤
│ Score │ Member │ Operations │
├──────────┼─────────────────┼───────────────────┤
│ 1050 │ player1 │ ZADD score │
│ 920 │ player3 │ ZINCRBY +50 │
│ 850 │ player2 │ ZREVRANGE 0 2 │
│ 720 │ player4 │ ZREVRANK player2 │
│ 650 │ player5 │ ZRANGEBYSCORE │
│ │ │ 800 1000 │
└──────────┴─────────────────┴───────────────────┘
▲
│
└─── Scores automatically sorted,
O(log N) operations

10. SCAN - Safe Key Iteration​

Purpose: Incrementally iterate keys without blocking

Why not KEYS?

  • KEYS * blocks Redis (use in production with extreme caution)
  • SCAN is non-blocking, production-safe

Examples:

# Iterate through all keys matching pattern
SCAN 0 MATCH user:* COUNT 100

# Iterate specific type
SCAN 0 MATCH product:* COUNT 50 TYPE string

# Hash scan
HSCAN user:123 0 MATCH *name* COUNT 10

# Set scan
SSCAN tags:article:456 0 COUNT 20

# Sorted set scan
ZSCAN leaderboard 0 COUNT 15

SCAN Iteration Pattern:

┌─────────────────────────────────────────────┐
│ SCAN Iteration Process │
├─────────────────────────────────────────────┤
│ Initial: SCAN 0 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "5" (next cursor) │
│ 2) ["user:1", "user:2", "user:3"] │
│ │
│ Next: SCAN 5 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "12" (next cursor) │
│ 2) ["user:4", "user:5"] │
│ │
│ Next: SCAN 12 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "0" (iteration complete) │
│ 2) ["user:6"] │
│ │
│ Cursor 0 = Done │
└─────────────────────────────────────────────┘

🏗️ Data Structure Selection Guide​

┌─────────────────────────────────────────────────────────────┐
│ Choosing the Right Data Structure │
├──────────────┬────────────────┬─────────────┬───────────────┤
│ Use Case │ Structure │ Best For │ Example │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Simple Cache │ String │ Single vals │ SET/GET │
│ │ │ With TTL │ user:session │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Object Store │ Hash │ Multiple │ HSET/HGETALL │
│ │ │ fields │ user:profile │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Queue │ List │ FIFO/LIFO │ LPUSH/RPOP │
│ │ │ │ jobs:queue │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Unique Items │ Set │ Membership │ SADD/SISMEMBER│
│ │ │ Operations │ article:tags │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Rankings │ Sorted Set │ Scores │ ZADD/ZRANGE │
│ │ │ Ranges │ leaderboard │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Time Series │ Sorted Set │ Timestamps │ ZADD ts:data │
│ │ or Stream │ Sequences │ XADD log:stream│
└──────────────┴────────────────┴─────────────┴───────────────┘

⚡ Advanced & Modern Patterns​

11. Pipeline - Batch Operations​

Purpose: Reduce round-trip time

# Without pipeline (3 round trips)
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30

# With pipeline (1 round trip)
MULTI
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30
EXEC

12. Transactions - Atomic Operations​

MULTI
GET account:123:balance
DECRBY account:123:balance 100
INCRBY account:456:balance 100
EXEC

13. Lua Scripting - Complex Atomic Operations​

EVAL "local current = redis.call('GET', KEYS[1])
if tonumber(current) < tonumber(ARGV[1]) then
return redis.call('INCR', KEYS[1])
else
return nil
end" 1 rate:limit:ip 10

14. Streams - Modern Message Queue​

# Producer
XADD mystream * sensor-id 1234 temperature 19.8

# Consumer group
XGROUP CREATE mystream mygroup 0

# Consumer
XREADGROUP GROUP mygroup consumer1 COUNT 1 BLOCK 5000 STREAMS mystream >

🔐 Security & Production Commands​

15. AUTH - Authentication​

AUTH your_strong_password_here

# Better: Use Redis 6+ ACL
ACL SETUSER alice on >password ~cache:* +get +set

16. CONFIG - Configuration Management​

# Get configuration
CONFIG GET maxmemory
CONFIG GET requirepass

# Set configuration (prefer redis.conf for persistence)
CONFIG SET maxmemory 2gb
CONFIG SET save "900 1 300 10 60 10000"

# Never expose these in production:
CONFIG SET requirepass "" # DON'T!
CONFIG SET protected-mode no # DON'T!

17. INFO - Monitoring​

# Get all info (verbose)
INFO

# Get specific sections
INFO memory
INFO stats
INFO clients
INFO replication

# Critical metrics to monitor:
INFO memory # used_memory, maxmemory, evictions
INFO stats # instantaneous_ops_per_sec, keyspace_hits/misses
INFO persistence # rdb_last_save_time, aof_rewrite_in_progress

🎯 Memory Optimization Commands​

18. MEMORY USAGE​

MEMORY USAGE large:key
MEMORY STATS
MEMORY PURGE # Attempts to defragment (Redis 4+)

19. Efficient Data Encoding​

# Use appropriate data types
# Strings under 44 bytes use EMBSTR encoding
SET small:key "short" # More efficient

# Hashes with <= 512 fields use ziplist
HSET config:app field1 "val1" ... field500 "val500"

# Lists with small elements use ziplist
LPUSH recent:items "item1" "item2" # Check list-max-ziplist* configs

📈 Performance Optimization Patterns​

Connection Pooling Diagram:​

┌─────────────┐ ┌─────────────────┐ ┌─────────────┐
│ App │ │ Connection │ │ Redis │
│ Server │─────▶│ Pool │─────▶│ Server │
│ │ │ │ │ │
│ Request 1 │ │ ┌───────────┐ │ │ │
│ ───────── │ │ │ Connection│──┼─────▶│ Process │
│ GET key │ │ │ 1 │ │ │ Request │
│ │ │ └───────────┘ │ │ │
│ Request 2 │ │ ┌───────────┐ │ │ │
│ ───────── │ │ │ Connection│──┼─────▶│ Process │
│ SET key │───┬─▶│ │ 2 │ │ │ Request │
│ │ │ │ └───────────┘ │ │ │
│ Request 3 │ │ │ ┌───────────┐ │ │ │
│ ───────── │ └─▶│ │ Connection│──┼─────▶│ Process │
│ INCR key │ │ │ 3 │ │ │ Request │
│ │ │ └───────────┘ │ │ │
└─────────────┘ └─────────────────┘ └─────────────┘

20. Monitor Slow Logs​

# Configure slow log
CONFIG SET slowlog-log-slower-than 10000 # 10ms
CONFIG SET slowlog-max-len 128

# View slow logs
SLOWLOG GET 10
SLOWLOG LEN
SLOWLOG RESET

🚫 Anti-Patterns to Avoid​

Bad Patterns:​

# 1. KEYS * in production (use SCAN instead)
KEYS * # BLOCKS SERVER!

# 2. Large values in single key
SET huge:key "[...10MB of data...]" # Blocks during serialization

# 3. Not using TTL for cache
SET cache:key "data" # Might fill memory

# 4. Using Redis as primary database
# Redis is in-memory - data can be lost

# 5. Not monitoring memory usage
# Leads to OOM (Out Of Memory) errors

Good Patterns:​

# 1. Use pipelining for bulk operations
MULTI
SET key1 "val1"
SET key2 "val2"
EXEC

# 2. Use appropriate data structures
# For object with many fields: Hash, not multiple String keys

# 3. Always set TTL for cache
SET session:abc "data" EX 3600

# 4. Use Lua scripts for complex atomic operations

# 5. Monitor with INFO and SLOWLOG

🔄 Replication & High Availability Commands​

21. Replication Management​

# On replica
REPLICAOF master-ip 6379
REPLICAOF NO ONE # Promote to master

# Check replication status
INFO replication

# Monitor replication lag
redis-cli --latency

22. Sentinel & Cluster Commands​

# Sentinel
SENTINEL MASTERS
SENTINEL GET-MASTER-ADDR-BY-NAME mymaster

# Cluster
CLUSTER NODES
CLUSTER INFO
CLUSTER SLOTS

🧹 Maintenance Commands​

23. Backup & Persistence​

# Manual save (blocks)
SAVE # Synchronous, blocks
BGSAVE # Asynchronous, forks

# Check persistence status
INFO persistence
LASTSAVE # Unix timestamp of last save

# AOF operations
BGREWRITEAOF

24. Cleanup & Debugging​

# Flush data (CAUTION!)
FLUSHDB # Current database
FLUSHALL # All databases

# Debug object
DEBUG OBJECT keyname

# Monitor commands in real-time
MONITOR # Use sparingly, verbose

# Client management
CLIENT LIST
CLIENT KILL addr ip:port
CLIENT PAUSE timeout_ms # Pause clients

📊 Monitoring Dashboard Metrics​

┌─────────────────────────────────────────────────────┐
│ Redis Health Dashboard │
├─────────────────┬─────────────────┬─────────────────┤
│ Memory Usage │ Command Stats │ Connections │
├─────────────────┼─────────────────┼─────────────────┤
│ Used: 1.2GB/2GB│ Ops/sec: 5,000 │ Connected: 45 │
│ 60% │ Hits: 98% │ Max: 10,000 │
│ Peak: 1.8GB │ Misses: 2% │ Rejected: 0 │
│ Evictions: 0 │ Cmd/sec: 8,000 │ Timeout: 0 │
├─────────────────┼─────────────────┼─────────────────┤
│ Replication │ Persistence │ Keyspace │
├─────────────────┼─────────────────┼─────────────────┤
│ Role: Master │ RDB: Enabled │ DB0 Keys: 45K │
│ Slaves: 2 │ Last Save: 2m │ Expires: 12K │
│ Lag: 0.5s │ AOF: Enabled │ Avg TTL: 300s │
│ Status: OK │ Size: 500MB │ Hit Rate: 99% │
└─────────────────┴─────────────────┴─────────────────┘

🚀 Quick Reference Cheat Sheet​

Caching Patterns:​

# Cache-aside pattern
1. GET cache:key
2. If nil, fetch from DB
3. SET cache:key value EX 300

# Write-through pattern
1. Update DB
2. SET cache:key new_value EX 300

# Write-behind pattern
1. SET cache:key new_value EX 300
2. Async update to DB

Key Design Best Practices:​

Good: Bad:
user:123:profile user_profile_123
session:abc123 abc123_session
product:789:views product_views_789
geo:nyc:users newyork_users_list

Pattern: object_type:id:field

TTL Strategies:​

# Cache: 5 minutes - 1 hour
SET cache:key value EX 300

# Session: 30 minutes - 24 hours
SET session:key data EX 1800

# Rate limit: 1 second - 1 minute
SET rate:limit:ip 1 EX 60 NX

# Leaderboard: 1 day - 7 days
EXPIRE leaderboard:daily 86400

🔍 Troubleshooting Commands​

25. Diagnose Issues​

# Check if Redis is alive
PING # Should return PONG

# Check latency between commands
redis-cli --latency

# Monitor commands in real-time (debug)
MONITOR

# Check memory fragmentation
INFO memory | grep fragmentation

# List connected clients
CLIENT LIST

# Check slow queries
SLOWLOG GET 5

26. Performance Testing​

# Benchmark tool (outside redis-cli)
redis-benchmark -t set,get -n 100000 -q

# Pipeline benchmark
redis-benchmark -t set,get -n 100000 -q -P 16

🎨 Visualization: Data Structure Memory Layout​

String:
┌────────┬────────┬────────┐
│ Header │ Data │ Null │
│ Type │ "value"│ term │
└────────┴────────┴────────┘

Hash (ziplist encoding):
┌────┬────┬────┬────┬────┬────┐
│ ZL │ F1 │ V1 │ F2 │ V2 │ ZL │
│bytes│"name"│"John"│"age"│"30"│end │
└────┴────┴────┴────┴────┴────┘

List (quicklist):
┌─────────┬─────────┬─────────┐
│ Node │ Node │ Node │
│ [ziplist│ [ziplist│ [ziplist│
│ "item1" │ "item4" │ "item7" │
│ "item2" │ "item5" │ │
│ "item3" │ "item6" │ │
└─────────┴─────────┴─────────┘

📚 Further Learning Path​

Next Steps:​

  1. Redis Streams - Modern message queues
  2. Redis Modules - RediSearch, RedisJSON, RedisGraph
  3. Redis Cluster - Horizontal scaling
  4. Redis Sentinel - High availability
  5. Redis Enterprise - Commercial features

Production Checklist:​

  • Set maxmemory and eviction policy
  • Enable persistence (RDB/AOF)
  • Configure backups
  • Set up monitoring
  • Implement connection pooling
  • Use key expiration
  • Regular maintenance
  • Disaster recovery plan

Remember: Redis is single-threaded for commands. One slow command blocks all others. Always test commands in staging before production, and monitor performance metrics regularly.

Deep Dive into Redis I

· 阅读需 37 分钟

Table of Contents​

  1. Redis Architecture: The Complete Picture
  2. Fundamentals
  3. Data Structures and Operations
  4. Persistence Mechanisms
  5. RDB and AOF File Formats Deep Dive
  6. Replication and High Availability
  7. Redis Cluster and Sharding
  8. Hash Slots Deep Dive
  9. Operational Considerations
  10. Mental Model Summary
  11. Quick Reference Guide

Redis Architecture: The Complete Picture​

Before diving into details, here's the complete Redis ecosystem at a glance:

graph TB
subgraph "Application Layer"
App[Application]
end

subgraph "Redis Layer"
subgraph "Single Instance"
Master1[Master]
Replica1[Replica]
Master1 --> Replica1
end

subgraph "Redis Cluster (Sharded)"
M1[Master A<br/>Slots 0-5460]
M2[Master B<br/>Slots 5461-10922]
M3[Master C<br/>Slots 10923-16383]
R1[Replica A]
R2[Replica B]
R3[Replica C]
M1 --> R1
M2 --> R2
M3 --> R3
end

subgraph "High Availability"
S1[Sentinel 1]
S2[Sentinel 2]
S3[Sentinel 3]
end
end

subgraph "Persistence Layer"
RDB[(RDB Snapshot)]
AOF[(AOF Log)]
end

App --> Master1
App --> M1
App --> M2
App --> M3

S1 -.Monitor.-> Master1
S2 -.Monitor.-> Master1
S3 -.Monitor.-> Master1

Master1 --> RDB
Master1 --> AOF
M1 --> RDB
M2 --> RDB
M3 --> RDB

Key Components:

ComponentPurposeWhen to Use
Single MasterBasic Redis instanceSmall datasets, simple caching
Master + ReplicaRead scaling + failoverRead-heavy workloads
SentinelAutomatic failoverHigh availability without sharding
Redis ClusterHorizontal scaling (sharding)Large datasets exceeding single node
RDBPoint-in-time snapshotsFast restarts, backups
AOFWrite operation logMaximum data durability

Decision Tree:

flowchart TD
Start{Data fits in<br/>single instance?}
Start -->|Yes| HA{Need high<br/>availability?}
Start -->|No| UseCluster[Use Redis Cluster]

HA -->|No| SingleMaster[Single Master<br/>+ optional replicas]
HA -->|Yes| UseSentinel[Master + Replicas<br/>+ Sentinel]

UseCluster --> Persist{Need persistence?}
UseSentinel --> Persist
SingleMaster --> Persist

Persist -->|Fast restart| UseRDB[Enable RDB]
Persist -->|Max durability| UseAOF[Enable AOF]
Persist -->|Best of both| UseHybrid[Enable Both<br/>Recommended]

Fundamentals​

What Is Redis?​

Redis stands for REmote DIctionary Server.

At its heart, Redis is:

  • An in-memory data store
  • Extremely fast (microsecond latency)
  • Used as:
    • Cache
    • Database
    • Message broker
    • Counter system
    • Coordination system

Redis in One Sentence​

Redis is a single-threaded, in-memory data structure server optimized for speed, simplicity, and predictable behavior.

Where Redis Fits in a System​

flowchart LR
Client --> Redis
Client --> DB[(Database)]
Redis --> DB

Redis often sits between your application and your database to:

  • Reduce load
  • Improve latency
  • Handle real-time data

Core Redis Philosophy​

Redis makes several strong design choices:

PrincipleMeaning
In-memoryData lives in RAM for speed
Single-threadedNo locks, no race conditions
Simple data structuresPredictable performance
Explicit durabilityYou choose persistence level

Why Single-Threaded Is a Feature (Not a Bug)​

sequenceDiagram
participant Client1
participant Client2
participant Redis

Client1->>Redis: INCR counter
Client2->>Redis: INCR counter
Redis-->>Client1: OK
Redis-->>Client2: OK

Because Redis processes one command at a time:

  • No locks
  • No inconsistent states
  • Operations like INCR are naturally atomic

Redis Consistency Model​

Redis favors:

Consistency per key, not global consistency

Key Guarantees:

  • Single command = atomic
  • No partial writes
  • Replica lag possible
  • Cluster operations limited across shards

Redis Performance Model​

Why Redis is fast:

graph TD
RAM --> NoDiskIO
SingleThread --> NoLocks
SimpleCommands --> PredictableLatency

Typical Latency:

OperationLatency
GET~1µs
INCR~1–2µs
Network RTTDominant cost

Redis Is Not a Silver Bullet​

Redis trades features for speed.

What Redis Is Great At:

  • Counters
  • Caching
  • Rate limiting
  • Queues
  • Leaderboards
  • Distributed locks

What Redis Is Not Good At:

  • Complex queries
  • Large cold datasets
  • Long-term storage without planning

Data Structures and Operations​

Redis Data Model (Mental Model)​

Redis is not a table store.

Think of Redis as a huge hash map in memory:

graph TD
Redis --> Key1["user:1"]
Redis --> Key2["order:991"]
Redis --> Key3["counter:login"]

Each key:

  • Is a string
  • Maps to a data structure, not just a value

Redis Data Structures (Conceptual View)​

Redis supports rich native data types:

TypeThink of it as
StringBasic value / counter
HashObject
ListQueue / Stack
SetUnique collection
Sorted SetRanking system
StreamAppend-only log

Example Mental Mapping:

graph LR
AppObject --> RedisHash["user:42 (hash)"]
Queue --> RedisList["jobs:list"]
Ranking --> ZSet["leaderboard"]

Redis as a Counter System (INCR)​

Counters are one of Redis's killer features.

Why INCR Is Special:

  • Atomic
  • Lock-free
  • Extremely fast
  • Works under massive concurrency

How INCR Works Internally:

sequenceDiagram
participant App1
participant App2
participant Redis

App1->>Redis: INCR page_views
App2->>Redis: INCR page_views
Redis->>Redis: page_views = page_views + 1
Redis-->>App1: 101
Redis-->>App2: 102

Because Redis is single-threaded:

  • No race conditions
  • No lost updates

How Redis Stores Data​

Important Clarification:

Redis does NOT understand { "user": "whereq" } as a data structure by default. Redis only sees bytes.

The application must decide how to store data:

Option 1: Simple key-value

SET user whereq

Stored as:

"user" -> "whereq"

Option 2: Namespaced key

SET user:whereq ""

Stored as:

"user:whereq" -> ""

Option 3: JSON serialized

SET user:whereq '{"user":"whereq"}'

Stored as:

"user:whereq" -> '{"user":"whereq"}'

Option 4: Redis Hash (logical structure)

HSET user:whereq user whereq

Stored internally as:

"user:whereq" -> hash(user=whereq)

Persistence Mechanisms​

Overview​

Redis is fundamentally an in-memory database, but it provides optional durability through two complementary persistence mechanisms:

graph LR
Memory["In-Memory Data<br/>(Source of Truth)"] --> RDB["RDB<br/>(Point-in-Time Snapshot)"]
Memory --> AOF["AOF<br/>(Write Command Log)"]

Key Principle:

Memory is always the source of truth. RDB and AOF are derived artifacts for recovery.

AspectRDBAOF
What it storesBinary snapshot of all dataSequential log of write commands
When it writesPeriodic (configurable intervals)After every write (configurable)
Recovery speedFast (binary load)Slower (replay commands)
Data loss riskHigher (between snapshots)Lower (per-write durability)
File sizeCompactLarger (needs rewriting)
Best forBackups, replica syncMaximum durability

Data Flow Through Persistence:

sequenceDiagram
participant Client
participant Redis Memory
participant AOF
participant RDB

Client->>Redis Memory: SET key value
Redis Memory->>Client: OK
Redis Memory->>AOF: Append command (immediate)

Note over Redis Memory,RDB: Later (based on save intervals)
Redis Memory->>RDB: Fork & snapshot all data

Note over Redis Memory: On restart:
alt AOF exists
AOF->>Redis Memory: Replay commands
else RDB only
RDB->>Redis Memory: Load snapshot
end

Relationship Between Components:

┌───────────────────────────────────────────────┐
│ │
│ WRITES FLOW (Write Command) │
│ │
│ Client → Memory → AOF (append) │
│ ↓ │
│ (periodic) │
│ ↓ │
│ RDB (snapshot) │
│ │
└───────────────────────────────────────────────┘

┌───────────────────────────────────────────────┐
│ │
│ RECOVERY FLOW (Redis Restart) │
│ │
│ 1. Check if AOF exists? │
│ ├─ Yes → Load AOF (replay all commands) │
│ └─ No → Load RDB (restore snapshot) │
│ │
│ 2. Memory rebuilt │
│ 3. Ready to serve │
│ │
└───────────────────────────────────────────────┘

RDB (Snapshotting)​

What RDB Really Is:

RDB = Redis DataBase file

It is:

  • A binary file on disk
  • Representing a point-in-time snapshot of Redis memory
  • Created by Redis itself, not by the OS

Think of RDB as:

📸 "A photograph of all Redis data at a specific moment in time"

What is inside an RDB file?

  • Every key
  • Every value
  • The data type of each value
  • Expiration metadata (TTL)
graph TD
RAM["Redis In-Memory Data"] --> Snapshot["RDB File on Disk"]

Is RDB a runtime state?

No. RDB exists outside Redis memory.

Redis runtime state is:

  • Live data in RAM
  • Active connections
  • Client buffers

RDB is only used for:

  • Restart recovery
  • Backup
  • Replication bootstrap

How RDB is created (conceptually):

Redis:

  1. Forks a child process
  2. Child process reads memory
  3. Writes a compact binary file
  4. Parent keeps serving requests
sequenceDiagram
Redis->>OS: fork()
Child->>Disk: write snapshot
Parent->>Clients: continue serving

✅ No blocking of the main Redis thread ❗ Extra memory during fork (copy-on-write)

When Redis uses RDB:

SituationUse RDB?
Redis restart✅
Full data reload✅
Replica sync (initial)✅
Incremental updates❌

Characteristics:

  • Periodic full snapshot
  • Fast restart
  • Risk of data loss between snapshots
timeline
t1 : Snapshot
t2 : Snapshot
t3 : Crash (data lost between t2 and t3)

AOF (Append Only File)​

What AOF Is:

  • Logs every write
  • More durable
  • Slightly slower
flowchart LR
Write --> AppendLog --> Disk

How Redis Manages Both RDB and AOF Together​

Key clarification:

There is NO extra daemon, NO sync process, NO background service.

Everything is handled by the Redis server process itself.

RDB vs AOF – different purposes:

RDBAOF
SnapshotCommand log
PeriodicContinuous
Fast restoreDurable
CompactLarger

They do not sync with each other.

How Redis actually works with both enabled:

graph TD
Write --> Memory
Write --> AOF
Timer --> RDB

Important insight:

  • Memory is the source of truth
  • RDB and AOF are derived artifacts

Restart logic (very important):

When Redis restarts:

  1. If AOF exists → AOF is used
  2. Else → RDB is used
flowchart TD
Start --> AOFExists{AOF exists?}
AOFExists -->|Yes| LoadAOF
AOFExists -->|No| LoadRDB

Why no syncing is needed:

Because:

  • Redis never writes from RDB to AOF
  • Redis never writes from AOF to RDB
  • Both are written from memory

Memory → Persistence Not persistence → memory → persistence

Production Reality:

Most systems use:

graph TD
Redis --> RDB
Redis --> AOF

Snapshots for speed + AOF for safety.

Persistence and Migration​

When slot migration occurs:

  • Migration moves keys in RAM
  • Does NOT force persistence
  • Persistence happens later

If a master crashes after migration but before snapshot:

Redis handles this safely:

  1. Master restarts and loads entire RDB into RAM
  2. Cluster metadata (slot ownership) overrides RDB contents
  3. Keys that no longer belong to this master are marked invalid
  4. They will NOT be served and will eventually be deleted

Key Principle:

Cluster metadata (slot ownership) overrides RDB contents

Will snapshot happen automatically after migration?

No. Slot migration does NOT automatically trigger an RDB snapshot.

Redis deliberately decouples:

  • data movement
  • persistence timing

Otherwise, resharding would cause massive disk IO spikes and latency stalls.

Important:

  • RDB = local historical photo
  • RAM = source of truth
  • Slots = routing metadata
  • Migration = RAM → RAM transfer
  • Persistence = future snapshots reflect reality

RDB and AOF File Formats Deep Dive​

This section explores the internal structure of Redis persistence files, how they are organized on disk, and the engineering trade-offs made in their design.

RDB File Format and Structure​

What's Inside an RDB File?

An RDB file is a binary serialization of Redis's entire in-memory dataset at a specific point in time.

graph TD
RDB[RDB File] --> Header[Header: Magic + Version]
RDB --> DB0[Database 0]
RDB --> DB1[Database 1]
RDB --> DBN[Database N]
RDB --> EOF[EOF Marker + Checksum]

DB0 --> Meta0[Metadata: DB selector]
DB0 --> Keys0[Key-Value Pairs]
DB0 --> Expire0[Expiry Information]

RDB File Structure (Sequential Layout):

┌─────────────────────────────────────────────────────────┐
│ MAGIC NUMBER: "REDIS" (5 bytes) │
├─────────────────────────────────────────────────────────┤
│ VERSION: "0009" (4 bytes, current version) │
├─────────────────────────────────────────────────────────┤
│ AUXILIARY FIELDS (optional metadata) │
│ - Redis version │
│ - Creation timestamp │
│ - Used memory │
│ - Replication info │
├─────────────────────────────────────────────────────────┤
│ DATABASE SELECTOR (e.g., DB 0) │
├─────────────────────────────────────────────────────────┤
│ RESIZE DB (hash table size hints) │
├─────────────────────────────────────────────────────────┤
│ KEY-VALUE PAIRS │
│ ┌──────────────────────────────────────┐ │
│ │ Type byte (string/list/hash/etc.) │ │
│ │ Key (encoded string) │ │
│ │ Value (type-specific encoding) │ │
│ │ Expiry (optional, millisecond TTL) │ │
│ └──────────────────────────────────────┘ │
│ ... (repeat for all keys in DB) │
├─────────────────────────────────────────────────────────┤
│ DATABASE SELECTOR (e.g., DB 1) │
│ ... (more key-value pairs) │
├─────────────────────────────────────────────────────────┤
│ EOF MARKER (0xFF) │
├─────────────────────────────────────────────────────────┤
│ CRC64 CHECKSUM (8 bytes) │
└─────────────────────────────────────────────────────────┘

Key-Value Encoding:

Each data type has optimized encoding:

Data TypeEncoding StrategyExample
StringRaw bytes, integer encoding for numbers"123" → 3-byte integer vs 3-byte string
ListZiplist (compact) or LinkedListSmall lists: compressed format
HashZiplist or Hash TableDepends on size thresholds
SetIntSet or Hash TableInteger-only sets: compact array
Sorted SetZiplist or Skip ListHybrid encoding based on size
StreamRadix tree nodesSpecialized compact format

Example: String Encoding

Small integers (0-9999):
Type: 1 byte (REDIS_ENCODING_INT)
Value: variable length integer encoding

Short strings (<= 20 bytes):
Type: 1 byte
Length: 1 byte
Data: N bytes

Long strings:
Type: 1 byte
Length: 4 bytes (encoded)
Data: N bytes

LZF Compressed strings:
Type: 1 byte + compression flag
Compressed length: variable
Uncompressed length: variable
Compressed data: N bytes

Does RDB Have Indexes?

❌ No — RDB has NO internal indexes.

RDB is designed for:

  • Sequential write (fast snapshot creation)
  • Sequential read (fast full restore)
  • Minimal file size (no index overhead)

How RDB Restore Works:

sequenceDiagram
participant Redis
participant RDB File
participant Memory

Redis->>RDB File: Open and validate checksum
RDB File->>Redis: Read header
Redis->>RDB File: Read DB selector
loop For each key-value pair
RDB File->>Redis: Read type + key + value
Redis->>Memory: Insert into hash table
Redis->>Memory: Set expiry if present
end
RDB File->>Redis: EOF marker
Redis->>Redis: Validate CRC64
Redis->>Memory: Rebuild indexes (if needed)

Key Point:

RDB is a dump, not a database. It's optimized for bulk I/O, not random access.

RDB Trade-offs:

Advantage ✅Disadvantage ❌
Compact binary formatNo indexes = can't query RDB directly
Fast to write (sequential I/O)Must load entire file to access any data
Fast to read (sequential load)Memory spike during fork (copy-on-write)
Built-in compression (LZF)Periodic snapshots = potential data loss
CRC64 integrity checkLarge datasets = long snapshot time
Portable across architecturesNot human-readable

RDB Creation Process (Technical Details):

flowchart TD
Start[Write command triggers save] --> Fork[fork() creates child process]
Fork --> COW[Parent uses Copy-on-Write]
Fork --> Child[Child has memory snapshot]

COW --> Parent[Parent continues serving requests]
Child --> Iterate[Iterate all databases]
Iterate --> Encode[Encode each key-value]
Encode --> Write[Sequential write to temp file]
Write --> Rename[Atomic rename to dump.rdb]
Rename --> Exit[Child exits]

Parent --> Continue[Continues normally]
Exit --> Continue

Why RDB Uses fork():

  • Parent process never blocks on disk I/O
  • Child gets consistent point-in-time snapshot
  • Copy-on-write = minimal memory overhead (only modified pages are copied)

Storage Organization:

Redis Data Directory:
/var/lib/redis/
├── dump.rdb ← Current RDB snapshot
├── temp-<pid>.rdb ← Temporary file during save (deleted after rename)
└── appendonly.aof ← AOF file (if enabled)

RDB Configuration:

# Automatic snapshots
save 900 1 # After 900 sec (15 min) if at least 1 key changed
save 300 10 # After 300 sec (5 min) if at least 10 keys changed
save 60 10000 # After 60 sec if at least 10000 keys changed

# Compression
rdbcompression yes # Use LZF compression (CPU vs size trade-off)

# Checksum
rdbchecksum yes # CRC64 integrity check

AOF File Format and Structure​

What's Inside an AOF File?

AOF (Append-Only File) is a text-based log of every write operation, stored in RESP protocol format (Redis Serialization Protocol).

graph TD
AOF[AOF File] --> Cmd1[Command 1: SET key1 value1]
AOF --> Cmd2[Command 2: INCR counter]
AOF --> Cmd3[Command 3: LPUSH list item]
AOF --> CmdN[Command N: ...]

Cmd1 --> RESP1[RESP Format]
Cmd2 --> RESP2[RESP Format]

AOF File Structure (Sequential Log):

┌─────────────────────────────────────────────────────────┐
│ *2 ← Array with 2 elements │
│ $6 ← Bulk string, 6 bytes │
│ SELECT ← Command name │
│ $1 ← Bulk string, 1 byte │
│ 0 ← Database number │
├─────────────────────────────────────────────────────────┤
│ *3 ← Array with 3 elements │
│ $3 ← Bulk string, 3 bytes │
│ SET ← Command name │
│ $4 ← Bulk string, 4 bytes │
│ key1 ← Key │
│ $6 ← Bulk string, 6 bytes │
│ value1 ← Value │
├─────────────────────────────────────────────────────────┤
│ *2 │
│ $4 │
│ INCR │
│ $7 │
│ counter │
├─────────────────────────────────────────────────────────┤
│ ... (commands continue chronologically) │
└─────────────────────────────────────────────────────────┘

RESP Protocol Format:

RESP Types:
*N = Array with N elements
$N = Bulk string with N bytes
+ = Simple string
- = Error
: = Integer

Example: Storing a Hash

HSET user:1 name "Alice" age 30

Becomes in AOF:

*5
$4
HSET
$6
user:1
$4
name
$5
Alice
$3
age
$2
30

Does AOF Have Indexes?

❌ No — AOF also has NO indexes.

AOF is:

  • Append-only (writes only at the end)
  • Sequential read (replay from start)
  • No random access needed

How AOF Restore Works:

sequenceDiagram
participant Redis
participant AOF File
participant Memory

Redis->>AOF File: Open file
loop For each command
AOF File->>Redis: Read RESP command
Redis->>Redis: Parse command
Redis->>Memory: Execute command
Redis->>Memory: Update data structures
end
AOF File->>Redis: EOF reached
Redis->>Memory: Ready to serve

AOF Rewrite Process:

Over time, AOF grows as it logs every write. Redis periodically rewrites AOF to compact it.

flowchart TD
Start[AOF grows too large] --> Trigger[Auto-rewrite triggered]
Trigger --> Fork[fork() child process]

Fork --> Parent[Parent: Continue logging to old AOF]
Fork --> Child[Child: Iterate current memory]

Child --> Snapshot[Read current state from memory]
Snapshot --> Generate[Generate minimal commands]
Generate --> NewAOF[Write to temp AOF file]

Parent --> Buffer[Buffer new writes in memory]
NewAOF --> Append[Append buffered writes]
Append --> Rename[Atomic rename to appendonly.aof]
Rename --> Replace[Old AOF replaced]

Before Rewrite:

SET counter 1
INCR counter
INCR counter
INCR counter
DEL temp_key
SET temp_key value
DEL temp_key
SET user:1 Alice
SET user:1 Bob
... (thousands of operations)

After Rewrite:

SET counter 4
SET user:1 Bob
... (only final state)

AOF Rewrite = Converting history into current state

AOF Fsync Strategies:

AOF offers three durability levels via appendfsync:

StrategyBehaviorDurabilityPerformanceData Loss Risk
alwaysfsync() after every writeMaximumSlowest~0 (only crash during write)
everysecfsync() once per secondGoodFast~1 second of data
noLet OS decide when to flushMinimalFastestPotentially 30+ seconds

Technical Detail:

// Simplified AOF write flow
write_command_to_aof_buffer(command);

if (appendfsync == ALWAYS) {
fsync(aof_fd); // Force write to disk NOW
} else if (appendfsync == EVERYSEC) {
// Background thread calls fsync() every second
schedule_fsync_background();
} else {
// OS will flush when it wants
}

Storage Organization:

Redis Data Directory:
/var/lib/redis/
├── appendonly.aof ← Main AOF file
├── appendonly.aof.manifest ← Manifest for multi-part AOF (Redis 7+)
├── appendonly.aof.1.base.rdb ← Base RDB (Redis 7+)
├── appendonly.aof.1.incr.aof ← Incremental AOF (Redis 7+)
└── temp-rewriteaof-<pid>.aof ← Temporary during rewrite

Redis 7+ Multi-Part AOF:

Redis 7 introduced AOF 2.0 with multiple files:

graph LR
Base[Base RDB Snapshot] --> Incr1[Incremental AOF 1]
Incr1 --> Incr2[Incremental AOF 2]
Incr2 --> IncrN[Incremental AOF N]

Benefits:

  • Faster rewrite (doesn't block as long)
  • Better crash recovery
  • Lower memory overhead

AOF Trade-offs:

Advantage ✅Disadvantage ❌
Much better durability (per-write)Larger file size than RDB
Human-readable (RESP text format)Slower recovery (must replay all commands)
Automatic rewrite compactionRewrite can spike CPU/memory
Granular fsync controlNot portable across Redis versions (sometimes)
Append-only = crash-safe writesCan grow very large without rewrite
Preserves command historyNo random access to specific keys

AOF Configuration:

# Enable AOF
appendonly yes

# Fsync strategy
appendfsync everysec # Recommended balance

# Auto-rewrite trigger
auto-aof-rewrite-percentage 100 # Rewrite when 100% larger
auto-aof-rewrite-min-size 64mb # Minimum size to trigger

# AOF file name
appendfilename "appendonly.aof"

# Use RDB preamble (hybrid mode)
aof-use-rdb-preamble yes # Store base snapshot as RDB + incremental AOF

Hybrid Persistence: RDB + AOF Together​

How They Work Together:

graph TD
Write[Write Command] --> Memory[Update Memory]
Memory --> AOF[Append to AOF]

Timer[Periodic Timer] --> RDB[Create RDB Snapshot]
RDB --> Disk1[dump.rdb]
AOF --> Disk2[appendonly.aof]

Memory -.Source of Truth.-> Both[Both derive from memory]

Restart Priority:

flowchart TD
Start[Redis Starts] --> Check{AOF enabled?}
Check -->|Yes| AOFExists{AOF file exists?}
Check -->|No| UseRDB[Load dump.rdb]

AOFExists -->|Yes| LoadAOF[Load AOF<br/>Higher priority]
AOFExists -->|No| LoadRDB[Load dump.rdb]

LoadAOF --> Validate[Validate data]
LoadRDB --> Validate
UseRDB --> Validate

Why AOF Has Priority?

AOF is more up-to-date (potentially has writes after last RDB snapshot)

Hybrid Persistence Strategy (Best Practice):

# Enable both
save 900 1
appendonly yes
appendfsync everysec

# Use RDB-format base in AOF rewrites
aof-use-rdb-preamble yes

Result:

  • RDB provides fast restart baseline
  • AOF provides durability since last snapshot
  • Hybrid mode = best of both worlds

File Format Evolution and Compatibility​

RDB Versions:

VersionRedis VersionKey Changes
11.0Initial format
52.6Millisecond expiry
62.8LZF compression improvements
73.2Quick list encoding
95.0Stream data type, 64-bit lengths

Compatibility:

  • Redis can read older RDB formats
  • Redis cannot read newer RDB formats
  • Always test before downgrading

AOF Format:

  • RESP2 (traditional)
  • RESP3 (Redis 6+, backward compatible)
  • Multi-part AOF (Redis 7+)

Performance Characteristics​

RDB Performance:

graph LR
Fork[fork] -->|Instant| Child[Child Process]
Child -->|Sequential Write| Disk[Disk I/O]
Disk -->|~1-10 seconds| Complete[Complete]

Fork -.Copy-on-Write.-> Memory[Memory Overhead]

Typical RDB Save Time:

Dataset SizeApproximate TimeMemory Overhead
1 GB1-2 seconds< 50 MB (COW)
10 GB10-20 seconds< 500 MB (COW)
100 GB100-200 seconds< 5 GB (COW)

AOF Performance:

  • Write latency: +0.1-1 ms (depends on fsync strategy)
  • Rewrite time: Similar to RDB (background fork)
  • Recovery time: Slower than RDB (replay all commands)

Comparison:

Recovery Time:
RDB: 10 GB → ~30 seconds (sequential read + hash table rebuild)
AOF: 10 GB → ~5 minutes (parse + execute millions of commands)

Durability:
RDB: Last snapshot (potentially minutes/hours old)
AOF: Last fsync (potentially 0-1 seconds old)

Internal Storage and I/O Optimization​

Linux Kernel Optimizations Redis Uses:

  1. fork() + Copy-on-Write

    • Shared memory pages until modification
    • Minimal memory duplication
  2. mmap() for file I/O

    • Memory-mapped files for fast access
  3. fallocate() for AOF

    • Pre-allocate disk space for better performance
  4. fdatasync() vs fsync()

    • fdatasync() = faster, skips metadata updates

Write Amplification:

AOF Write Amplification = How many disk writes per logical write?

appendfsync always: 1 fsync per write = High durability, high cost
appendfsync everysec: 1 fsync per second = Good balance
appendfsync no: OS buffering = Low cost, low durability

Trade-offs Summary​

RDB:

✅ Fast restart
✅ Compact file
✅ No runtime overhead
✅ Perfect for backups

❌ Data loss risk (between snapshots)
❌ Memory spike during fork
❌ Long save time for huge datasets
❌ No incremental updates

AOF:

✅ High durability (per-write)
✅ Human-readable
✅ Continuous updates
✅ Automatic compaction (rewrite)

❌ Larger files
❌ Slower recovery
❌ Slight write latency overhead
❌ Rewrite can be expensive

Hybrid (Recommended):

✅ Best of both worlds
✅ Fast recovery (RDB base)
✅ Minimal data loss (AOF incremental)

⚠️ Slightly more complex configuration
⚠️ Uses more disk space

Key Takeaways​

  1. RDB = Binary snapshot, NO indexes, sequential I/O optimized
  2. AOF = Command log, NO indexes, append-only optimized
  3. Both use fork() to avoid blocking main thread
  4. Neither has random access capability - they're recovery mechanisms, not databases
  5. Hybrid mode (RDB + AOF) is the recommended production setup
  6. File formats evolve with Redis versions - test compatibility when upgrading

Mental Model:

┌─────────────────────────────────────────────┐
│ In-Memory Data Structures │
│ (Source of Truth) │
└─────────┬─────────────────────┬─────────────┘
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ RDB │ │ AOF │
│ Snapshot │ │ Command │
│ File │ │ Log │
└──────────┘ └──────────┘

No Indexes No Indexes
Sequential I/O Append-Only
Fast Recovery High Durability

Replication and High Availability​

Master–Replica Model​

Replication means copying data from one Redis to others.

graph TD
Master --> Replica1
Master --> Replica2
  • Writes go to Master
  • Reads can go to Replicas
  • Replicas are eventually consistent

Why Replicas Matter:

  • Scale reads
  • Protect against data loss
  • Enable failover

How Master–Replica Works Internally​

Key correction:

❗ Master is NOT always the only interface to the application

That's a design choice, not a Redis rule.

Logical model:

graph TD
App --> Master
Master --> Replica1
Master --> Replica2
  • Writes → Master
  • Reads → Master or Replicas (optional)

How data flows:

There is no separate sync daemon.

Replication is done by:

  • Redis master
  • Redis replica
  • Using a replication protocol

Replication phases:

Phase 1: Full sync (initial)

sequenceDiagram
Replica->>Master: SYNC
Master->>Replica: RDB snapshot
Master->>Replica: buffered writes

Phase 2: Continuous replication

sequenceDiagram
Client->>Master: Write
Master->>Replica: Propagate command
  • Replicas replay commands
  • Replication is asynchronous

Can applications talk to replicas?

Yes — but carefully.

OperationSafe on Replica?
Reads✅
Writes❌
Strong consistency❌

Master and Replica Relationship:

  • Master and replica are the same Redis server binary
  • Same persistence mechanisms (RDB/AOF)
  • Same data model
  • Same networking behavior
  • Roles are logical, not physical

Replacement happens via failover, not magically.

graph LR
Master --> Replica
Replica -->|Failover| NewMaster

Sentinel (High Availability Brain)​

Redis Sentinel watches Redis instances.

What Sentinel Does:

  • Monitors Redis health
  • Detects failures
  • Promotes replicas
  • Reconfigures clients

Sentinel Architecture:

graph TD
Sentinel1 --> Master
Sentinel2 --> Master
Sentinel3 --> Master
Master --> Replica

Sentinels vote to avoid false failover.

Why 3 Sentinels?

Quorum and fault tolerance

Sentinel is NOT Redis:

  • Sentinel is a separate process
  • It monitors Redis nodes
  • It talks to other Sentinels

Why multiple Sentinels?

If only 1 Sentinel:

  • Network glitch = false failover

If 2 Sentinels:

  • Split brain possible

If 3 Sentinels:

  • Majority vote works

Odd number rule:

graph TD
Sentinel1 --> Vote
Sentinel2 --> Vote
Sentinel3 --> Vote

Majority = 2 out of 3 Failover only happens if quorum is reached

Sentinel failure tolerance:

SentinelsCan lose
31
52

Automatic Failover Flow:

sequenceDiagram
Sentinel->>Master: Ping
Master--x Sentinel: No response
Sentinel->>Replica: Promote
Replica->>Sentinel: I am master

Redis Cluster and Sharding​

Understanding Nodes, Instances, and Masters​

Terminology:

TermMeaning
NodeA machine / VM / container
Redis instanceOne Redis process
MasterA Redis instance that owns slots
ReplicaA Redis instance replicating a master

Important:

🔴 One Redis instance = one role (master OR replica) 🔴 One node can host multiple Redis instances (rare in prod)

Correct architecture:

graph TD
Node1 --> MasterA
Node2 --> ReplicaA1
Node3 --> ReplicaA2

Best practice:

One Redis instance per node for:

  • Simpler failure domain
  • Clear CPU / memory isolation
  • Predictable persistence behavior

What Is Redis Cluster?​

When a single Redis node is not enough, you use Redis Cluster.

What Is Sharding?

Splitting data across multiple Redis nodes.

graph TD
App --> Slot1[Redis Node A]
App --> Slot2[Redis Node B]
App --> Slot3[Redis Node C]

Correct Redis Cluster Layout:

A Redis Cluster has multiple masters, each responsible for a subset of slots

graph TD
Master1 --> Replica1
Master2 --> Replica2
Master3 --> Replica3

Each master:

  • Owns a different shard (slot range)
  • Has 0+ replicas

Why multiple masters are required:

ReasonExplanation
ScalabilityOne master cannot hold entire keyspace
ThroughputWrites distributed across masters
AvailabilityFailure isolates only part of data

Understanding Sharding​

Correct definition:

Sharding partitions the KEYSPACE, not snapshots

Sharding:

  • Happens at runtime
  • Happens at key routing time
  • Is independent of persistence

Correct sharding model:

graph TD
Keyspace --> Slot0-5000 --> MasterA
Keyspace --> Slot5001-10000 --> MasterB
Keyspace --> Slot10001-16383 --> MasterC

Replica relationship:

graph TD
MasterA --> ReplicaA1
MasterB --> ReplicaB1

❗ Replicas do NOT participate in sharding ❗ Replicas mirror exactly one master's shard

Snapshot relationship:

graph TD
MasterA --> RDB_A
MasterB --> RDB_B
ReplicaA1 --> RDB_A_replica

📌 Each instance snapshots only its own shard

Important Clarifications:

  • Sharding = logical keyspace partitioning concept
  • Realized by assigning slot ranges to Redis master instances
  • NOT a file, NOT a database, NOT a persistence mechanism

Shard Definition:

Shard = Master + its assigned slots (+ replicas)

One master owns one or more slot ranges, which together form a shard.

A master can own multiple non-contiguous slot ranges, but conceptually they are treated as one shard.

graph TD
MasterA --> Slot1_100
MasterA --> Slot300_500

Redis Cluster Architecture​

Critical Understanding:

Redis has NO "cluster main controller" Redis Cluster is fully decentralized

Every Redis node:

  • Knows the slot map
  • Can redirect clients
  • Acts independently

What Redis Cluster Solves:

ProblemSolved?
Horizontal scaling✅
Node failure✅
Cross-slot transactions❌ (limited)

Big Picture:

graph TD
App --> RedisCluster
RedisCluster --> Master1
RedisCluster --> Master2
Master1 --> Replica1
Master2 --> Replica2
Sentinel --> RedisCluster
RedisCluster --> Disk

Hash Slots Deep Dive​

Why Redis Has Hash Slots (Not Just Hash % N)​

Before Redis Cluster (pre-3.0), people did:

node = hash(key) % number_of_nodes

Problems with that approach:

  • Adding/removing a node reshuffles almost all keys
  • Massive cache invalidation
  • Impossible to do online resharding

Redis Cluster Solution: Hash Slots

Redis introduces an indirection layer:

Key → Hash → Slot → Master Node
flowchart LR
Key --> Hash
Hash --> Slot[0..16383]
Slot --> Master

Fixed number of slots:

  • 16384 slots (0–16383)
  • Slots never change
  • Only slot → node mapping changes

This is the foundation of Redis Cluster scalability.

Exact Hash / Slot Algorithm​

Step 1: Extract hash tag (if any)

key = user:{42}:profile
hash_input = 42

If {} exists → only content inside is hashed If not → entire key is hashed

Step 2: CRC16

slot = CRC16(hash_input) % 16384

Why CRC16?

  • Fast
  • Good distribution
  • Deterministic
  • Same across all clients

Step 3: Slot → Master lookup

flowchart LR
Slot -->|Slot table| MasterA

Important:

🔹 Only the key string matters for slot calculation 🔹 The value is irrelevant for routing

Slot Ranges and Assignment​

Is the Redis slot just an integer calculated by CRC16?

✅ Yes — in modern Redis Cluster:

slot = CRC16(key) % 16384
  • Slot range is fixed: 0 – 16383
  • This has never changed since Redis Cluster was introduced
  • Slots are integers only, not ranges, not objects

Important:

A key always hashes to the same slot forever. Slot migration only changes which node owns that slot, not how the slot is computed.

Example: 3-Master Cluster

Total slots = 16384

MasterSlots
Master A0 – 5460
Master B5461 – 10922
Master C10923 – 16383

This is:

  • Balanced
  • Explicit
  • Deterministic

📌 These ranges are not required to be contiguous.

Can slots in the same shard be non-contiguous?

✅ Yes — absolutely allowed

Redis Cluster rule:

A master can own any set of slots, contiguous or not

Example:

graph TD
Slot1_100 --> MasterA
Slot201_300 --> MasterA
Slot101_200 --> MasterB

✔ Redis does not require continuity ✔ Continuity is usually chosen for operational simplicity

Why Redis allows non-contiguous slots:

  • Online resharding
  • Gradual migration
  • Failure recovery
  • Load balancing

Why operators prefer contiguous slots:

  • Easier to reason about
  • Faster debugging
  • Cleaner monitoring

Slot Assignment​

Who assigns and manages slot ranges?

❌ Not applications ❌ Not hashing logic ❌ Not modulo arithmetic

✅ Redis Cluster itself (with admin tools)

Slot assignment happens via:

  • redis-cli --cluster create
  • redis-cli --cluster reshard
  • Redis operators / orchestration systems
  • Cloud Redis services

How slot assignment works internally:

Each master maintains:

slots[0..16383] → owned / not owned

Cluster-wide metadata looks like:

flowchart TB
SlotMap["Cluster Slot Map"]

SlotMap -->|0-5460| MasterA
SlotMap -->|5461-10922| MasterB
SlotMap -->|10923-16383| MasterC

Clients cache this map.

Where Is Slot Ownership Stored?

Each Redis node keeps:

  • A full copy of the cluster topology
  • Slot → node mapping
  • Node health state

Stored in:

nodes.conf

This file is:

  • Managed automatically
  • Updated by Redis itself
  • Never edited manually

Slot Migration​

Are Slots Physical Things?

❌ No — slots are NOT physical objects

A slot is:

  • A logical label (0–16383)
  • Used only for routing and ownership
  • Stored as cluster metadata in each Redis node

There is:

  • ❌ No slot file
  • ❌ No slot directory
  • ❌ No slot process
  • ❌ No slot memory region

What is physical?

PhysicalDescription
Redis instanceOS process
MemoryActual key-value storage
KeysStored in RAM
RDB / AOFPersistence files
TCP connectionsClient & cluster bus

Slots are just numbers used to decide which keys belong where.

What Does "Slot Migration" Actually Mean?

Slot migration is a two-part operation:

1. Move ownership of a slot
2. Move keys belonging to that slot

Step-by-Step Slot Migration:

Let's migrate slot 8000 from Master A → Master B

Step 1: Mark slot as "migrating" and "importing"

sequenceDiagram
participant A as Master A
participant B as Master B
participant C as Clients

A->>A: slot 8000 = MIGRATING to B
B->>B: slot 8000 = IMPORTING from A
  • Slot still logically exists
  • Both nodes know migration is in progress

Step 2: Move keys belonging to the slot

flowchart LR
Key1 -->|DUMP| A
A -->|RESTORE| B

Redis internally:

  • Scans keys in slot 8000 on A
  • Serializes each key
  • Sends to B
  • Deletes from A

This is key-level movement, not memory blocks.

What actually moves:

KEY + VALUE + TTL + TYPE

Internally via:

MIGRATE host port key db timeout

Step 3: Finalize slot ownership

sequenceDiagram
A->>A: remove slot 8000
B->>B: own slot 8000
A->>Cluster: update slot map
B->>Cluster: update slot map

Now:

slot 8000 → Master B

Important Corrections:

  • Slots are never transmitted
  • Only key-value pairs move
  • Slot metadata is updated separately

There is no structure like:

{
"slot": 5793,
"data": {...}
}

Who Coordinates Slot Migration?

❌ Not Sentinel ❌ Not a central controller ❌ Not ZooKeeper ❌ Not etcd

✅ Redis nodes coordinate themselves

Redis Cluster is fully decentralized.

Coordination happens via:

  • Cluster bus (TCP port: client_port + 10000)
  • Gossip protocol
  • Cluster state machine inside each Redis process

Who initiates migration?

Usually:

  • redis-cli --cluster reshard
  • Or orchestration tools (K8s operator, Ansible, etc.)

But once initiated: ➡ Redis nodes do everything themselves

How Do Clients Survive During Migration?

This is where ASK / MOVED comes in.

During migration:

sequenceDiagram
Client->>MasterA: GET key_in_8000
MasterA-->>Client: ASK MasterB
Client->>MasterB: ASKING + GET key
  • Temporary redirect
  • Client retries once
  • No downtime

After migration:

sequenceDiagram
Client->>AnyNode: GET key
Node-->>Client: MOVED slot 8000 MasterB
  • Permanent redirect
  • Client updates slot cache

Hash Tags: Forcing Co-Location​

Problem:

Multi-key operations must be on same slot

MGET user:1 user:2 ❌ (likely different slots)

Solution: Hash tags

user:{42}:name
user:{42}:email

Both hash on 42

flowchart LR
user42name --> SlotX
user42email --> SlotX
SlotX --> MasterA

✔ Multi-key ops work ✔ Transactions work ✔ Lua scripts work

Hash Tag Rule:

Only substring inside { } is hashed

Best practice:

  • Use hash tags sparingly
  • Avoid hotspotting
  • Never hash on low-cardinality values (e.g. {US})

Can You Customize Hash / Slot Logic?​

What you CANNOT customize:

ItemCustomizable
Number of slots❌ Fixed at 16384
Hash algorithm❌ CRC16 is fixed
Slot computation❌ Fixed
Slot redirection behavior❌ Fixed

Redis must behave identically across all clients.

What you CAN influence:

1. Key design

This is your only real control

user:{id}:*
order:{id}:*
session:{id}

2. Slot distribution

Via:

  • CLUSTER ADDSLOTS
  • CLUSTER DELSLOTS
  • redis-cli --cluster reshard

3. Read routing

  • Read from master only
  • Or allow replica reads (client-level)

Complete Data Flow Example​

Example: Writing and Reading Data

Data: {user: "whereq"}

Write Flow:

  1. Application stores:

    SET user:whereq '{"user":"whereq"}'
  2. Redis sees:

    KEY = "user:whereq"
    VALUE = '{"user":"whereq"}'
  3. Slot calculation:

    slot = CRC16("user:whereq") % 16384 = 5793
  4. Cluster metadata contains:

    MasterA owns slots: 4096–8191 (example)
    → slot 5793 ∈ MasterA
  5. Data stored in MasterA's RAM (not directly in RDB)

Read Flow:

  1. Application sends:

    GET user:whereq
  2. Client hashes:

    CRC16("user:whereq") → slot 5793
  3. Client (or Redis redirect) routes to MasterA

  4. MasterA returns:

    {"user":"whereq"}

After Migration:

If slot 5793 migrates to MasterB:

  1. MasterA sends entire key-value pair to MasterB
  2. Cluster metadata updated: slot 5793 now belongs to MasterB
  3. Application reads: same hash → slot 5793 → now routes to MasterB
  4. Application receives data transparently

Key Principle:

Slots route data; nodes store data


Operational Considerations​

Adding a New Master to the Cluster​

What happens when you add a new master node?

Step 1: New master joins the cluster

redis-cli --cluster add-node NewMaster ExistingMaster

Result:

  • New master joins with ZERO slots
  • It holds no data
  • It serves no traffic
flowchart LR
A[Master A<br/>Slots 0–5460]
B[Master B<br/>Slots 5461–10922]
C[Master C<br/>Slots 10923–16383]
D[New Master D<br/>Slots: none]

A --- B --- C --- D

📌 Redis will never auto-assign slots. This is intentional.

Why Redis does NOT auto-rebalance slots:

Auto-rebalance would be dangerous:

RiskExplanation
Massive data movementMillions/billions of keys
Latency spikesMIGRATE blocks CPU
Unpredictable IODisk + network storms
App impactCascading failures

Redis follows the principle:

Data movement must be explicit, observable, and reversible

Step 2: Resharding (manual or scripted)

You explicitly run:

redis-cli --cluster reshard

Or specify:

redis-cli --cluster reshard \
--cluster-from <old-master-id> \
--cluster-to <new-master-id> \
--cluster-slots 4096

This:

  • Moves slots, not files
  • Migrates key/value pairs
  • Updates cluster metadata
  • Happens online

Example: 3 Masters → 4 Masters

Before:

MasterSlots
A0–5460
B5461–10922
C10923–16383

Target distribution (example):

MasterSlots
A0–4095
B4096–8191
C8192–12287
D12288–16383

During migration:

sequenceDiagram
participant Client
participant A as MasterA
participant D as MasterD

Client->>A: GET key(slot=13000)
A-->>Client: ASK 13000 D
Client->>D: GET key
D-->>Client: value

Redis uses ASK redirects during migration.

What happens to RDB/AOF after migration?

Persistence follows memory ownership:

  1. Key moves from A → D (RAM)
  2. Slot ownership updated
  3. Next RDB snapshot:
    • A's RDB ❌ no longer has the key
    • D's RDB ✅ contains the key

No syncing between RDBs ever happens.

Adding replicas:

You normally add replicas AFTER slot resharding:

redis-cli --cluster add-node ReplicaOfD NewMasterD --cluster-slave

Replicas automatically sync data.

Does application need to change anything?

❌ No changes required

Clients:

  • Cache slot → master map
  • Receive MOVED / ASK
  • Refresh metadata automatically

This is why slots exist.

Memory Management​

Redis masters must keep data in RAM:

✅ Yes — Redis is fundamentally an in-memory database

Redis guarantees:

  • All active data lives in RAM
  • Persistence is for recovery only

What if data exceeds RAM?

Redis gives you explicit strategies, not silent paging.

Option 1: Eviction (most common)

maxmemory 32gb
maxmemory-policy allkeys-lru

Policies include:

  • allkeys-lru
  • volatile-lru
  • allkeys-random
  • noeviction (write fails)

📌 Redis will never swap to disk automatically.

Option 2: Redis Cluster (scale horizontally)

Instead of bigger RAM:

Add masters
Reshard slots
Distribute memory

Option 3: Redis on Flash (specialized)

Enterprise / Redis Stack only:

  • SSD-backed
  • Still RAM-first
  • Different architecture

Best Practices​

Slot layout:

  • Prefer contiguous slot ranges
  • Evenly distribute across masters

Key naming:

  • Predictable
  • Stable
  • Avoid random prefixes

Bad:

abc123:user:1
xyz999:user:2

Good:

user:1
user:2

Hash tags:

Use only when required:

✔ Sessions ✔ User-scoped objects ❌ Global counters ❌ Time series

Avoid hotspots:

Bad:

counter:{global}

Better:

counter:{region}:{id}

When Adding Masters:

✔ Add master first (empty) ✔ Reshard gradually ✔ Move slots from multiple masters ✔ Monitor latency during migration ❌ Don't auto-rebalance ❌ Don't migrate too many slots at once


Mental Model Summary​

Final Mental Model (The One to Keep)​

KEY --(CRC16 % 16384)--> SLOT --(cluster map)--> MASTER --> REPLICAS
  • Slot ≠ shard
  • Slot ≠ data
  • Slot = routing label
  • Shard = ownership of many slots

Complete Architecture​

KEY
↓
CRC16 hash
↓
SLOT (0–16383)
↓
Cluster Metadata
↓
Owning Master
↓
RAM (source of truth)
↓
RDB / AOF (recovery only)

Key Truths​

TopicCore Truth
RDBDisk snapshot of memory
AOFWrite command log
Both togetherMemory is source of truth
ReplicationMaster streams commands
SentinelExternal HA coordinator
ShardingData split by slots
SlotsLogical routing layer

One-Sentence Summary​

Redis Cluster shards the keyspace using hash slots, assigns slots to masters, replicates data to replicas, and persists each instance independently using RDB/AOF — all without a central controller.

Redis Mental Checklist (Hero Level)​

When designing with Redis, always ask:

  1. Is this hot data?
  2. Do I need atomic operations?
  3. What happens if Redis restarts?
  4. Can I tolerate eventual consistency?
  5. Do I need sharding or just replicas?

Final Thought​

Redis is powerful because it is opinionated.

If you:

  • Respect its strengths
  • Understand its limits
  • Design intentionally

Redis becomes one of the most reliable and elegant tools in distributed systems.


Quick Reference Guide​

Common Redis Patterns​

1. Cache Pattern:

Application → Check Redis → Hit? Return : Fetch from DB → Store in Redis → Return

2. Counter Pattern:

INCR page_views
INCRBY user:123:credits 50

3. Distributed Lock:

SET lock:resource "token" NX PX 30000 # Atomic lock with 30s TTL

4. Rate Limiting:

INCR rate:user:123:20260108 # Count requests per day
EXPIRE rate:user:123:20260108 86400

5. Session Store:

SETEX session:abc123 3600 '{"user_id":123,"role":"admin"}'

Common Commands Cheat Sheet​

OperationCommandExample
Set valueSET key value [EX seconds]SET user:1 "Alice" EX 3600
Get valueGET keyGET user:1
IncrementINCR keyINCR counter
Hash setHSET key field valueHSET user:1 name "Alice"
Hash getHGET key fieldHGET user:1 name
List pushLPUSH key valueLPUSH queue:jobs task1
List popRPOP keyRPOP queue:jobs
Set addSADD key memberSADD tags:post:1 "redis"
Sorted set addZADD key score memberZADD leaderboard 100 "player1"
Check existenceEXISTS keyEXISTS user:1
DeleteDEL keyDEL user:1
Set expiryEXPIRE key secondsEXPIRE session:abc 3600
Get TTLTTL keyTTL session:abc

Configuration Quick Reference​

Persistence:

# RDB Snapshots
save 900 1 # Save if 1+ keys changed in 900s
save 300 10 # Save if 10+ keys changed in 300s
save 60 10000 # Save if 10000+ keys changed in 60s
rdbcompression yes
rdbchecksum yes

# AOF
appendonly yes
appendfsync everysec # Options: always | everysec | no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-use-rdb-preamble yes # Hybrid mode

# Hybrid (Recommended)
save 900 1
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes

Memory Management:

maxmemory 2gb
maxmemory-policy allkeys-lru # Options:
# allkeys-lru - Evict any key, LRU
# volatile-lru - Evict keys with TTL, LRU
# allkeys-random - Evict any key, random
# volatile-random - Evict keys with TTL, random
# volatile-ttl - Evict keys with shortest TTL
# noeviction - Return errors when memory limit reached

Replication:

# On replica
replicaof <master-ip> <master-port>
replica-read-only yes

Cluster:

cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000

Troubleshooting Guide​

ProblemPossible CausesSolutions
Slow queriesLarge keys, blocking commandsUse SCAN instead of KEYS, avoid SMEMBERS on large sets
Memory spikesRDB fork during saveIncrease memory, schedule saves during low traffic
Data lossNo persistence enabledEnable AOF with appendfsync everysec
Connection timeoutsNetwork issues, maxclients limitCheck maxclients, network latency
Replication lagMaster overloaded, slow networkOptimize write load, check network bandwidth
Cluster MOVED errorsSlot migration in progressNormal during resharding, wait or retry
OOM errorsMemory limit reachedCheck maxmemory-policy, add more nodes

Performance Tips​

DO:

  • ✅ Use pipelining for bulk operations
  • ✅ Use Redis transactions (MULTI/EXEC) for atomicity
  • ✅ Set appropriate TTLs to expire unused data
  • ✅ Use hash tags for multi-key operations in cluster
  • ✅ Monitor with INFO command and Redis monitoring tools
  • ✅ Use connection pooling in applications
  • ✅ Batch operations when possible

DON'T:

  • ❌ Use KEYS * in production (use SCAN instead)
  • ❌ Store huge values (> 100KB) in single keys
  • ❌ Use Redis as primary database without persistence
  • ❌ Run blocking commands (BLPOP with long timeout) without care
  • ❌ Ignore memory limits and eviction policies
  • ❌ Use transactions across multiple hash slots in cluster
  • ❌ Store sensitive data without encryption layer

Redis Versions Features​

VersionKey Features
3.0Redis Cluster introduced
3.2GEO commands, Quick List
4.0Modules system, lazy freeing
5.0Streams data type, new sorted set commands
6.0SSL/TLS, ACLs, RESP3, threaded I/O
6.2Tracking, client-side caching
7.0Functions, sharded pub/sub, ACL improvements, Multi-part AOF

Monitoring Metrics​

Key Metrics to Watch:

# Memory usage
INFO memory
- used_memory
- used_memory_rss
- mem_fragmentation_ratio

# Performance
INFO stats
- instantaneous_ops_per_sec
- total_commands_processed
- keyspace_hits / keyspace_misses (hit ratio)

# Persistence
INFO persistence
- rdb_last_save_time
- rdb_changes_since_last_save
- aof_current_size

# Replication
INFO replication
- role (master/replica)
- connected_slaves
- master_repl_offset

# Cluster
CLUSTER INFO
- cluster_state
- cluster_slots_assigned
- cluster_slots_ok

Resource Planning​

Sizing Guidelines:

Use CaseRecommended SetupExample
Small cacheSingle master1-4 GB RAM
Production cacheMaster + 1-2 replicas + Sentinel8-32 GB RAM per node
Large dataset3+ master cluster, 1 replica each32-256 GB RAM per node
High availabilitySentinel (3+ instances) or ClusterOdd number of sentinels

Network Requirements:

  • Client ↔ Redis: Low latency (< 1ms ideal)
  • Cluster nodes: Low latency (< 1ms), high bandwidth
  • Replication: Sustained bandwidth for writes

Key Corrections and Clarifications​

Slots and Data Storage​

Important:

  • Slots are just labels
  • Nodes own labels
  • Keys follow labels
  • Redis nodes gossip label ownership
  • Shard = naming convenience

Correct Understanding:

  • Slot is a pure logic term
  • Slot represents the keyspace
  • Slot is basically in Redis metadata
  • Slot is a logic label
  • Data is NOT stored "in slots"
  • Data is stored in Master's memory
  • Slot routes request to the correct master

Migration and Persistence​

Important:

  • Slots are NOT stored in RDB
  • Slots are NOT persisted with data
  • Slots live in cluster metadata
RDB = raw key/value memory dump
Cluster metadata = routing truth

Summary Answers:

QuestionCorrect Answer
Snapshot after migration?❌ No
Temporary RDB inconsistency?✅ Yes, expected
Crash before snapshot safe?✅ Yes
Slot logic during RDB load?Metadata-driven
Hash during load?Only for validation
Data always in RAM?✅ Yes
Exceed RAM?Evict or scale

Application Interaction​

Does the application care which master or replica it talks to?

✅ Conceptually correct

Writes:

  • Application does not choose the master
  • Client library computes hash slot
  • Client routes to correct master
  • If wrong node → MOVED redirect

✔ Application logic remains unaware

Reads:

  • Reads go to masters by default
  • Guarantees strong consistency
  • Clients may read from replicas
  • Requires explicit client configuration

⚠️ Replica reads may be eventually consistent

Adding a New Master​

When a new master is added later, Redis does NOT automatically rebalance slots. Slot redistribution (resharding) is an explicit, controlled operation.

Key Principle:

Adding a master does NOT change hashing. It only changes slot ownership — explicitly.

KEY → SLOT (fixed) → MASTER (mutable)

Deep Dive into Redis II

· 阅读需 5 分钟

1. Big Picture: What Redis Actually Is​

At its core, Redis is:

An in-memory data store with optional persistence and optional clustering (sharding + replication)

Everything else is layered on top of this idea.


2. Node, Master, Replica — The Physical Roles​

Node​

A node is a single Redis process:

  • One Redis server
  • One TCP port
  • One memory space
  • One persistence configuration (RDB/AOF)
[ Redis Node ]
├── RAM
├── RDB file (optional)
├── AOF file (optional)
└── Network endpoint

Master​

A master node:

  • Accepts writes
  • Owns slots (in cluster mode)
  • Replicates data to replicas
[ Master Node ]
├── Slot ownership
├── Handles writes
├── RAM = source of truth
└── Replication stream

Replica (Slave)​

A replica:

  • Receives data from a master
  • Is read-only
  • Can be promoted if master fails
Master ---> Replica
RAM RAM
RDB RDB

⚠️ Replicas do not own slots


3. Snapshot vs RDB vs AOF (Critical Distinction)​

Snapshot (Concept)​

A snapshot is a conceptual idea:

“Redis memory state at a specific moment in time”

It is not a file.


RDB (Redis Database File)​

RDB is the physical result of a snapshot

Time T
┌───────────────┐
│ Redis RAM │ ---- SNAPSHOT ----> dump.rdb
└───────────────┘

Key points:

  • Full memory dump

  • Binary file

  • Point-in-time

  • Created via:

    • SAVE
    • BGSAVE
    • Auto rules (e.g., every N seconds if M writes)

AOF (Append Only File)​

AOF records operations, not memory state.

SET a 1
INCR a
INCR a
DEL b

Key points:

  • Append-only
  • Grows over time
  • Can be rewritten (AOF rewrite)
  • Replays commands to rebuild RAM

RDB vs AOF (Mental Model)​

RDB: "What memory looks like"
AOF: "How memory was built"

4. Does RDB File Size Change?​

YES — absolutely

RDB characteristics:​

  • Each dump is independent

  • File size can:

    • Increase
    • Decrease
  • Depends on:

    • Key count
    • Value sizes
    • Data types
    • Compression efficiency
RDB at T1: 120 MB
RDB at T2: 95 MB (keys deleted / smaller values)

❌ There is NO incremental RDB writing

Each RDB is:

A full rewrite, from scratch


AOF File Size Behavior​

  • Always appends
  • Grows monotonically
  • Periodically compacted by rewrite

5. Shards vs Sharding vs Slots​

Sharding (Concept)​

Sharding is a logical data partitioning strategy

“Split the total keyspace across multiple masters”

Sharding itself:

  • Is NOT a file
  • Is NOT persistence
  • Is NOT memory layout

Shard (In Redis Cluster)​

A shard usually means:

One master + its replicas owning a subset of hash slots

Shard 1: MasterA + Replicas
Shard 2: MasterB + Replicas
Shard 3: MasterC + Replicas

6. Slots — The Core of Redis Cluster​

Slot Basics​

Redis Cluster defines:

  • 16384 hash slots
  • Slots are integers: 0 → 16383

Every key maps to exactly one slot


Slot Calculation​

In modern Redis:

slot = CRC16(key) % 16384

Optionally using hash tags:

{user:123}:name
{user:123}:email

→ Same slot


Slots Are LOGICAL​

Slots:

  • Are NOT stored in RDB

  • Are NOT stored in AOF

  • Exist only in:

    • Cluster metadata
    • Node configuration
    • Client routing logic

7. Slot → Master Mapping (Routing)​

Key: user:whereq
|
CRC16
|
Slot 5793
|
Cluster Metadata
|
MasterA
Client
|
|-- key --> slot
|
|-- slot --> master

Clients either:

  • Cache slot tables
  • Or ask Redis (MOVED / ASK)

8. How Slot Ranges Are Assigned​

Initial Cluster Creation​

Slots are manually assigned (by admin tools):

3 Masters
16384 / 3 ≈ 5461 slots each

MasterA: 0 - 5460
MasterB: 5461 - 10922
MasterC: 10923 - 16383

⚠️ Redis does NOT do:

slot % number_of_masters

That would break consistency.


9. Adding a New Master (Re-sharding)​

When a new master joins:

  1. Admin decides how many slots to move
  2. Slots are migrated
  3. Keys are moved key-by-key
Before:
MasterA: 0-5460
MasterB: 5461-10922
MasterC: 10923-16383

After adding MasterD:
MasterA: 0-4095
MasterB: 4096-8191
MasterC: 8192-12287
MasterD: 12288-16383

10. Slot Migration — What Really Moves?​

Key Truth​

Slot migration moves KEY-VALUE PAIRS in RAM NOT RDB files


Migration Flow​

Slot 5793 moves from A → B

MasterA RAM:
user:whereq -> {"user":"whereq"}

MasterB RAM:
(empty)
1) Cluster metadata updated (MIGRATING / IMPORTING)
2) Key-value pairs copied
3) Ownership finalized
MasterA --> send key + value --> MasterB

Slot Structure Clarification​

❌ Slots do NOT store values like:

5793 -> {...}

✅ Actual storage:

"user:whereq" -> '{"user":"whereq"}'

Slot is only used for routing


11. RDB + Slot Migration Interaction​

Scenario Timeline​

Step 1: RDB Snapshot​

MasterA RAM:
user:whereq

dump.rdb:
user:whereq

✔ Correct


Step 2: Slot Migration Happens​

MasterA --> sends key+value --> MasterB

Now:

MasterA RAM: (no user:whereq)
MasterB RAM: user:whereq

Step 3: No Immediate Snapshot​

Redis does NOT auto-trigger RDB after migration.

This is OK because:

RDB is not authoritative for cluster topology


Crash Safety​

If MasterA crashes:

  1. It reloads RDB

  2. It rejoins cluster

  3. Cluster metadata says:

    • Slot 5793 belongs to MasterB
  4. MasterA:

    • Deletes or ignores that key
    • Syncs from correct owner if replica

✔ No double ownership


12. Does Redis Re-hash During RDB Load?​

YES — but conceptually.

When loading RDB:

  • Redis loads keys
  • Slot is recalculated
  • If slot not owned → key discarded or redirected
RDB load
|
|-- key --> slot
|-- slot --> ownership check

13. Routing Summary Diagram​

Client
|
|-- key
|-- CRC16
|-- slot
|
+--> Master (via cluster map)

14. Memory Is Always the Source of Truth​

Redis Fundamental Rule​

All Redis data must fit in RAM

Persistence is:

  • Recovery mechanism
  • Replication mechanism
  • NOT primary storage

What If Data Exceeds RAM?​

Redis will:

  • Fail writes
  • Or evict keys (if configured)

Eviction policies:

noeviction
allkeys-lru
volatile-lru
allkeys-random
...

Redis is NOT a disk-first database.


15. Final Concept Relationship Map​

┌────────────┐
│ Client │
└─────┬──────┘
│
Routing
│
┌──────▼───────┐
│ Hash Slot │ (CRC16 % 16384)
└──────┬───────┘
│
┌────────▼────────┐
│ Master Node │
│ (RAM) │
└──────┬─────────┘
│
┌────────▼────────┐
│ Replica Nodes │
└────────────────┘

Persistence:
RAM --snapshot--> RDB
RAM --commands--> AOF

16. One-Sentence Mental Models​

  • Snapshot → a moment in memory
  • RDB → binary memory dump
  • AOF → command history
  • Slot → routing number
  • Shard → slot-owning master group
  • Sharding → data partition strategy
  • Master → writes + slot owner
  • Replica → copy + failover
  • Node → Redis process
  • Routing → key → slot → master

Deep Dive into Redis III

· 阅读需 4 分钟

Great question — cluster metadata is the last missing puzzle piece that makes everything you learned click. I’ll explain what it is, where it lives, what’s inside it, how it’s updated, and who trusts it, again with diagrams and zero code.


Redis Cluster Metadata — Deep Dive

1. What Is Redis Cluster Metadata?​

Redis cluster metadata is the shared logical map that tells every node:

“Who owns which slots, who is master/replica of whom, and how to route keys.”

It is:

  • NOT user data
  • NOT stored in RDB
  • NOT stored in AOF
  • NOT centralized

It is distributed and eventually consistent.


2. What Information Is in Cluster Metadata?​

Each node stores the same logical structure (with slight timing differences):

Core Contents​

Cluster State:
- cluster_id
- epoch (version)
- nodes:
- node_id
- ip:port
- role (master / replica)
- master_of (if replica)
- slots owned (if master)
- flags (fail, pfail, handshake, etc.)

Conceptual Structure (Simplified)​

CLUSTER METADATA
│
├── Cluster ID
├── Epoch (version)
│
├── Node A
│ ├── id: A1
│ ├── role: master
│ ├── slots: 0-5460
│ └── replicas: B1, B2
│
├── Node B
│ ├── id: C1
│ ├── role: master
│ ├── slots: 5461-10922
│
└── Node C
├── id: D1
├── role: master
├── slots: 10923-16383

3. Where Is Cluster Metadata Stored?​

On Every Node — Locally​

Each Redis node stores cluster metadata in:

nodes.conf

Important properties:

  • Written automatically by Redis
  • Updated dynamically
  • Not meant for manual editing
  • Survives restart
Redis Node
├── RAM
├── dump.rdb
├── appendonly.aof
└── nodes.conf <-- cluster metadata

Key Point​

nodes.conf is NOT a configuration file you manage

It is Redis’s internal cluster ledger


4. nodes.conf — What Does It Look Like Conceptually?​

Conceptually (not exact syntax):

node A1 10.0.0.1:6379 master - 0-5460
node B1 10.0.0.2:6379 slave A1
node C1 10.0.0.3:6379 master - 5461-10922
node D1 10.0.0.4:6379 master - 10923-16383

Each node has:

  • A unique node ID
  • Role
  • Slot ownership (masters only)
  • Relationship to others

5. How Cluster Metadata Is Distributed​

Gossip Protocol​

Redis Cluster uses a gossip-style protocol:

Node A <-- gossip --> Node B <-- gossip --> Node C

Metadata exchange happens:

  • Periodically
  • Piggybacked on heartbeat packets
  • Incrementally

This ensures:

  • No central coordinator
  • No single source of truth
  • High availability

6. Cluster Epoch — Versioning the Metadata​

Why Epoch Exists​

When multiple changes happen:

  • Failover
  • Slot migration
  • Master promotion

Redis needs to decide:

“Which view of the cluster is newer?”

Epoch Rule​

Higher epoch = newer truth

Every metadata-changing event:

  • Increments epoch
  • Spreads via gossip

7. Slot Ownership in Metadata​

Slots are represented as ranges, not individual numbers.

Node A:
slots: 0-4095, 8192-12287

This allows:

  • Non-contiguous ownership
  • Partial migration

During Migration (Transitional States)​

Node A:
MIGRATING slot 5793 -> Node B

Node B:
IMPORTING slot 5793 <- Node A

Clients may see:

  • ASK
  • MOVED

8. Who Updates Cluster Metadata?​

Different Scenarios​

EventWho TriggersHow
Cluster creationAdmin toolAssign slots
Add masterAdmin toolRebalance slots
Slot migrationSource & target mastersSlot states
Master failureRedis nodesVoting + epoch
Replica promotionRedis nodesAutomatic

Important Clarification​

Redis does NOT have a leader node for metadata

All nodes:

  • Observe
  • Vote
  • Converge

9. Client’s View of Cluster Metadata​

Clients:

  • Cache slot → node map
  • Learn from Redis replies
Client --> Node
<-- MOVED 5793 10.0.0.2:6379

Client updates local cache.


Client Metadata vs Server Metadata​

Client cache = hint
Cluster metadata = authority

10. Metadata Is Separate from Data​

This separation is intentional and powerful:

DATA PATH:
Key -> RAM -> RDB/AOF

CONTROL PATH:
Slot -> Metadata -> Routing

This is why:

  • Slot migration doesn’t move files
  • RDB doesn’t store slot info
  • Restarting nodes doesn’t break routing

11. Failure Scenario: Why Metadata Saves You​

Master Crash After Migration​

MasterA crashes
MasterB owns slot 5793

On restart:

  1. MasterA loads RDB
  2. Reads nodes.conf
  3. Sees slot 5793 not owned
  4. Discards or syncs properly

✔ No split-brain


12. Mental Model Diagram (Complete)​

┌────────────┐
│ Client │
└─────┬──────┘
│
Slot Calculation
│
┌──────▼───────┐
│ Slot (0-16383)│
└──────┬───────┘
│
┌─────────────▼─────────────┐
│ Cluster Metadata (nodes) │
│ - slot ranges │
│ - roles │
│ - epochs │
└─────────────┬─────────────┘
│
┌────────▼────────┐
│ Master Node │
│ RAM │
└────────┬────────┘
│
┌─────────▼─────────┐
│ Replica Nodes │
└───────────────────┘

13. One-Sentence Summary​

Redis cluster metadata is a distributed, versioned routing ledger that maps hash slots to master nodes, stored in nodes.conf, propagated by gossip, and completely independent from user data and persistence.

Deep Dive into Redis IV

· 阅读需 4 分钟

Slots are ONLY for routing, NOT for locating data. Redis finds data by key lookup in memory, not by slot.

We’ll clarify four different layers, because confusion usually comes from mixing them:

  1. Routing layer (Cluster / Slots)
  2. Process ownership layer (Master / Shard)
  3. In-memory data structures
  4. Persistence (RDB / AOF)

1. Slot ≠ Data Location​

❌ Common misunderstanding​

“Slot key tells Redis where the data is stored”

✅ Correct understanding​

Slot only tells Redis WHICH MASTER should own the key

It does NOT tell Redis WHERE inside memory the key lives


2. What a Slot Really Is​

A Redis slot is:

  • An integer label: 0 ~ 16383

  • Computed by:

    slot = CRC16(key) % 16384
  • Stored as metadata

  • Used for routing decisions only


Slot’s Responsibility​

KEY ──hash──▶ SLOT ──lookup──▶ MASTER NODE

That’s it. After that, slot is no longer involved.


3. What Happens After Routing​

Once the request reaches the correct master:

Client
↓
Correct Master
↓
Redis Process
↓
In-memory key lookup

At this point:

  • Redis does NOT use slots
  • Redis does NOT use RDB
  • Redis does NOT scan files

4. Redis Is an In-Memory Database First​

Redis always works like this:

WRITE:
Client → RAM → (later) RDB/AOF

READ:
Client → RAM

Persistence is secondary.


5. Core Data Structure: The Dictionary (Hash Table)​

Every Redis instance maintains a global in-memory dictionary:

redisDb
└── dict
├── "user:whereq" → value_ptr
├── "order:1001" → value_ptr
├── "counter:login" → value_ptr
└── ...

This dictionary:​

  • Is a hash table
  • Key lookup is O(1) average
  • Slot is NOT part of the key

Diagram: Inside a Redis Master​

Redis Master (Shard Owner)
┌─────────────────────────────┐
│ redisDb │
│ ┌─────────────────────────┐ │
│ │ dict (hash table) │ │
│ │ ┌──────────┐ │ │
│ │ │ key → ptr│───▶ Value │ │
│ │ └──────────┘ │ │
│ └─────────────────────────┘ │
└─────────────────────────────┘

6. Then What Is a “Shard”?​

In Redis terms:

A shard = one master + its replicas

It is not:

  • A separate data structure
  • A file
  • A memory region

It’s a deployment and responsibility concept.


Logical Shard View​

Shard A
├── Master A (owns slots 0–5460)
│ └── RAM: keys belonging to those slots
├── Replica A1
└── Replica A2

7. How Redis Ensures a Shard Only Contains Its Slots’ Data​

Important rule:

A master only stores keys whose slots it owns

This is enforced by:

  • Slot ownership metadata
  • Migration logic

During Normal Operation​

KEY "user:whereq"
↓
CRC16 → slot 5793
↓
Cluster metadata → Master A
↓
Stored in Master A’s dict

Master B never sees this key.


8. Slot Is NOT Stored With the Key​

Internally, Redis stores:

key = "user:whereq"
value = '{"user":"whereq"}'

NOT:

slot = 5793
key = "user:whereq"
value = ...

Slot is recomputed when needed, not stored.


9. What About Slot Migration?​

Migration Moves DATA, Not Metadata Only​

During migration:

Master A (slot 5793)
|
|-- key + value
v
Master B

After migration:

  • Master A deletes the key
  • Master B stores the key in its own dict
  • Metadata updates slot ownership

Migration Diagram​

Before:
Master A dict: "user:whereq"
Master B dict: (empty)

After:
Master A dict: (deleted)
Master B dict: "user:whereq"

Slot is just the rule that decides this move, not the container.


10. RDB Has No Index — Why That’s OK​

You are correct:

RDB has no index

That’s because:

  • RDB is never queried directly
  • Redis loads RDB into RAM at startup
  • After loading, Redis rebuilds the hash table

RDB Load Process​

Redis restart
↓
Read RDB sequentially
↓
For each key:
- allocate memory
- insert into dict
↓
Ready for traffic

Diagram: RDB Restore​

dump.rdb
├── key1 → value1
├── key2 → value2
└── key3 → value3
↓
redisDb.dict

11. What Happens If RDB Contains “Wrong” Keys?​

Example:

  • Slot migrated away
  • Old RDB still contains keys

On startup:

  1. Redis loads all keys

  2. Cluster metadata says:

    • “You do NOT own slot 5793”
  3. Redis:

    • Deletes or refuses those keys
    • Or resyncs from correct master

✔ Metadata always wins


12. Full Request Lifecycle (End-to-End)​

Client
↓
Hash key → slot
↓
Cluster metadata lookup
↓
Correct master
↓
Redis dict lookup
↓
Return value

End-to-End Diagram​

┌────────┐
│ Client │
└───┬────┘
│ CRC16(key)
▼
┌────────────┐
│ Slot (5793)│
└───┬────────┘
│ metadata
▼
┌────────────┐
│ Master A │
│ redisDb │
│ dict[key] │
└────────────┘

13. Key Takeaways (Very Important)​

✅ Correct Mental Model​

ConceptRole
SlotRouting label
ShardOwnership responsibility
MasterData holder
RAM dictActual data location
RDBCold backup
AOFChange log

🔥 Golden Rule​

Redis never locates data by slot. Redis locates data by key lookup in RAM. Slot only decides WHICH Redis instance is allowed to hold that key.


Deep Dive into Redis V: Magic Hash

· 阅读需 4 分钟
  1. What the Redis “magic hash” really is
  2. Why keys are not strings, but SDS pointers
  3. How Redis achieves O(1) lookup without slots
  4. How Redis dict works internally (buckets, hashing, collisions)
  5. Why this is faster than slot-based indexing
  6. What happens during rehashing (without stopping the world)

Redis “Magic Hash”: How Redis Finds Data in O(1)

Redis does NOT use slots to find data Redis does NOT scan memory Redis uses a very carefully designed hash table

This is the real magic.


1. First: Redis Keys Are NOT Strings​

When you do:

SET user:whereq {"user":"whereq"}

Internally:

  • "user:whereq" is stored as an SDS
  • SDS = Simple Dynamic String
  • Redis stores a pointer to SDS, not a raw string

Internal Representation​

key (char*) ──▶ SDS struct
├── len
├── alloc
└── buf[] = "user:whereq"

So when Redis “hashes the key”, it hashes:

the bytes in SDS.buf, not a language string


2. Redis Dict: The Core Data Structure​

Every Redis instance has:

redisDb
└── dict

This dict is a hash table, implemented as:

dict
├── ht[0] (current table)
├── ht[1] (rehashing table)
├── rehashidx

Diagram: Redis Dict Layout​

dict
├── ht[0]
│ ├── bucket[0] → entry → entry → NULL
│ ├── bucket[1] → NULL
│ ├── bucket[2] → entry → NULL
│ └── ...
├── ht[1] (used only during rehash)
└── rehashidx

3. The “Magic Hash” Function​

Redis uses SipHash (modern Redis) for dict keys.

Why SipHash?​

ReasonBenefit
FastO(1) average
SecurePrevents hash-flooding attacks
StableGood distribution
Byte-basedWorks on SDS.buf

Hashing Flow​

SDS.buf ("user:whereq")
↓
SipHash(key_bytes, secret_seed)
↓
64-bit hash value

This hash value is not the Redis slot hash.


4. From Hash → Bucket Index​

Redis converts the hash to a bucket index:

index = hash & (table_size - 1)

Why this works:

  • Table size is always power of 2
  • Bitwise AND is faster than modulo

Example​

hash = 0xA92F4C19
table_size = 1024

index = hash & 1023

⚡ One CPU instruction


5. O(1) Lookup Path (Real Path)​

When Redis reads a key:

1. Hash SDS.buf (SipHash)
2. Compute bucket index
3. Follow pointer
4. Compare keys (rare collisions)
5. Return value

Diagram: Lookup Path​

GET user:whereq
↓
SipHash(SDS.buf)
↓
bucket index
↓
bucket pointer
↓
dictEntry
↓
value pointer

6. Collision Handling (Still O(1))​

Redis uses separate chaining:

bucket[i] → entry → entry → NULL

Why collisions stay rare:

  • Good hash function
  • Resize before load factor is high

Load Factor Rules​

ConditionAction
load factor ≥ 1expand
load factor ≤ 0.1shrink

This keeps chains short.


7. Incremental Rehashing (No Stop-the-World)​

When resizing is needed:

  • Redis allocates ht[1]
  • Gradually moves buckets from ht[0] → ht[1]
  • Each command migrates a few buckets

Rehashing Diagram​

Before:
ht[0] → buckets
ht[1] → empty

During:
ht[0] → some buckets
ht[1] → some buckets
rehashidx → moving pointer

After:
ht[0] empty
ht[1] becomes new ht[0]

Reads check both tables during rehash.


8. Why Slots Are NOT Used for Lookup​

Now your key insight:

“Why not use slot to locate data?”

Because:

SlotDict
16K fixedDynamic size
Routing onlyData access
Cluster-levelInstance-level
CoarsePrecise

Slot would require secondary index. Redis avoids this entirely.


9. Why This Is Faster Than Slot-Based Lookup​

If Redis used slots to locate data:

slot → table → bucket → key

Current Redis:

hash(key) → bucket → key

Fewer indirections = faster.


10. Slot vs Dict Hash (Very Important)​

PurposeHash Used
Cluster routingCRC16
Key lookupSipHash

They are completely independent.


11. Full Mental Model (Correct)​

Client
↓
CRC16(key) → slot → master
↓
SipHash(key) → bucket → dictEntry
↓
value

Full Stack Diagram​

┌────────┐
│ Client │
└───┬────┘
│
▼
CRC16(key) ──▶ Slot ──▶ Master Node
│
▼
SipHash(key)
│
▼
Hash Table Bucket
│
▼
dictEntry → Value

12. Why Redis Can Promise O(1)​

Redis guarantees:

O(1) average time complexity

Because:

  • Good hash
  • Controlled load factor
  • Incremental rehash
  • In-memory pointers

Worst-case exists (like any hash table), but is practically avoided.


13. Final Takeaway (Key Insight)​

Slots route traffic. Dict hashes locate data. RDB/AOF persist state. None of them replace the others.

Redis is fast because each layer does only one job — and does it extremely well.


Redis INCR: From Atomic Counter to Distributed System Primitive-ChatGPT

· 阅读需 5 分钟

1. What Is INCR — Really?​

At the surface:

INCR key increments the integer value stored at key by 1.

But conceptually:

INCR is Redis’s atomic, single-threaded, in-memory state transition primitive.

It is not just a command — it is a contract:

  • Atomic
  • Linearizable (within a shard)
  • Fast (O(1))
  • Durable only through persistence configuration

2. The Core Property: Atomicity by Design​

Redis achieves atomicity without locks.

Why?​

Redis executes commands:

  • In a single-threaded event loop
  • One command at a time per master

Mental model​

sequenceDiagram
participant C1 as Client A
participant C2 as Client B
participant R as Redis Master

C1->>R: INCR counter
R->>R: counter = counter + 1
R-->>C1: 101

C2->>R: INCR counter
R->>R: counter = counter + 1
R-->>C2: 102

No race conditions. No CAS. No mutexes.

📌 Atomicity is a consequence of Redis architecture, not a feature bolted on.


3. INCR at the Memory Level​

Redis stores values as encoded objects.

Counter lifecycle​

flowchart LR
Key["counter"]
Obj["Redis Object<br/>(int-encoded)"]
RAM["Master RAM"]

Key --> Obj --> RAM

Key properties:

  • Stored as integer encoding, not string
  • Conversion happens automatically if needed
  • O(1) mutation

4. What Happens on First INCR?​

If the key does not exist:

Redis implicitly creates it with value 0, then increments.

flowchart LR
NoKey["Key not exist"]
Create["Create key = 0"]
Inc["Increment to 1"]

NoKey --> Create --> Inc

This is critical for idempotent system design.


5. INCR vs INCRBY vs DECR​

All share the same atomic model:

INCR → +1
INCRBY → +N
DECR → -1

They differ only in delta, not semantics.


6. INCR in a Redis Cluster (Slots Matter)​

Slot routing​

flowchart LR
App["Application"]
Hash["CRC16(key) % 16384"]
Slot["Slot 5793"]
Master["Owning Master"]

App --> Hash --> Slot --> Master

Important constraint​

All operations on a key must hit the same master.

That’s why:

  • Counters scale by key
  • Not by total throughput automatically

7. One Counter = One Slot = One Master​

flowchart LR
Counter["page:view:home"]
Slot["Slot 10234"]
MasterA["Master A"]

Counter --> Slot --> MasterA

This implies:

  • A single hot counter can become a bottleneck
  • Sharding requires key design, not Redis magic

8. Real-World Use Case #1: Page View Counters​

Naive design​

INCR page:home

Problem​

  • All traffic hits one master
  • Hotspot risk

Better design (sharded counters)​

flowchart LR
App -->|Random shard| C1["page:home:1"]
App -->|Random shard| C2["page:home:2"]
App -->|Random shard| C3["page:home:3"]

C1 --> M1
C2 --> M2
C3 --> M3

Later:

  • Aggregate offline or periodically

📌 INCR is atomic, aggregation is eventual.


9. Real-World Use Case #2: Distributed Rate Limiting​

Conceptual flow​

flowchart LR
Request --> Key["user:123:rate:minute"]
Key --> INCR
INCR --> Check["<= limit?"]
Check -->|Yes| Allow
Check -->|No| Reject

Why INCR is perfect here​

  • Atomic increment
  • No locks
  • TTL-based expiration

10. TTL + INCR = Time-Window Counters​

flowchart LR
Counter["login:fail:user123"]
INCR --> Value
TTL["Expire in 60s"]

Counter --> INCR --> TTL

Key idea:

  • Redis counter lifecycle = window lifecycle

11. Real-World Use Case #3: ID Generation​

Monotonic IDs (per shard)​

flowchart LR
INCR --> ID["Order ID"]
ID --> Business

⚠️ Limitations:

  • Not globally monotonic in cluster
  • Not gap-free (crashes, rollbacks)

12. Persistence and INCR​

With RDB​

flowchart LR
RAM["Counter = 105"]
Snapshot["RDB Snapshot"]
Crash["Crash"]
Restore["Reload RDB (maybe 103)"]

RAM --> Snapshot --> Crash --> Restore

📌 Counters may roll back to last snapshot.


With AOF​

flowchart LR
INCR1 --> AOF
INCR2 --> AOF
INCR3 --> AOF
Crash --> Replay --> CorrectValue

AOF provides:

  • Stronger durability
  • Higher IO cost

13. Replication Semantics of INCR​

sequenceDiagram
participant App
participant Master
participant Replica

App->>Master: INCR counter
Master->>Replica: INCR counter

Replicas:

  • Replay commands
  • Do NOT compute independently

14. Failover and Counters​

What happens during failover?​

flowchart LR
MasterA --> ReplicaB
Crash --> Promotion
ReplicaB --> NewMaster

Possible effects:

  • Lost increments (depending on replication lag)
  • Duplicate increments (rare, edge cases)

📌 Redis counters are at-least-once durable, not exactly-once.


15. INCR Is NOT a Transaction Log​

Do NOT use Redis counters when you need:

  • Auditable correctness
  • Exactly-once guarantees
  • Financial-grade accuracy

Use:

  • Databases
  • Event logs
  • Kafka-style append-only systems

16. Best Practices Summary​

Design best practices​

PrincipleWhy
Shard hot countersAvoid single-master bottleneck
Combine INCR + TTLNatural windowing
Accept eventual aggregationScale horizontally
Use AOF for critical countersBetter durability
Avoid cross-slot countersNot atomic

Anti-patterns​

❌ Global counters without sharding ❌ Using INCR for money ❌ Assuming strong durability by default ❌ Expecting ordering across masters


17. Mental Model to Keep Forever​

INCR =
Atomic
In-memory
Single-shard
Best-effort durable
Extremely fast

Or visually:

flowchart TB
INCR --> Atomic
INCR --> Fast
INCR --> RAM
INCR --> ShardBound
INCR --> EventuallyDurable

18. Final Thought​

Redis INCR is not just a counter. It is a building block for distributed coordination — as long as you respect its boundaries.

If you want next, I can:

  • Compare INCR vs Kafka counters
  • Design a 10M QPS rate limiter
  • Explain Lua + INCR atomic workflows
  • Dive into Redis Time Series vs INCR

Just tell me where you want to go next.

Redis INCR: From Atomic Counter to Distributed System Primitive-DeepSeek

· 阅读需 15 分钟

Executive Summary​

This document provides a comprehensive explanation of Redis' core architectural concepts and their interrelationships. We'll explore persistence mechanisms, data distribution strategies, and high-availability patterns through detailed conceptual diagrams and clear explanations.

1. Persistence Layer: RDB vs AOF​

1.1 RDB (Redis Database File) - The Snapshot​

RDB is a point-in-time binary snapshot of Redis' entire in-memory dataset at a specific moment.

graph TB
subgraph "RDB Snapshot Creation Process"
A[Redis Main Process] --> B["Fork() Child Process<br/>Copy-on-Write"]
B --> C["Child Process: Reads Memory State"]
C --> D["Writes Binary RDB File"]
D --> E["temp-12345.rdb → dump.rdb<br/>(Atomic Rename)"]

F["Parent Process"] --> G["Continues Serving Clients"]
G --> H["Updates Memory (Copy-on-Write)"]
end

subgraph "RDB File Characteristics"
I["Complete Dataset<br/>At Snapshot Time"] --> J["Binary Format<br/>Compact, Efficient"]
K["Self-contained<br/>No External Dependencies"] --> L["Variable Size<br/>Matches Dataset Size"]
end

Key RDB Insights:

  1. Complete, not incremental: Each RDB contains the full dataset at snapshot time
  2. Variable file size: Yes, RDB size varies with dataset size - it can increase OR decrease
  3. No incremental writing: Each RDB is written from scratch
  4. Copy-on-Write mechanism: Forked child process reads while parent continues operations

RDB Size Dynamics:

Scenario 1: Dataset grows → RDB size increases
Scenario 2: Dataset shrinks → RDB size decreases
Scenario 3: Data changes but same size → RDB similar size
Scenario 4: Different compression/encoding → RDB size varies

1.2 AOF (Append Only File) - The Command Log​

AOF is an append-only log of every write command received by Redis.

graph LR
subgraph "AOF Write Pattern"
A[Write Command] --> B[Execute in Memory]
B --> C[Append to AOF Buffer]
C --> D{Sync Strategy}

D --> E["appendfsync always<br/>Every command (Safe)"]
D --> F["appendfsync everysec<br/>Every second (Balanced)"]
D --> G["appendfsync no<br/>OS decides (Fast)"]

E --> H[AOF File]
F --> H
G --> H
end

subgraph "AOF Characteristics"
I["Append-only<br/>Commands accumulate"] --> J["Human-readable (mostly)<br/>Redis commands"]
K["Can grow large<br/>Requires rewriting"] --> L["Incremental by nature<br/>Only adds, never overwrites"]
end

subgraph "AOF Rewrite Process"
M[AOF File Grows] --> N["BGREWRITEAOF triggered"]
N --> O["Fork child process"]
O --> P["Create new compact AOF<br/>From current memory state"]
P --> Q["Atomic switch to new AOF"]
end

AOF vs RDB Comparison:

quadrantChart
title RDB vs AOF: Trade-off Analysis
x-axis "Durability/Safety" --> "Performance/Speed"
y-axis "Recovery Speed" --> "Storage Efficiency"

quadrant-1 "AOF fsync=always: Maximum Safety"
quadrant-2 "RDB: Fast Recovery, Less Safe"
quadrant-3 "No Persistence: Max Performance"
quadrant-4 "AOF fsync=everysec: Good Balance"

"RDB (hourly)": [0.4, 0.8]
"RDB (5 min)": [0.5, 0.7]
"AOF (everysec)": [0.7, 0.6]
"AOF (always)": [0.9, 0.3]
"RDB + AOF": [0.8, 0.5]

1.3 Snapshot: The Concept vs Implementation​

Important distinction: Snapshot is the concept, RDB is the implementation.

graph TD
subgraph "Snapshot Concept"
A["Point-in-time Capture<br/>of System State"] --> B["Represents Complete State<br/>at Specific Moment"]
B --> C["Use Cases: Backup,<br/>Recovery, Migration"]
end

subgraph "RDB Implementation"
D["Binary File Format<br/>Serialized Memory State"] --> E["Implementation of<br/>Snapshot Concept"]
E --> F["Physical Manifestation<br/>of Logical Snapshot"]
end

A --> G["Conceptual<br/>(What)"]
D --> H["Implementation<br/>(How)"]

I["Memory State<br/>at Time T"] --> J["RDB Creation Process"] --> K["RDB File<br/>(Snapshot at Time T)"]

Snapshot Lifecycle:

1. Memory state exists (runtime)
2. Snapshot triggered (admin or schedule)
3. RDB file created (implementation)
4. RDB persists snapshot (storage)
5. RDB used for recovery (restoration)

2. Data Distribution: Sharding & Slots​

2.1 Shards/Sharding - Horizontal Partitioning​

Sharding is the practice of splitting data across multiple Redis instances to scale beyond single-node limits.

graph TB
subgraph "Sharding Architecture"
A["Complete Dataset<br/>(Too large for one node)"] --> B{Sharding Strategy}

B --> C["Hash-based Sharding<br/>CRC16(key) % N"]
B --> D["Range-based Sharding<br/>Key ranges per node"]
B --> E["Directory-based Sharding<br/>Lookup service"]

C --> F["Redis Cluster Approach<br/>16384 hash slots"]

subgraph "Physical Distribution"
G[Shard 1<br/>Master + Replicas] --> H["~33% of data<br/>Independent failure domain"]
I[Shard 2<br/>Master + Replicas] --> J["~33% of data<br/>Independent failure domain"]
K[Shard 3<br/>Master + Replicas] --> L["~33% of data<br/>Independent failure domain"]
end
end

subgraph "Shard Characteristics"
M["Independent Redis Instance"] --> N["Own persistence (RDB/AOF)"]
O["Own memory space"] --> P["Can fail independently"]
Q["Horizontal scalability<br/>Add more shards as needed"]
end

2.2 Slots - The Logical Partitioning Layer​

Slots are logical partitions (0-16383) that provide an indirection layer between keys and physical nodes.

graph LR
subgraph "Slot Routing Architecture"
A["Key: user:12345"] --> B["CRC16('user:12345')<br/>= 948293"]
B --> C["Slot = 948293 % 16384<br/>= 5793"]
C --> D["Slot Map Lookup"]
D --> E["Slot 5793 → Master Node A"]
E --> F["Request Routed to Correct Node"]
end

subgraph "Fixed Slot Count Advantage"
G["Always 16384 slots<br/>Never changes"] --> H["Stable key→slot mapping<br/>No rehashing on scale"]
I["Only slot→node mapping changes"] --> J["Minimal data movement<br/>during scaling"]
end

subgraph "Slot Distribution Example"
K["Master Node 1<br/>Slots: 0-5460"] --> L["~33% of slots"]
M["Master Node 2<br/>Slots: 5461-10922"] --> N["~33% of slots"]
O["Master Node 3<br/>Slots: 10923-16383"] --> P["~33% of slots"]
end

Why 16384 Slots?

  • Enough for distribution (more than typical node count)
  • Small enough for efficient metadata management
  • Binary-friendly number (2^14)
  • Fits in 16KB with 1-bit per slot in cluster messages

2.3 Relationship: Shards vs Slots​

graph TD
subgraph "Logical Layer"
A["Slots (0-16383)<br/>Logical partitions<br/>Routing labels"]
end

subgraph "Physical Layer"
B["Shards<br/>Physical Redis instances<br/>Master + replicas"]
end

subgraph "Mapping Relationship"
C["Slot Ranges → Shard Assignment"] --> D["Many-to-one mapping<br/>Multiple slots per shard"]
E["Dynamic Mapping<br/>Can change without affecting keys"] --> F["Enables online resharding<br/>Slot migration between shards"]
end

A --> G["Routing Logic:<br/>Key → Slot → Shard"]
B --> H["Data Storage:<br/>Shard holds actual key-value data"]

I["Key Space Partitioning"] --> J["Slots = How to partition"]
I --> K["Shards = Where to store partitions"]

3. Node Roles & Replication​

3.1 Node - The Fundamental Unit​

A node is a single Redis server process running on physical/virtual hardware.

graph TB
subgraph "Node Composition"
A["Redis Server Process"] --> B["Memory Space<br/>(Data storage)"]
A --> C["Network Endpoint<br/>(Port binding)"]
A --> D["Persistence Files<br/>(RDB/AOF)"]
A --> E["Configuration<br/>(Runtime settings)"]
end

subgraph "Node Types in Context"
F["Standalone Node<br/>Single instance, no replication"] --> G["Development, testing<br/>Simple use cases"]

H["Master Node<br/>Accepts writes<br/>Owns slot ranges"] --> I["Part of replication group<br/>Primary data source"]

J["Replica Node<br/>Read-only copy<br/>Syncs from master"] --> K["High availability<br/>Read scaling<br/>Backup source"]

L["Sentinel Node<br/>Monitoring/coordination<br/>No data storage"] --> M["Failure detection<br/>Automatic failover"]
end

3.2 Master & Replica - The Replication Pair​

Master-replica architecture provides data redundancy and read scalability.

graph TB
subgraph "Master Node"
A["Accepts writes<br/>(Primary)"]
B["Owns data authoritatively"]
C["Propagates changes to replicas"]
D["Can have multiple replicas"]
end

subgraph "Replica Node"
E["Read-only<br/>(Secondary)"]
F["Syncs from single master"]
G["Exact data copy<br/>(Eventually consistent)"]
H["Can be promoted to master"]
end

subgraph "Replication Flow"
I[Client Write] --> J[Master Execution]
J --> K[Replication Buffer]
K --> L[Async Propagation]
L --> M[Replica Application]

N["Initial Sync: RDB + Buffered Commands"]
O["Continuous: Command Streaming"]
end

subgraph "Failover Scenario"
P[Master Failure] --> Q[Replica Promotion]
Q --> R[New Master]
R --> S[Other Replicas Reconfigure]
end

A -->|Replication| E

3.3 Replication Topologies​

graph TB
subgraph "Basic Star Topology"
A[Master] --> B[Replica 1]
A --> C[Replica 2]
A --> D[Replica 3]

E["Simple setup<br/>Master handles all replication<br/>Network load on master"]
end

subgraph "Chain Replication"
F[Master] --> G[Replica 1]
G --> H[Replica 2]
H --> I[Replica 3]

J["Reduces master load<br/>Higher latency for distant replicas<br/>Single point in chain fails propagation"]
end

subgraph "Tree Topology"
K[Master] --> L[Replica A]
K --> M[Replica B]

L --> N[Replica A1]
L --> O[Replica A2]
M --> P[Replica B1]

Q["Balances load<br/>Complex setup<br/>Good for large deployments"]
end

4. Redis Cluster: Integrated Architecture​

4.1 Complete Cluster Architecture​

graph TB
subgraph "Redis Cluster Components"
A["Master Nodes (3+)<br/>Own slot ranges<br/>Accept writes"]

B["Replica Nodes (Optional)<br/>Copy master data<br/>Read scaling + HA"]

C["Hash Slots (16384)<br/>Logical partitions<br/>Key routing"]

D["Cluster Bus<br/>Gossip protocol<br/>Node communication"]

E["Clients<br/>Cluster-aware<br/>Slot caching"]
end

subgraph "Data Flow"
F[Client Request] --> G{Key Hash → Slot}
G --> H[Slot → Master Mapping]
H --> I[Route to Correct Master]
I --> J[Master Processes]
J --> K[Async to Replicas]
end

subgraph "Failure Handling"
L[Node Failure] --> M[Replica Promotion]
N[Slot Reassignment] --> O[Client Redirection]
end

A --> B
C --> A
D --> A
D --> B
E --> C

4.2 Routing in Redis Cluster​

Smart client routing with automatic redirection:

sequenceDiagram
participant C as Client
participant N1 as Random Node
participant M1 as Correct Master
participant M2 as New Master (after migration)

Note over C,N1: Initial Request (No Slot Cache)
C->>N1: SET user:123 "data"
N1->>N1: CRC16("user:123") → Slot 5793
N1->>N1: Slot 5793 not owned by me
N1-->>C: -MOVED 5793 M1_IP:M1_PORT
C->>C: Update slot cache: 5793 → M1

Note over C,M1: Direct Routing (With Cache)
C->>M1: SET user:123 "data"
M1-->>C: OK

Note over M1,M2: Slot Migration Occurs
M1->>M2: Migrate slot 5793 + keys

Note over C,M1: Post-Migration Request
C->>M1: GET user:123
M1-->>C: -MOVED 5793 M2_IP:M2_PORT
C->>C: Update cache: 5793 → M2
C->>M2: GET user:123
M2-->>C: "data"

Routing Components:

  1. Slot caching: Clients remember slot→node mappings
  2. MOVED response: Permanent redirect (update cache)
  3. ASK response: Temporary redirect during migration
  4. Cluster slots command: Full mapping retrieval

5. Concept Relationships & Data Flow​

5.1 Complete Data Lifecycle​

graph TD
subgraph "Write Path"
A[Client Write] --> B[Key Hashing]
B --> C[Slot Determination]
C --> D[Master Routing]
D --> E[Memory Update]
E --> F[Replication Propagation]
F --> G[Replica Memory Update]
E --> H[AOF Append]
end

subgraph "Persistence Path"
I[Memory State] --> J{Scheduled/Manual Trigger}
J --> K[RDB Snapshot]
H --> L[AOF Continuous Log]
end

subgraph "Read Path"
M[Client Read] --> N[Slot Determination]
N --> O[Master/Replica Routing]
O --> P[Memory Read]
P --> Q[Response]
end

subgraph "Recovery Path"
R[Node Restart] --> S{Recovery Source}
S --> T["AOF (if exists)"]
S --> U["RDB (AOF not available)"]
T --> V[Replay Commands]
U --> W[Load Snapshot]
V --> X[Memory Restored]
W --> X
end

subgraph "Scaling Path"
Y[Add New Node] --> Z[Reshard Slots]
Z --> AA[Migrate Key-Value Pairs]
AA --> BB[Update Cluster Metadata]
end

5.2 Concept Hierarchy & Dependencies​

graph TD
subgraph "Foundation Layer"
A["Node<br/>Basic Redis instance"]
B["Memory<br/>Runtime data storage"]
end

subgraph "Persistence Layer"
C["RDB<br/>Snapshot implementation"]
D["AOF<br/>Command log"]
E["Snapshot<br/>Point-in-time concept"]
end

subgraph "Replication Layer"
F["Master<br/>Write authority"]
G["Replica<br/>Read copy"]
H["Replication<br/>Data sync protocol"]
end

subgraph "Distribution Layer"
I["Sharding<br/>Horizontal partitioning"]
J["Slots<br/>Logical partitions (0-16383)"]
K["Cluster<br/>Coordinated node group"]
end

subgraph "Routing Layer"
L["Hash Function<br/>CRC16 key hashing"]
M["Slot Map<br/>Slot→Node mapping"]
N["Client Routing<br/>Request direction"]
end

A --> B
B --> C
B --> D
C --> E

A --> F
A --> G
F --> H
G --> H

I --> J
J --> K
A --> K

L --> J
J --> M
M --> N

6. Advanced Persistence Insights​

6.1 RDB Size Dynamics Explained​

RDB file size varies based on multiple factors:

graph TB
subgraph "RDB Size Determinants"
A["Dataset Size"] --> B["Primary factor<br/>Larger data = larger RDB"]

C["Data Composition"] --> D["Strings vs Hashes vs Sets<br/>Different encoding efficiencies"]

E["Compression"] --> F["rdbcompression yes/no<br/>Trade-off: size vs CPU"]

G["Redis Version"] --> H["Encoding improvements<br/>Memory optimizations"]
end

subgraph "RDB Size Scenarios"
I["Growing Application"] --> J["RDB size increases over time<br/>Expected pattern"]

K["Data Archival"] --> L["Old data expires/deleted<br/>RDB size decreases"]

M["Data Transformation"] --> N["Compact encoding<br/>Same data, smaller RDB"]

O["Compression Change"] --> P["Enable/disable compression<br/>Size changes without data change"]
end

subgraph "Monitoring Considerations"
Q["Size Trends"] --> R["Monitor growth rate<br/>Predict storage needs"]

S["Size vs Memory"] --> T["RDB typically smaller than RAM<br/>Due to encoding optimizations"]

U["Sudden Changes"] --> V["Investigate data/model changes<br/>May indicate issues"]
end

RDB Size Characteristics:

  1. Not monotonic: Can increase or decrease between snapshots
  2. Not incremental: Each RDB is complete, written from scratch
  3. Compression optional: rdbcompression setting affects size
  4. Encoding dependent: Redis optimizes storage differently than runtime memory

6.2 AOF Growth & Rewriting​

AOF append-only nature leads to continuous growth:

graph LR
subgraph "AOF Lifecycle"
A["Empty AOF<br/>Startup"] --> B["Commands Append<br/>Linear growth"]
B --> C["AOF Rewrite Triggered<br/>Size/ratio threshold"]
C --> D["BGREWRITEAOF<br/>Fork child process"]
D --> E["New Compact AOF<br/>Current memory state"]
E --> F["Atomic Switch<br/>New AOF active"]
F --> G["Old AOF Deleted<br/>Cleanup"]
end

subgraph "AOF Rewrite Triggers"
H["auto-aof-rewrite-percentage 100"] --> I["AOF is 100% larger than last rewrite"]
J["auto-aof-rewrite-min-size 64mb"] --> K["AOF must be at least 64MB"]
L["Manual Command<br/>BGREWRITEAOF"] --> M["Administrative control"]
end

subgraph "AOF vs RDB Size Pattern"
N["RDB: Variable<br/>Snapshot-based"] --> O["Can shrink<br/>Reflects current dataset"]
P["AOF: Monotonic<br/>Until rewrite"] --> Q["Only grows<br/>Accumulates commands"]
R["After Rewrite: Reset<br/>Compact representation"] --> S["Size drops dramatically"]
end

AOF Rewrite Process:

  1. Fork child process (copy-on-write)
  2. Child writes new AOF from current memory state
  3. Parent continues serving and buffering new commands
  4. Atomic switch when child completes
  5. Parent appends buffered commands to new AOF

7. Operational Considerations​

7.1 Choosing Between RDB and AOF​

graph TD
subgraph "Decision Framework"
A["Persistence Requirements"] --> B{What matters most?}

B --> C["Recovery Speed"] --> D["✅ RDB preferred<br/>Faster restart"]
B --> E["Data Safety"] --> F["✅ AOF preferred<br/>Less data loss risk"]
B --> G["Storage Efficiency"] --> H["✅ RDB preferred<br/>More compact"]

I["Production Recommendation"] --> J["✅ Both RDB + AOF<br/>Best of both worlds"]
end

subgraph "Use Case Patterns"
K["Session Store"] --> L["✅ RDB only<br/>Acceptable data loss"]
M["Financial Transactions"] --> N["✅ AOF fsync=always<br/>Maximum durability"]
O["Cache Layer"] --> P["✅ RDB periodic<br/>Fast recovery acceptable"]
Q["Primary Database"] --> R["✅ RDB + AOF everysec<br/>Balanced approach"]
end

subgraph "Configuration Guidelines"
S["RDB Settings"] --> T["save 900 1<br/>save 300 10<br/>save 60 10000"]
U["AOF Settings"] --> V["appendonly yes<br/>appendfsync everysec<br/>auto-aof-rewrite-percentage 100"]
end

7.2 Scaling Strategy Matrix​

graph TB
subgraph "When to Use What"
A["Data Size"] --> B{How much data?}
B -->|"< 10GB"| C["Single Node<br/>Master + Replicas"]
B -->|"10GB - 100GB"| D["Redis Cluster<br/>Multiple masters"]
B -->|"> 100GB"| E["Large Cluster<br/>Many shards + planning"]

F["Throughput Needs"] --> G{Operations/sec?}
G -->|"< 50K"| H["Single Master<br/>With replicas for reads"]
G -->|"50K - 200K"| I["Redis Cluster<br/>Distributed writes"]
G -->|"> 200K"| J["Optimized Cluster<br/>Pipeline + connection pooling"]

K["Availability Requirements"] --> L{Uptime SLA?}
L -->|"< 99.9%"| M["Master + 1 Replica<br/>Manual failover"]
L -->|"99.9% - 99.99%"| N["Sentinel<br/>Automatic failover"]
L -->|"> 99.99%"| O["Multi-Region Cluster<br/>Geo-redundancy"]
end

8. Summary: Unified Architecture View​

8.1 The Complete Picture​

graph TB
subgraph "Physical Layer"
A["Nodes<br/>Redis processes on hardware"]
B["Memory<br/>RAM storage per node"]
C["Disk<br/>RDB/AOF persistence"]
end

subgraph "Logical Layer"
D["Slots<br/>16384 logical partitions"]
E["Shards<br/>Masters with slot ranges"]
F["Replication Groups<br/>Master + replicas"]
end

subgraph "Coordination Layer"
G["Cluster Bus<br/>Node gossip, metadata"]
H["Client Routing<br/>Slot caching, redirects"]
I["Sentinel<br/>Failure detection (optional)"]
end

subgraph "Data Flow"
J["Write Path:<br/>Client → Slot → Master → Memory → Replicas → AOF"]
K["Snapshot Path:<br/>Memory → RDB (periodic)"]
L["Recovery Path:<br/>AOF/RDB → Memory"]
M["Scaling Path:<br/>Add nodes → Reshard slots → Migrate data"]
end

A --> B
B --> C
D --> E
E --> F
G --> A
G --> E
H --> D
J --> B
K --> C
L --> B
M --> A
M --> D

8.2 Key Principles​

  1. Separation of concerns: Slots (logical) vs Shards (physical) vs Nodes (instances)
  2. Indirection enables scaling: Fixed slots allow dynamic node assignment
  3. Memory as source of truth: Persistence is for recovery, not runtime
  4. Decentralized coordination: No single point of failure in cluster
  5. Explicit over automatic: Manual resharding prevents uncontrolled data movement

8.3 RDB vs AOF: Final Comparison​

AspectRDB (Snapshot)AOF (Append Log)
PatternPoint-in-time complete dumpContinuous command accumulation
Size BehaviorVariable - matches datasetMonotonic growth until rewrite
Write PatternComplete rewrite each timeAppend-only, incremental
RecoveryFast load of snapshotSlow replay of command history
Data SafetyPotential loss since last snapshotConfigurable (everysec/always)
Storage EfficiencyCompact, encoded formatVerbose, command-based

Production Recommendation: Enable both with appendfsync everysec for optimal balance of performance, safety, and recovery speed.

Discussion: Redis as a Distributed Counter System

· 阅读需 14 分钟

Executive Summary​

This document evaluates Redis as a distributed counter solution, examining its architectural advantages, limitations, and trade-offs in modern systems.

Core Value Proposition: Why Consider Redis for Counters?​

Key Strengths at a Glance​

flowchart TD
A[Redis as Distributed Counter] --> B[Performance Advantages]
A --> C[Data Structure Richness]
A --> D[Operational Simplicity]

B --> B1[Sub-millisecond Operations]
B --> B2[High Throughput 100K+ OPS]
B --> B3[In-memory Speed]

C --> C1[Atomic Operations]
C --> C2[Multiple Counter Types]
C --> C3[Built-in Expiration]

D --> D1[Minimal Setup Complexity]
D --> D2[Proven Reliability]
D --> D3[Extensive Ecosystem]

1. Architecture Advantages​

1.1 Performance Characteristics​

graph LR
subgraph "Latency Comparison (P95 in ms)"
A[Redis Single Node] --> A1[< 1ms]
B[Redis Cluster] --> B1[1-3ms]
C[Database Counter<br/>e.g., PostgreSQL] --> C1[5-20ms]
D[Distributed Log<br/>e.g., Kafka-based] --> D1[10-50ms]
end

subgraph "Throughput Capacity"
E[Simple INCR Operations] --> E1[80,000-100,000 OPS/node]
F[Pipelined Operations] --> F1[500,000+ OPS/node]
G[Clustered Deployment] --> G1[Millions OPS<br/>linear scaling]
end

Real-World Performance Metrics:

  • Single operation latency: 0.1-0.5ms for local Redis
  • Network-added latency: 1-3ms for cross-AZ deployments
  • Throughput scaling: Nearly linear with added nodes
  • Connection overhead: Minimal (10-50KB per connection)

1.2 Data Model Strengths​

Data StructureCounter Use CaseAtomic OperationMemory Efficiency
StringSimple countersINCR, INCRBYHigh (int encoded)
HashMulti-field countersHINCRBYMedium
Sorted SetLeaderboards/rankingZINCRBYMedium
BitmapBoolean/flag countersSETBIT, BITCOUNTVery High
HyperLogLogApproximate unique countsPFADD, PFCOUNTExtremely High

Example: Efficient Multi-dimensional Counting

# User session tracking across dimensions
HINCRBY user:12345:sessions today 1
HINCRBY user:12345:sessions browser:chrome 1
HINCRBY user:12345:sessions country:US 1
EXPIRE user:12345:sessions 86400

1.3 Operational Simplicity​

flowchart TD
subgraph "Deployment & Maintenance"
direction LR
A1[Single Redis Node] --> A2[Simple<br/>Single process]
B1[Redis Sentinel] --> B2[Auto-failover<br/>3-5 nodes]
C1[Redis Cluster] --> C2[Horizontal scaling<br/>Sharded data]
end

subgraph "Development Experience"
D1[Client Libraries] --> D2[20+ languages<br/>Mature SDKs]
E1[API Simplicity] --> E2[INCR, DECR, GET<br/>No schema needed]
F1[Tooling Ecosystem] --> F2[Redis Insight, CLI<br/>Rich monitoring]
end

2. Critical Limitations & Risks​

2.1 Durability and Data Loss Concerns​

timeline
title Data Persistence Trade-offs in Redis
section RDB (Snapshotting)
Save 1 : Point-in-time backup
: Configurable intervals (minute to hours)
: Forking can cause latency spikes
: Potential data loss between saves

section AOF (Append-Only File)
Save 2 : Every-write durability
: Configurable fsync policies
: No data loss with fsync=always
: File growth & recovery time concerns

section RDB + AOF Combined
Save 3 : Balance of safety & performance
: RDB for backups + AOF for durability
: Complex recovery procedures
: Higher disk I/O overhead

Persistence Configuration Decisions:

  • appendfsync everysec: Balance of performance and safety (recommended)
  • appendfsync always: Maximum durability, 10x slower writes
  • appendfsync no: Best performance, OS-controlled flushing
  • save 900 1: RDB snapshots, configurable trade-offs

2.2 Memory Constraints and Costs​

Cost Analysis for 100M Counter System:

Counter TypeMemory per CounterTotal MemoryMonthly Cost (AWS)Notes
Simple Integer16 bytes1.6 GB~$80/moBest case
Hashed Counter64 bytes6.4 GB~$320/moMulti-field
With Metadata128+ bytes12.8 GB+~$640+/moTTL, labels

Memory Optimization Realities:

  • Fragmentation: Can add 30-50% overhead over time
  • TTL cleanup: Lazy expiration causes "ghost memory"
  • Replication overhead: 100% memory duplication for each replica
  • Memory pressure behavior: maxmemory-policy choices all have trade-offs

2.3 Consistency in Distributed Scenarios​

flowchart TD
A[Counter Update Request] --> B{Consistency Requirement}

B -->|Strong Consistency| C[Single Master Architecture]
B -->|Eventual Consistency| D[Multi-Region Active-Active]

C --> C1[Pros: Linearizable operations]
C --> C2[Cons: Single point of failure<br/>Cross-region latency]

D --> D1[Pros: Low latency local reads<br/>High availability]
D --> D2[Cons: Conflict resolution needed<br/>Potential overcount]

C1 --> E[Use Case: Financial transactions<br/>Inventory management]
D2 --> F[Use Case: Page view counters<br/>Social media likes]

Redis Cluster Limitations:

  • Multi-key operations: Limited to single slot (no atomic multi-key INCR)
  • Cross-slot transactions: Not supported natively
  • Consistency during failover: Possible small data loss (async replication)
  • Read-after-write consistency: Not guaranteed across replicas

3. Key Trade-offs Analysis​

3.1 The CAP Theorem Perspective​

quadrantChart
title Redis Counter Deployment in CAP Context
x-axis "Partition Tolerance" --> "Consistency vs Availability"
y-axis "Low" --> "High"

quadrant-1 "High Consistency, Lower Availability"
quadrant-2 "Strong Consistency When Possible"
quadrant-3 "High Availability, Weaker Consistency"
quadrant-4 "Balanced Approach (Default)"

"Redis Single Node<br/>(CA System)": [0.2, 0.3]
"Redis + Sentinel<br/>(CP During Failover)": [0.4, 0.6]
"Redis Cluster<br/>(AP During Partitions)": [0.7, 0.4]
"Multi-Region Async<br/>(Eventual Consistency)": [0.9, 0.2]
"RDB+AOF Everysec<br/>(Balanced Default)": [0.5, 0.5]

Practical CAP Implications:

  • Single Redis node: CA system (no partition tolerance)
  • Redis Sentinel: CP during failover (unavailable during election)
  • Redis Cluster: AP during network partitions (consistency sacrificed)
  • Cross-datacenter: Typically AP (eventual consistency)

3.2 Scalability vs. Complexity​

graph TD
subgraph "Scaling Dimensions"
A[Vertical Scaling<br/>Larger instances] --> A1[Simple<br/>Limited by instance size<br/>~500GB practical limit]
B[Sharding<br/>Manual key distribution] --> B1[Medium complexity<br/>Application-managed<br/>Uneven distribution risk]
C[Redis Cluster<br/>Automatic sharding] --> C1[High complexity<br/>Operational overhead<br/>Multi-key operation limits]
D[Proxy Layer<br/>Twemproxy, Envoy] --> D1[Added complexity<br/>Additional hop<br/>Improved client experience]
end

A1 --> E[Best For: 10M-100M counters<br/>Simple operations]
B1 --> F[Best For: Predictable key patterns<br/>High throughput needs]
C1 --> G[Best For: 100M+ counters<br/>Auto-failover required]
D1 --> H[Best For: Multiple client types<br/>Mixed workloads]

Scaling Decision Framework:

  1. <10M counters: Single large instance often sufficient
  2. 10M-100M counters: Consider Redis Cluster with 3-6 shards
  3. 100M+ counters: Need careful shard key design + monitoring
  4. Global counters: Consider specialized solutions (CRDT-based)

3.3 Cost Efficiency Analysis​

Total Cost of Ownership Comparison:

Cost ComponentRedis In-MemoryDatabase (PostgreSQL)Specialized Counter Service
InfrastructureHigh (RAM-intensive)Medium (CPU/IO balanced)Variable (often SaaS premium)
Development TimeLow (simple API)Medium (schema design)Low (managed service)
OperationsMedium (backup, scaling)High (tuning, vacuuming)Low (provider managed)
Scaling CostLinear with data sizeNon-linear (index overhead)Usage-based pricing
Hidden CostsMemory fragmentation, replication lagConnection pooling, vacuum overheadVendor lock-in, egress fees

When Redis Becomes Expensive:

  • Counters with long TTL (>30 days)
  • High cardinality (millions of unique keys)
  • Multi-region replication needs
  • Strict durability requirements (fsync=always)

4. Alternative Architectures Comparison​

4.1 When to Consider Alternatives​

flowchart TD
A[Counter System Requirements] --> B{Durability Critical?}
B -->|Yes| C{Write Volume?}
B -->|No| D[Redis May Be Suitable]

C -->|Extreme High >1M/sec| E[Consider:<br/>Kafka + Consumer]
C -->|Moderate| F[Consider:<br/>Database + Redis cache]

D --> G{Global Consistency Needed?}
G -->|Yes| H[Consider:<br/>CRDT-based systems]
G -->|No| I[Redis Acceptable]

E --> J[Examples: Clickstream analytics<br/>IoT sensor data]
F --> K[Examples: User session counts<br/>API rate limiting]
H --> L[Examples: Global view counters<br/>Multi-region inventory]

4.2 Hybrid Approach Benefits​

Redis as Cache Layer Pattern:

┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Clients │────▶│ Redis │◀───▶│ Database │
│ │ │ (Counter │ │ (Persistent │
│ │ │ Cache) │ │ Store) │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
│ Fast reads/writes Durability &
│ (~1ms latency) consistency
│
│ ┌─────────────┐
└─────────────▶│ Batch │
│ Processor │
│ (Sync to │
│ database) │
└─────────────┘

Hybrid Strategy Advantages:

  • Best of both worlds: Redis performance + database durability
  • Graceful degradation: Database as fallback during Redis outages
  • Cost optimization: Keep hot data in Redis, archive to database
  • Analytics ready: Database enables complex queries on counter history

4.3 Emerging Alternatives​

SolutionBest ForRedis Comparison
CRDT-based countersMulti-region consistencyBetter for global counts, more complex
Time-series databasesCounter history/analyticsBetter for temporal queries, less real-time
Distributed logsAuditability & replayBetter durability, higher latency
Specialized countersExtreme scale (>1B counters)Better scaling, less general-purpose

5. Decision Framework & Recommendations​

5.1 When Redis is the Right Choice ✅​

Strong Green Lights:

  • Temporary counters: Sessions, rate limits, short-lived metrics
  • Performance-critical: Real-time dashboards, leaderboards
  • Development velocity: Prototyping, MVP development
  • Predictable key patterns: Good shard distribution possible
  • Acceptable data loss: <1 second loss tolerable

Ideal Use Case Profile:

┌─────────────────────────────────────────────────────┐
│ Redis Counter Sweet Spot │
├─────────────────────────────────────────────────────┤
│ • 10M-100M counters │
│ • <5ms latency requirement │
│ • TTL < 30 days for most counters │
│ • Regional deployment (not global) │
│ • OPS team familiar with Redis │
│ • Budget for sufficient RAM │
└─────────────────────────────────────────────────────┘

5.2 When to Be Cautious ⚠️​

Yellow Flags Requiring Mitigation:

  • Financial transactions: Need stronger durability guarantees
  • Global counters: Multi-region consistency challenges
  • Extreme scale: >100M constantly growing counters
  • Long-term retention: >90 days data lifetime
  • Regulatory compliance: Strict audit trail requirements

Mitigation Strategies:

  • Hybrid approach: Redis + database synchronization
  • Enhanced monitoring: Proactive memory/performance alerts
  • Regular testing: Failover and recovery procedures
  • Data tiering: Move old counters to cheaper storage

5.3 When to Choose Alternatives 🛑​

Clear Red Flags:

  • Cannot tolerate any data loss: Financial settlement systems
  • Need ACID transactions: Inventory systems with strict consistency
  • Extremely high cardinality: Billions of unique counter keys
  • Limited RAM budget: Cost-prohibitive for data size
  • Complex query needs: Joins, historical analysis on counter data

6. Implementation Recommendations​

6.1 Success Patterns​

  1. Start Simple: Single Redis instance, add complexity only when needed
  2. Design for Failure: Assume Redis will fail, plan recovery procedures
  3. Monitor Aggressively: Memory, latency, hit rate, replication status
  4. Implement Graceful Degradation: Fallback to database or cached values
  5. Regularly Test Recovery: Backup restoration, failover procedures

6.2 Key Metrics to Watch​

graph TD
subgraph "Critical Redis Counter Metrics"
A[Memory Usage] --> A1[>80% = Warning<br/>>90% = Critical]
B[Latency P99] --> B1[<5ms = Good<br/>>20ms = Investigate]
C[Key Expiration] --> C1[Expired vs Evicted<br/>High eviction = Problem]
D[Replication Lag] --> D1[<1s = Healthy<br/>>10s = Critical]
E[Network Traffic] --> E1[Unexpected spikes = Alert]
end

subgraph "Business Metrics"
F[Counter Accuracy] --> F1[Compare with backup source]
G[Service Availability] --> G1[Counter API uptime]
H[Cost Efficiency] --> H1[Cost per million operations]
end

6.3 Team Readiness Assessment​

CapabilityRequired for Redis CountersAssessment Questions
OperationalRedis administration, backup/restoreCan team handle failover at 3 AM?
MonitoringPerformance tuning, capacity planningAre metrics and alerts in place?
DevelopmentClient library expertise, connection poolingDo devs understand pipeline vs transaction?
ArchitectureData modeling, sharding strategyIs key distribution understood?
Disaster RecoveryBackup validation, recovery proceduresHas recovery been tested recently?

Conclusion: Balanced Perspective​

Redis excels as a distributed counter system for the right use cases. Its unparalleled performance and simplicity make it an excellent choice for many applications, but these advantages come with trade-offs around durability, memory constraints, and distributed consistency.

The decision boils down to:

  1. Can you accept the durability model? (async replication, potential data loss)
  2. Can you afford the memory? (RAM is more expensive than disk)
  3. Do you need strong consistency? (vs. eventual consistency)
  4. Is your team operationally ready? (Redis requires active management)

For many applications—particularly those involving transient data, real-time requirements, or development velocity—Redis represents an excellent choice. For systems requiring absolute durability, complex transactions, or cost-effective long-term storage, alternative or hybrid approaches warrant serious consideration.

Final Recommendation: Start with Redis for its simplicity and performance, but implement the monitoring and fallback strategies that will let you sleep well at night, knowing you can detect and handle its limitations before they impact your users.

Generated by ChatGPT

Below is a concise but deep discussion-style document (not a formal design doc) that focuses on thinking, trade-offs, and architectural judgment when using Redis as a distributed counter system. It mixes high-level concepts with just enough technical detail, and uses diagrams to anchor intuition.


Redis as a Distributed Counter System

Pros, Cons, Benefits, and Trade-offs (Discussion)​


1. What Problem Are We Really Solving?​

At its core, a distributed counter answers one simple question:

“How do many machines safely change one number?”

Examples:

  • Count API requests
  • Enforce rate limits
  • Track events or metrics
  • Maintain inventory or quotas

The challenge is not counting — it’s coordination under concurrency.


2. Why Redis Is Often Chosen​

High-Level View​

flowchart LR
S1[Service A]
S2[Service B]
S3[Service C]

R[(Redis)]

S1 --> R
S2 --> R
S3 --> R

Redis becomes a coordination point that all services trust.


3. Key Benefits of Redis for Counters​

3.1 Atomicity Comes “For Free”​

sequenceDiagram
participant App
participant Redis

App->>Redis: INCR counter
Redis-->>App: new_value
  • Redis is single-threaded per shard
  • INCR is atomic by default
  • No locks, no CAS, no retries

Benefit: ✔ Simple mental model ✔ Hard to misuse


3.2 Extremely Low Latency​

flowchart LR
App -->|~0.2ms| Redis
  • In-memory
  • No disk I/O on hot path
  • Faster than databases or coordination services

Benefit: ✔ Works well for high-QPS counters (rate limiting, metrics)


3.3 Centralized Truth in a Distributed World​

flowchart TD
Local1[Local Counter ❌]
Local2[Local Counter ❌]
Redis[(Single Source of Truth ✔)]

Local1 -. inconsistency .- Local2
Local1 --> Redis
Local2 --> Redis

Benefit: ✔ Avoids drift ✔ Avoids reconciliation logic ✔ Easier reasoning


3.4 Built-in Expiry Enables Time-Based Counters​

flowchart TD
Counter -->|TTL| Expiry
Expiry --> Reset

Examples:

  • Per-minute request counts
  • Daily metrics
  • Sliding windows (via Lua)

Benefit: ✔ No cron jobs ✔ No cleanup code


4. Trade-offs You Accept (Knowingly or Not)​

4.1 Redis Becomes a Central Dependency​

flowchart TD
App --> Redis
Redis -->|Down| AppFail[App degraded or blocked]

Trade-off:

  • You gain simplicity
  • You lose decentralization

Redis availability now directly affects your system.


4.2 Single-Threaded Nature Is a Double-Edged Sword​

flowchart LR
ManyClients --> OneCore[Redis Core]

✔ Atomicity ❌ CPU bottleneck at extreme scale

Implication:

  • Great for counters
  • Dangerous if abused with heavy scripts or big keys

4.3 Memory-First Means Cost Sensitivity​

flowchart TD
Counters --> Memory
Memory -->|Eviction| DataLoss

Trade-off:

  • Speed vs cost
  • Persistence vs simplicity

Counters must tolerate:

  • Evictions
  • Resets
  • Restarts

4.4 Durability Is “Best Effort” (Unless Tuned)​

flowchart LR
INCR --> RAM -->|Async| Disk

Depending on config:

  • RDB snapshots
  • AOF (append-only)

Trade-off: ✔ Fast writes ❌ Possible loss of recent increments


5. Redis vs Other Mental Models​

5.1 Redis vs Database Counters​

flowchart LR
App --> Redis --> Fast
App --> DB --> Slow
AspectRedisDatabase
Speed⭐⭐⭐⭐⭐⭐⭐
AtomicityBuilt-inNeeds transactions
CostMemory-basedDisk-based
DurabilityConfigurableStrong

5.2 Redis vs Local Counters​

flowchart TD
LocalA --> Drift
LocalB --> Drift

Local counters:

  • Fast
  • Wrong

Redis counters:

  • Slightly slower
  • Correct

5.3 Redis vs Streaming Systems (Kafka)​

flowchart LR
App --> Kafka --> Aggregation --> Counter

Kafka is:

  • Append-only
  • Eventually consistent

Redis is:

  • Immediate
  • Mutable

Trade-off: Real-time accuracy vs replayability


6. Where Redis Counters Shine 🌟​

flowchart TD
UseCases --> RateLimit
UseCases --> Metrics
UseCases --> Quotas
UseCases --> Inventory

Best fit:

  • Rate limiting
  • Quota enforcement
  • Live metrics
  • Feature usage counters

7. Where Redis Counters Are Risky ⚠️​

flowchart TD
Risky --> Money
Risky --> Audits
Risky --> ExactlyOnce

Poor fit:

  • Financial ledgers
  • Legal/audit logs
  • Exactly-once semantics
  • Long-term historical truth

8. Operational Complexity (Often Underestimated)​

flowchart TD
Redis --> Monitoring
Redis --> Backup
Redis --> Failover

You must care about:

  • Memory pressure
  • Evictions
  • Failover behavior
  • Client retries
  • Lua safety

Trade-off: Operational burden for runtime simplicity


9. The Philosophical Trade-off​

flowchart LR
Consistency --> Availability

Redis counters choose:

  • Strong atomic updates
  • Temporary unavailability over inconsistency

This is often the right trade-off for counters.


10. Mental Checklist Before Choosing Redis​

Ask yourself:

  • Can this counter reset safely?
  • Is losing a few increments acceptable?
  • Can the system pause if Redis is down?
  • Do we need exact historical replay?

If most answers are “yes” → Redis is a strong candidate.


11. Final Thought​

Redis counters are not about counting — they are about coordination.

They trade:

  • durability for speed
  • decentralization for simplicity
  • historical truth for real-time correctness

Used consciously, Redis is one of the best distributed counter tools available.


If you want, I can:

  • Compare Redis counters with etcd / Zookeeper
  • Discuss exactly-once illusions
  • Show real incident patterns
  • Turn this into an architecture decision record (ADR)