跳到主要内容

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.