分布式系统的终极命题:网络不可靠、时钟不可靠与状态共识
在分布式环境下,没有全局物理时钟,任何网络包都可能丢失、乱序或延迟数秒,硬件故障是系统常态。真正的架构师不追求“永不宕机”,而是通过严密的数学证明与工程契约,在非可靠底座上构建可靠的分布式状态机。
分布式共识协议与状态机复制 (Raft Consensus & Log Replication)
1. Nodes exist in three states: Follower, Candidate, or Leader.
2. If Followers do not receive Leader heartbeats before the randomized election timeout, they become Candidates.
3. A Candidate needs votes from a strict majority (N/2 + 1) to become Leader.
4. The Leader commits log entries only after a majority of Followers write them to disk.
5. In network partitions, minority partitions cannot commit writes, preventing split-brain corruption.
一致性模型深度辨析:线性一致性、因果一致性与 PACELC 定理
分布式事务终局:Saga 编排器与 Transactional Outbox + CDC
1. Two-Phase Commit (2PC) blocks resources and causes severe latency under high scale.
2. Sagas break transactions into local steps with corresponding compensating rollback actions.
3. Transactional Outbox writes the domain change and the outbound message into the same local database transaction.
4. Debezium CDC reads database transaction logs and streams events to Kafka with zero dual-write inconsistency.
现代存储引擎剖析:LSM-Tree vs B+ Tree、读写放大与分层压缩
- • 读优化:随机点查性能极致,单次点查仅需 3~4 次磁盘寻道。
- • 原地修改:每次写入需修改磁盘物理页,引发随机 I/O 与双写缓冲。
- • 写放大系数通常在 10 ~ 30 倍。
- • 写优化:顺序追加写入 WAL 与内存跳表 (MemTable),吞吐极高。
- • 后台分层压实 (Leveled Compaction):内存满后刷盘为 SSTable,后台线程周期性合并。
- • 布隆过滤器 (Bloom Filter):快速跳过不包含目标 Key 的 SSTable,缓解读放大。
高性能缓存设计与双写一致性陷阱 (Cache-Aside & Delayed Double Delete)
应用层只修改数据库,不直接碰缓存;由独立消费组件监听 MySQL Binlog 增量,确认事务提交成功后,再向 Redis 发送删除指令,结合消息队列的重试保障,彻底切断应用层双写时序竞争。
海量数据分片:一致性哈希与虚拟节点负载均衡 (Consistent Hashing & Virtual Nodes)
分布式限流与流量整型算法 (Token Bucket vs Leaky Bucket vs Sliding Window)
- 固定窗口计数器:粗糙,存在跨窗口临界爆发(前后两秒临界瞬间涌入 2 倍限额)。
- 滑动窗口计数器:平滑统计最近 60s 内请求加权和,消灭临界突刺。
- 漏桶算法 (Leaky Bucket):恒定速率流出,常用于写数据库限速,完全禁止突发。
- 令牌桶算法 (Token Bucket):允许突发流量消费预存令牌,适合面向用户的 API 网关。
高吞吐消息引擎与流处理语义 (Kafka Partitions & Exactly-Once Semantics - EOS)
2. PageCache 与 Zero-Copy: 使用 Linux
sendfile 系统调用,网卡直接从 OS PageCache 抓取数据,零用户态拷贝。3. 端到端 EOS: 生产者分配 PID + Sequence Number 保证幂等,结合
beginTransaction() 确保消费位移与下游产出原子提交。
服务高可用弹性与防雪崩模式:断路器、全抖动重试与过载脱离
1. Blind retries create a Retry Storm that crashes recovering backends.
2. Exponential backoff increases delay between retries exponentially: \(t = b \times 2^i\).
3. Full Jitter chooses a random sleep time uniformly between 0 and \(t\), breaking synchronization spikes.
4. Load Shedding drops low-priority requests based on queue latency before CPU reaches 100% saturation.
分布式全局唯一 ID 生成体系与时钟回拨处理 (Snowflake & NTP Skew)
无主复制与 Quorum 读写仲裁机制 (Dynamo Quorum & Anti-Entropy)
1. In leaderless replication, any node accepts writes and reads.
2. Set total replicas \(N\), write quorum \(W\), and read quorum \(R\).
3. If \(R + W > N\), the read set and write set must overlap by at least one node.
4. This guarantees that reads always discover the newest written value version.