Files
myWiki/concepts/distributed-optimistic-locking.md
2026-06-01 10:46:01 +08:00

29 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Distributed Optimistic Locking (分布式乐观锁)"
created: 2026-05-29
updated: 2026-05-29
type: concept
tags: ["distributed-systems", "consistency", "redis", "concurrency"]
sources: ["https://mp.weixin.qq.com/s/MUWV7eug14bktUMlqsxfQw"]
---
# Distributed Optimistic Locking (分布式乐观锁)
**Distributed Optimistic Locking** 是 [[distributed-prompt-caching]] 中的一致性保障机制:通过 Redis 的 `WATCH` 命令和上下文版本号,确保多个物理节点并发更新同一会话上下文时不会发生"缓存分叉"。
## 机制
1. **版本号递增**:每次 Agent 生成响应或 Tool 返回结果上下文逻辑版本号递增V_{next} = V_{current} + 1
2. **乐观锁申请**:任何节点发起带缓存的请求前,向 Redis 申请 `WATCH cache:version:[Session_UID]`
3. **冲突处理**:只有率先成功将版本号推向新值的节点能成功提交;落后节点缓存被宣告"部分污染"
4. **上下文对齐**:落后节点触发 Context-Realign——从共享骨干网拉取最新 Message 历史,重新进行局部 Shadow Calling 预热
## 在缓存同步中的必要性
分布式 Prompt Caching 的难点在于:多个决策模型节点可能同时对同一信号给出不同的交叉评估结论,如果缺乏一致性控制,各节点的缓存前缀将发生分叉,导致后续协作时使用过期或冲突的上下文。
## 相关
- [[distributed-prompt-caching]] — 分布式缓存体系
- [[distributed-agent-cache-sync-2026]] — 原始文章