4. Weight 字段详解 (The "Weight" Field)
4.1 设计局限性 (Design Limitations)
4.1.1 当前方案的不完美性 (Imperfection of Current Approach)
Weight (服务器选择字段) 并不完全令人满意 (not quite satisfactory)。
根本原因 (Root Cause):
典型服务器上的实际负载 (actual load) 变化太快 (changes much too quickly),无法保存在 DNS 缓存中。
技术约束 (Technical Constraints):
DNS TTL 通常: 秒/分钟级别
服务器负载变化: 毫秒/秒级别
时间尺度不匹配 (Time Scale Mismatch)
→ DNS 无法实时反映动态负载
4.1.2 最佳实践方案 (Best Practical Approach)
在作者看来,为管理员提供一种方式来表达"这台机器的速度是那台机器的三倍"是实际上能做到的最好的事情 (the best that can practically be done)。
Weight 字段的定位 (Weight Field Positioning):
Weight 表达的是:
✅ 静态硬件能力 (Static Hardware Capacity)
- CPU 性能
- 网络带宽
- 内存容量
❌ 动态实时负载 (Dynamic Real-Time Load)
- 当前 CPU 使用率
- 当前网络流量
- 当前连接数
4.2 动态负载均衡的替代方案 (Alternatives for Dynamic Load Balancing)
4.2.1 分离服务器方案 (Separate Server Approach)
方案描述 (Approach Description):
作者能看到的获得"更好"负载数字的唯一方法是:
在客户端选择服务器并联系它时,询问一个单独的服务器 (separate server) 以获取实时负载信息。
架构示意 (Architecture Diagram):
客户端 负载均衡服务器 目标服务器
| | |
|-- 1. 请求服务器列表 ------>| |
|<-- 2. 返回 SRV 记录 -------| |
| | |
|-- 3. 查询实时负载 -------->| |
|<-- 4. 返回负载数据 --------| |
| | |
|-- 5. 连接选定服务器 ------------------------------>|
4.2.2 短期服务的问题 (Problems with Short-Lived Services)
场景 (Scenario):
短期服务 (short-lived services),例如 HTTP 请求、DNS 查询。
问题分析 (Problem Analysis):
连接建立中的额外步骤 (extra step) 似乎太昂贵了 (too expensive)。
成本分析 (Cost Analysis):
传统方式:
1. DNS 查询 SRV 记录
2. 连接目标服务器
总计: 2 步
动态负载查询:
1. DNS 查询 SRV 记录
2. 查询负载均衡服务器
3. 连接目标服务器
总计: 3 步 (增加 50% 延迟)
影响评估 (Impact Assessment):
- 短期服务的连接时间占比高
- 额外步骤显著增加用户感知延迟
- 不适用于高性能要求场景
4.2.3 长期服务的问题 (Problems with Long-Lived Services)
场景 (Scenario):
长期服务 (long-lived services),例如数据库连接、SSH 会话。
问题分析 (Problem Analysis):
负载数字可能在连接建立一分钟后 (a minute after) 就被抛弃 (thrown off),当其他人开始或完成繁重的工作时。
负载变化场景 (Load Change Scenarios):
T=0: 查询负载均衡器 → Server A: 10% CPU
选择 Server A
T=1: 连接到 Server A,开始长期任务
T=2: 其他用户启动重负载任务
→ Server A: 95% CPU
但客户端已经连接,无法切换
结论 (Conclusion):
长期连接建立后,初始负载信息失去意义。
4.3 实验性方案与未来研究 (Experimental Approaches and Future Study)
4.3.1 网络度量实验 (Network Metrics Experiments)
目前正在进行各种实验,提供以下服务:
- 相对网络接近度估计 (relative network proximity estimation)
- 可用带宽估计 (available bandwidth estimation)
- 类似服务
研究方向 (Research Directions):
-
网络拓扑感知 (Network Topology Awareness)
- 选择地理位置最近的服务器
- 减少网络延迟
-
带宽感知 (Bandwidth Awareness)
- 测量客户端到服务器的可用带宽
- 优化大文件传输
-
延迟感知 (Latency Awareness)
- 测量往返时间 (RTT)
- 优化实时应用
4.3.2 Weight 字段在实验性设施中的使用 (Use of Weight with Experimental Facilities)
将 SRV 记录与此类设施一起使用,特别是在使用这些设施时对 Weight 字段的解释,有待进一步研究 (is for further study)。
开放问题 (Open Questions):
- 如何将静态 Weight 与动态度量结合?
- 是否需要新的 DNS 记录类型?
- 客户端如何选择最佳组合策略?
4.4 Weight 字段的设计意图 (Design Intent of Weight Field)
4.4.1 静态信息表达 (Static Information Expression)
Weight 仅用于静态 (only intended for static),而非动态 (not dynamic) 服务器选择。
适用场景 (Applicable Scenarios):
Weight 仅用于表达静态信息,例如:
示例 1: CPU 性能差异
; 高性能服务器 (8 核心)
_service._tcp.example.com. IN SRV 0 80 8080 fast-server.example.com.
; 中等性能服务器 (4 核心)
_service._tcp.example.com. IN SRV 0 40 8080 medium-server.example.com.
; 低性能服务器 (2 核心)
_service._tcp.example.com. IN SRV 0 20 8080 slow-server.example.com.
权重比例: 80:40:20 = 4:2:1
对应 CPU 核心比例
示例 2: 网络连接质量
; 10 Gbps 网络连接
_storage._tcp.example.com. IN SRV 0 100 9000 datacenter-a.example.com.
; 1 Gbps 网络连接
_storage._tcp.example.com. IN SRV 0 10 9000 datacenter-b.example.com.
权重比例: 100:10 = 10:1
对应带宽比例
4.4.2 动态选择的问题 (Problems with Dynamic Selection)
技术约束 (Technical Constraints):
使用 SRV weight 进行动态服务器选择将需要为 SRV 资源记录分配不合理的短 TTL (unreasonably short TTLs)。
负面影响链 (Negative Impact Chain):
短 TTL (例如 1-5 秒)
↓
限制 DNS 缓存机制的有用性
↓
增加整体网络负载 (increasing overall network load)
↓
降低整体可靠性 (decreasing overall reliability)
问题分析 (Problem Analysis):
| 方面 | 静态 Weight (长 TTL) | 动态 Weight (短 TTL) |
|---|---|---|
| DNS 查询频率 | 低 (每小时/天一次) | 高 (每秒/分钟多次) |
| DNS 服务器负载 | 低 | 极高 |
| 网络流量 | 低 | 高 |
| 缓存效率 | 高 | 低 |
| 可靠性 | 高 | 低 (DNS 成为单点故障) |
4.4.3 通过 SRV 进行服务器选择的范围 (Scope of Server Selection via SRV)
明确定位 (Clear Positioning):
通过 SRV 进行的服务器选择仅用于表达静态信息。
合适的使用场景 (Appropriate Use Cases):
✅ 硬件能力差异
"这台服务器的 CPU 比那台服务器快"
("this server has a faster CPU than that one")
✅ 网络基础设施差异
"这台服务器的网络连接比那台服务器好得多"
("this server has a much better network connection than that one")
✅ 存储容量差异
"这台服务器的存储空间是那台服务器的两倍"
不合适的使用场景 (Inappropriate Use Cases):
❌ 实时 CPU 使用率
"这台服务器当前 CPU 使用率为 30%"
→ 使用专门的负载均衡服务
❌ 当前连接数
"这台服务器当前有 50 个活动连接"
→ 使用应用层负载均衡
❌ 实时网络拥塞
"到这台服务器的网络当前延迟为 50ms"
→ 使用智能 DNS 或 CDN
4.5 最佳实践建议 (Best Practice Recommendations)
4.5.1 Weight 值的设定原则 (Weight Value Setting Principles)
原则 1: 反映硬件能力比例
; 32 核心服务器
_app._tcp.example.com. IN SRV 0 32 8080 server-32core.example.com.
; 16 核心服务器
_app._tcp.example.com. IN SRV 0 16 8080 server-16core.example.com.
; 8 核心服务器
_app._tcp.example.com. IN SRV 0 8 8080 server-8core.example.com.
原则 2: 考虑综合性能
; 高性能服务器 (新硬件 + 快速网络)
_db._tcp.example.com. IN SRV 0 100 5432 db-new.example.com.
; 中等性能服务器 (旧硬件 + 快速网络)
_db._tcp.example.com. IN SRV 0 60 5432 db-old-fast-net.example.com.
; 低性能服务器 (旧硬件 + 慢速网络)
_db._tcp.example.com. IN SRV 0 30 5432 db-old-slow-net.example.com.
原则 3: 使用简单比例
; 推荐: 简单比例 (2:1)
_svc._tcp.example.com. IN SRV 0 2 9000 fast.example.com.
_svc._tcp.example.com. IN SRV 0 1 9000 slow.example.com.
; 不推荐: 复杂比例 (1.73:1)
_svc._tcp.example.com. IN SRV 0 173 9000 fast.example.com.
_svc._tcp.example.com. IN SRV 0 100 9000 slow.example.com.
4.5.2 TTL 的选择 (TTL Selection)
静态 Weight 的 TTL 建议:
; 硬件很少变更 - 长 TTL
_service._tcp.example.com. 86400 IN SRV 0 100 8080 server1.example.com.
↑
24 小时
; 服务器经常调整 - 中等 TTL
_service._tcp.example.com. 3600 IN SRV 0 100 8080 server2.example.com.
↑
1 小时
; 避免: 动态负载 - 短 TTL (不推荐)
_service._tcp.example.com. 60 IN SRV 0 100 8080 server3.example.com.
↑
1 分钟 (会导致前述问题)
4.6 本章小结 (Chapter Summary)
Weight 字段的核心理念 (Core Philosophy):
- 静态性 (Static Nature): 反映硬件和基础设施的固有能力,而非动态负载
- 实用主义 (Pragmatism): 在 DNS 机制的限制下提供最佳解决方案
- 长期视角 (Long-Term Perspective): 配置应在较长时间内保持稳定
设计权衡 (Design Trade-offs):
优点:
✅ 简单且标准化
✅ 不增加额外的基础设施
✅ 利用现有 DNS 缓存机制
✅ 可靠性高
缺点:
❌ 无法反映动态负载
❌ 需要手动配置和维护
❌ 无法自动适应负载变化
替代方案的适用场景 (Alternative Approaches):
- 应用层负载均衡: Nginx, HAProxy, AWS ELB
- 智能 DNS: Amazon Route 53, Cloudflare Load Balancing
- 服务网格: Istio, Linkerd
- 客户端负载均衡: Ribbon, gRPC 内置负载均衡