Databases 22 min read

Redis Deep Dive: Protocols, ACID Myths, Persistence, and Optimization

This article explores Redis's text-based RESP protocol, examines whether Redis truly supports ACID transactions, explains its optimistic lock implementation, details persistence mechanisms like RDB and AOF, discusses memory‑saving data structures, outlines replication processes, and reviews expiration deletion strategies, providing a comprehensive technical overview.

21CTO
21CTO
21CTO
Redis Deep Dive: Protocols, ACID Myths, Persistence, and Optimization

1. Redis Communication Protocol

Redis uses a simple text protocol called RESP (Redis Serialization Protocol) for client‑server communication. Commands are encoded as simple strings (+), errors (-), integers (:), bulk strings ($), or arrays (*), allowing interaction via Telnet or raw TCP streams.

2. Does Redis Have ACID Transactions?

Redis provides transactions with MULTI and EXEC, but they lack full ACID guarantees. While commands are queued and executed atomically, there is no rollback on failure, so strict atomicity and durability are not ensured. Consistency and isolation are limited by Redis's single‑threaded execution model.

3. How Redis Optimistic Lock (Watch) Works

The WATCH command monitors one or more keys before a transaction. If any watched key changes before EXEC, the transaction is aborted. Internally, Redis maintains a watched_keys dictionary mapping keys to client flags; when a key is modified, all watching clients are marked CLIENT_DIRTY_CAS and their transactions are discarded.

4. How Redis Persists Data

Redis offers two persistence modes: RDB snapshots and AOF logs. RDB creates point‑in‑time binary dumps, either synchronously with SAVE or asynchronously with BGSAVE (forking a child process). AOF records every write command; it can be rewritten with BGREWRITEAOF to shrink size. Since AOF writes occur after command execution, Redis cannot roll back operations.

5. Memory Optimization in Redis

Redis uses compact data structures such as Ziplist for small hashes and sorted sets, and Quicklist (a linked list of Ziplist nodes) for lists, reducing memory overhead. Configuration parameters like hash-max-ziplist-entries and list-max-ziplist-value control when structures switch to standard representations. Object sharing via reference counting further saves memory when multiple keys hold identical values.

6. Replication

Redis replication uses the PSYNC command (replacing SYNC) to perform full or partial synchronization. Full sync sends an RDB snapshot and streams subsequent writes. Partial sync replays only the missed commands from the replication backlog if the slave’s offset is still available, avoiding costly full resynchronizations.

7. Expiration Deletion Strategies

Redis employs three strategies for expired keys: timed deletion (creates a timer per key, costly for CPU), lazy deletion (checks expiration on key access, cheap for CPU but can waste memory), and periodic deletion (runs at intervals, balancing CPU and memory usage). The periodic algorithm limits execution time and frequency to minimize impact.

8. Conclusion

Redis balances simplicity, performance, and flexibility, sacrificing strict ACID compliance for speed. Understanding its protocol, transaction model, persistence options, memory optimizations, replication, and expiration policies enables developers to use Redis effectively while avoiding common pitfalls.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory OptimizationPersistenceReplicationprotocolACIDExpiration
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.