What You Need to Know About Distributed Systems: Theory, Consistency, Transactions & Redis
This comprehensive guide covers the core concepts of distributed systems—including CAP theory, consistency models, transaction protocols like 2PC/3PC and TCC, Redis features, persistence options, expiration strategies, memory eviction policies, distributed locking with Redis and Zookeeper, as well as an overview of Kafka's architecture and reliability mechanisms.
Distributed Theory
Distributed components include cache (Redis), lock (Redis/Zookeeper), services (Dubbo/Spring Cloud), coordination (Zookeeper), message queues (Kafka, RabbitMQ), session management, transactions, and search (Elasticsearch). Mastery of one area is essential.
CAP and Consistency
CAP theorem states a distributed system cannot simultaneously guarantee Consistency, Availability, and Partition tolerance; typically a CP or AP trade‑off is chosen. Consistency can be strong, weak, or eventual.
Distributed Transactions
Two‑phase commit (2PC) and three‑phase commit (3PC) are basic protocols. Common solutions include TCC, XA, and message‑queue based compensation.
T (Try) – lock resources, e.g., set order status to “PAYING”, freeze stock, or pre‑add points.
Order status → PAYING
Freeze stock in a temporary field
Pre‑add points in a temporary field
C (Confirm) – after all Try steps succeed, commit changes such as marking order paid, deducting stock, adding points.
Cancel – if any Try fails, rollback the temporary resources.
Redis Overview
Redis is an in‑memory key‑value store offering fast access, rich data types (string, list, set, sorted set, hash), atomic operations, expiration, and single‑threaded I/O multiplexing.
Data structures – strings use SDS, lists use ziplist or linked list, etc.
Persistence – AOF logs commands (fast write, slower recovery) and RDB snapshots (fast recovery). AOF rewrite compacts the log.
Expiration – lazy deletion on access and periodic scanning; random deletion and TTL‑based policies are also used.
Memory eviction – policies include volatile‑LRU, allkeys‑LRU, allkeys‑random, volatile‑ttl, etc.
Cluster – master‑slave, Sentinel, and Cluster modes with 16384 hash slots for horizontal scaling.
Redis Distributed Lock
Implemented via SET key value EX seconds NX. The lock value can be a JSON containing thread ID and count for re‑entrancy. Use atomic set and expire to avoid deadlocks.
Zookeeper
Provides coordination services, distributed lock via temporary sequential znodes, leader election, and can act as a service registry.
Kafka Basics
Kafka is a distributed log system with topics, partitions, and replicas. It ensures high throughput via zero‑copy, batch compression, sequential disk writes, and supports strong durability with ISR and ACK configurations.
Consumers belong to consumer groups; each message is delivered to one consumer in the group. Offsets track consumption.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
