How does Redis work internally?
Redis is an in-memory data structure store. Understanding its internals explains both its performance characteristics and its limitations.
**Single-threaded event loop**: Redis processes commands on a single thread using an I/O event loop (based on epoll/kqueue). This eliminates lock contention and context-switch overhead, giving Redis predictably low latency—typically sub-millisecond. The single-threaded model means CPU is rarely the bottleneck; network I/O and memory bandwidth are. Redis 6.0 added threaded I/O for reading/writing network data, but command execution itself remains single-threaded.
**Data structures**: Redis stores all values as one of its specialized data structures, each with a compact internal encoding that switches based on size. Lists are stored as listpacks (formerly ziplist) for small sizes and doubly-linked lists for large. Sets are intsets (sorted arrays of integers) for small integer sets, hashtables otherwise. Sorted sets use a skiplist + hashtable dual structure: the skiplist enables O(log N) range queries, the hashtable enables O(1) score lookups by member. Hashes use listpack when small, hashtable when large. These automatic encoding switches save significant memory.
**Memory model**: Redis uses jemalloc and stores all data in RAM. Key expiry is handled lazily (checked on access) and actively (periodic sampling of random keys to evict expired ones). When `maxmemory` is set, eviction policies (LRU, LFU, random, volatile-only variants) determine what gets evicted under memory pressure.
**Persistence**: Two mechanisms. **RDB snapshots** fork the process and write a point-in-time binary dump—fast to restore, but can lose minutes of data. **AOF (Append-Only File)** logs every write command; `fsync` policy (`always`, `everysec`, `no`) controls durability vs. performance trade-off. `everysec` is the common default: at most 1 second of data loss. AOF rewrite compacts the log by re-emitting the current state. Many deployments use both: AOF for durability, RDB for fast restarts.
**Replication** is asynchronous: replicas stream the replication backlog from the primary. Redis Cluster shards data across nodes using 16384 hash slots with gossip-based cluster management. Sentinel provides HA for non-cluster deployments via leader election and automatic failover.
Explains single-threaded event loop and why it's fast, describes at least two data structures with their internal encodings, explains RDB vs. AOF persistence trade-offs.
Covers threaded I/O in Redis 6+, explains encoding switches (listpack→hashtable), discusses lazy vs. active expiry, explains AOF fsync policies with their durability/performance trade-offs, and describes replication and cluster architecture.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment