Comprehensive Guide to Redis Data Structures, Persistence, Transactions, Clustering, and Applications
This article provides an in‑depth technical overview of Redis, covering its core data structures, memory allocation strategies, eviction policies, persistence mechanisms (RDB and AOF), transaction model, sentinel and cluster architectures, Pub/Sub messaging, and multiple approaches to implementing distributed locks.
Redis is an open‑source, in‑memory, key‑value store written in C that supports five primary data structures—String, List, Set, Sorted Set (ZSet), and Hash—each with specific internal encodings such as SDS for strings, ziplist and quicklist for lists, and hash tables for hashes.
Memory allocation is managed by the redisObject structure, which records the type and encoding of each value; Redis uses both int , raw , and embstr encodings for strings, and employs ziplist, skiplist, and hashtable implementations for other types. The article shows example C code snippets and explains how these encodings affect performance.
When the configured maxmemory limit is reached, Redis applies one of six eviction policies (noeviction, allkeys‑lru, volatile‑lru, allkeys‑random, volatile‑random, volatile‑ttl) to free space, with LRU approximated by random sampling of keys.
Persistence is offered via RDB snapshots (triggered by SAVE or BGSAVE ) and AOF logging (with appendonly yes and configurable appendfsync policies). The article details the rewrite process that compacts AOF files and introduces hybrid persistence (RDB+AOF) in Redis 4.0+.
Redis transactions are implemented with the commands MULTI , EXEC , DISCARD , and WATCH . They queue commands for atomic execution but lack true rollback; runtime errors are reported per command while syntax errors abort the whole transaction.
High‑availability setups include master‑slave replication, Sentinel monitoring (with automatic failover using a Raft‑style election), and full Redis Cluster which shards the keyspace into 16,384 slots and routes requests based on CRC16 hashing.
Pub/Sub messaging is demonstrated with PUBLISH , SUBSCRIBE , and pattern subscriptions ( PSUBSCRIBE ), showing how clients can receive real‑time messages.
Distributed locking can be built using simple SETNX / GETSET / EXPIRE / DEL patterns, a more robust Lua‑scripted approach, or the Redisson library, which abstracts lock acquisition, lease renewal, re‑entrancy, and release via a background watchdog.
Code examples throughout the article are wrapped in <code>...</code> tags to preserve exact syntax for Java, Redis CLI, and C snippets.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.