How Redis Works End to End
-
You start a Redis server - it runs as a single process on a machine.
-
Redis stores everything in-memory, so it’s blazing fast.
-
You connect to Redis using a client - could be from Java, Python, Node.js, etc.
-
You send commands like
SET
,GET
,INCR
,DEL
- it responds instantly. -
Redis stores data as key-value pairs - the value can be strings, hashes, lists, sets, or sorted sets.
-
It supports atomic operations - e.g., increment a counter with
INCR
and it’s safe from race conditions. -
You can set expiry on keys - using
EXPIRE
orSET key value EX 60
to auto-delete after 60 seconds. -
It’s often used as a cache - store data that’s expensive to compute or slow to fetch from DB.
-
Redis supports pub/sub - you can publish messages to a channel and other clients can subscribe and react in real time.
-
You can use Lists (like a queue), Sets (for unique items), and Sorted Sets (for leaderboards, ranked data).
-
Redis supports Lua scripting - you can run small programs on the server to perform complex logic atomically.
-
You can use Streams in Redis - great for real-time event data like logs or messaging.
-
Redis supports bitmaps, hyperloglogs, and geospatial indexes - niche but powerful tools for specific use cases.
-
Redis is single-threaded for commands but super fast due to memory and tight architecture.
-
It can persist data using RDB snapshots (periodic dumps) or AOF logs (write every operation).
-
You can choose to run it purely in-memory (ephemeral) or with persistence (survives reboots).
-
Redis supports replication - a master can have one or more read-only replicas.
-
If master goes down, you can use Redis Sentinel to handle automatic failover and monitoring.
-
For large-scale setups, you can use Redis Cluster - data is sharded across multiple nodes.
-
Redis also supports transactions using
MULTI
/EXEC
- run multiple commands as one unit. -
You can use it for rate limiting, session storage, real-time analytics, job queues, and leaderboards.
-
All of this with millisecond latency - that’s why Redis is a go-to for performance-critical systems.
Redis Technical Deep Dive
Core Architecture
Redis employs a simple yet powerful architecture:
-
Event Loop Model:
- Uses an event-driven, non-blocking I/O model
- Built around
ae
(Advanced Event) library - Similar to Node.js event loop but single-threaded
- Processes commands sequentially (no context switching overhead)
-
Memory Management:
- Custom memory allocator (jemalloc/tcmalloc)
- Minimizes fragmentation
- Memory is pre-allocated in chunks
- No garbage collection pauses
-
Communication Protocol (RESP):
- Redis Serialization Protocol
- Simple text-based protocol
- Binary-safe
- Low overhead for parsing
-
Main Components:
- Server core (event loop + command processing)
- Data structures implementations
- Persistence mechanisms
- Replication system
- Cluster management
Data Structures Implementation
Redis data types are implemented with sophisticated internal structures:
-
Strings:
- Simple Dynamic Strings (SDS) implementation
- Binary-safe (can contain any data)
- Optimized small string encoding (for values < 44 bytes)
- Int optimization (stores numbers as actual integers)
-
Lists:
- Implemented as linked lists or ziplist (for small lists)
- Optimized for fast push/pop at both ends (O(1))
- Memory-efficient encodings for small lists
-
Hashes:
- Small hashes use ziplist encoding
- Larger hashes use hash tables (dictionaries)
- Auto-conversion between encodings based on size
-
Sets:
- Either hash tables (for regular sets) or intsets (for integer-only sets)
- O(1) member lookups
- Memory optimization for integer-only sets
-
Sorted Sets:
- Combination of a hash table and a skip list
- O(log N) insertions and score-based retrievals
- Used for leaderboards, priority queues, range queries
-
Bitmaps:
- Not a separate type (implemented on strings)
- Bit-level operations on string values
- Memory-efficient for counting unique items (1 bit per item)
-
HyperLogLog:
- Probabilistic data structure for cardinality estimation
- Extremely memory-efficient (uses ~12KB for counting millions of items)
- 0.81% standard error
Command Processing Flow
How Redis handles a command:
-
Command Reception:
- Client sends RESP-formatted command
- Server reads input buffer during event loop
- Parses command and arguments
-
Command Lookup:
- Looks up command in command table (O(1) hash table)
- Verifies argument count and access permissions
-
Command Execution:
- Calls command implementation function
- Manipulates data structures in memory
- Prepares response
-
Response Generation:
- Formats response in RESP protocol
- Writes to client output buffer
- Sends when socket is writable (next event loop iteration)
-
Command Propagation (if applicable):
- Appends to AOF if enabled
- Queues for replication to replicas
- Logs for monitoring if enabled
Persistence Mechanisms
Redis offers two main persistence options:
-
RDB (Redis Database):
- Point-in-time snapshots
- Binary compact format
- Created via:
- Background process (BGSAVE)
- Foreground save (SAVE)
- Automatic based on configuration
- Pros: Compact, faster restarts
- Cons: Potential data loss between snapshots
-
AOF (Append-Only File):
- Log of all write operations
- Human-readable format (Redis commands)
- Configurable fsync policies:
- always: Safest, slowest
- everysec: Good compromise
- no: Fastest, OS controlled
- Background rewriting to avoid file growth
- Pros: Durability, easier corruption recovery
- Cons: Larger files, slower restarts
-
Hybrid Persistence (RDB+AOF):
- AOF can include RDB preamble
- Combines restart speed with better durability
Memory Optimization Techniques
Redis employs several techniques to minimize memory usage:
-
Special Encodings:
- Small lists, sets and hashes use compact encodings
- Integer-only data uses special representations
- Shared objects for common values
-
Memory Policies:
- LRU (Least Recently Used) eviction
- LFU (Least Frequently Used) eviction
- Random eviction
- TTL-based eviction
- No-eviction (return errors when memory limit reached)
-
maxmemory Configuration:
- Sets memory usage limit
- Triggers eviction when limit is reached
- Can be dynamically adjusted
-
Key Expiration:
- Passive expiry (lazy deletion when keys are accessed)
- Active expiry (periodic sampling of expired keys)
- Avoids CPU spikes from many simultaneous expirations
Replication Architecture
Redis uses a master-replica model:
-
Replication Process:
- Initial full sync (RDB transfer)
- Followed by command streaming
- Partial resync after disconnections (using replication buffer)
-
Replication Modes:
- Disk-based: Master creates RDB on disk, then sends
- Diskless: Master sends RDB directly to replicas
-
Consistency Model:
- Asynchronous replication (by default)
- Semi-synchronous with WAIT command
- Eventual consistency guarantee
-
Replica Features:
- Read-only by default (can be configured as writable)
- Can have their own replicas (cascading replication)
- Can persist data independently of master
Sentinel System
Redis Sentinel provides high availability:
-
Core Functions:
- Monitoring: Checks if masters and replicas are working
- Notification: Alerts administrators or other programs
- Automatic failover: Promotes replica when master fails
- Configuration provider: Clients connect to Sentinel to find current master
-
Quorum-based Decisions:
- Multiple Sentinels must agree on failure
- Prevents split-brain scenarios
- Configurable quorum size
-
Failover Process:
- Majority of Sentinels detect master failure
- Leader election among Sentinels
- Best replica selection
- Reconfiguration of other replicas
Clustering Architecture
Redis Cluster for horizontal scaling:
-
Data Sharding:
- Uses hash slot approach (16384 slots)
- CRC16(key) mod 16384 determines slot
- Each node handles a subset of slots
- Hash tags allow multi-key operations ([key-name]) within same slot
-
Node Communication:
- Gossip protocol for node discovery and heartbeats
- Binary protocol for data exchange
- Separate bus port for node-to-node communication
-
Fault Tolerance:
- Master-replica pairs for each shard
- Automatic failover when master is down
- Automatic replica migration for better distribution
-
Consistency Guarantees:
- No strong consistency guarantee
- Asynchronous replication between master and replicas
- Potential data loss during failovers
- Redis Cluster prefers availability over consistency (AP in CAP theorem)
Performance Considerations
Key factors affecting Redis performance:
-
CPU Impact:
- Single-threaded main process (commands execute sequentially)
- IO threading since Redis 6 (for network handling)
- CPU frequency more important than core count
- Background tasks (e.g., persistence) affect performance
-
Memory Impact:
- Data should fit in RAM for optimal performance
- Swapping to disk severely impacts performance
- Virtual memory fragmentation affects real memory usage
-
Network Factors:
- Latency between client and server
- Network bandwidth for large values
- Connection pooling importance
- Pipelining to reduce round-trips
-
Persistence Overhead:
- RDB creation (fork and copy-on-write)
- AOF rewriting (temporary increased memory usage)
- fsync frequency affecting write latency