# 故障转移

故障转移（Failover）是将操作从故障的主组件自动切换到冗余备用组件的机制，使服务无需人工干预即可继续运行；由操作员执行的同一操作称为切换（switchover）。主备协议由三个成本指标刻画：复制度、阻塞时间和故障转移时间；故障转移时间定义为由于没有主节点而可能丢失请求的最坏情况时段。<sup>[1](https://www.cs.cornell.edu/fbs/publications/DSbook.c8.pdf)</sup> 故障转移用于各类高可用基础设施，从数据库集群到负载均衡器和多区域云部署。

| 关键事实 | 值 |
|---|---|
| 保证 | 服务在备用节点上继续运行；故障转移时间是无主节点的最坏情况窗口<sup>[1](https://www.cs.cornell.edu/fbs/publications/DSbook.c8.pdf)</sup> |
| 经典检测 | Tandem/16 每 1 秒发送一条“我还活着”消息，并在 2 秒未收到后宣布处理器宕机<sup>[2](https://tcm.computerhistory.org/ComputerTimeline/Chap29_tandem16_CS2.pdf)</sup> |
| 托管云数据库 RTO | Amazon RDS 多可用区（Multi-AZ）故障转移通常可在 35 秒内完成<sup>[3](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts-failover.html)</sup> |
| Kubernetes 检测 | 在默认控制器设置下，主节点在其节点变为不可达后约 40 至 55 秒被观测为未就绪<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup> |
| 灾难恢复层级 | AWS 策略从备份/恢复（RPO 以小时计，RTO 为 24 小时或更短）到多区域双活（RPO 接近零，RTO 可能为零）<sup>[5](https://docs.aws.amazon.com/wellarchitected/2023-04-10/framework/rel_planning_for_recovery_disaster_recovery.html)</sup> |
| 容量成本 | Uber 的故障转移架构将稳态配置从 2 倍降至 1.3 倍，同时维持 99.97% 的可用性<sup>[6](https://www.usenix.org/system/files/nsdi26-bansal.pdf)</sup> |
| 脑裂防御 | 在 pg_auto_failover 中，主节点的守护进程在备用节点被提升之前停止旧主节点上的 PostgreSQL，以避免脑裂<sup>[7](https://github.com/citusdata/pg_auto_failover/blob/v1.6.3/docs/fault-tolerance.rst)</sup><sup> • </sup><sup>[30](https://pg-auto-failover.readthedocs.io/en/main/failover-state-machine.html)</sup> |

## 工作原理

**检测**是触发器。遗留系统使用心跳：Tandem 处理器每秒交换“我还活着”消息，并在静默两秒后假定处理器宕机<sup>[2](https://tcm.computerhistory.org/ComputerTimeline/Chap29_tandem16_CS2.pdf)</sup>；Patroni 每 10 秒向 Etcd、Consul 或 ZooKeeper 等分布式键值存储写入心跳，错过这些心跳的节点会从集群中移除。<sup>[8](https://stormatics.tech/blogs/understanding-patroni-failovers)</sup> 当主节点的就绪探针失败时间超过 `.spec.failoverDelay` 时，CloudNativePG 会触发故障转移。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup>

随后是选举与围栏。Patroni 通过配置存储中的领导者键进行协调：旧领导者降级其 Postgres 并释放该键，新领导者取得该键并发出提升指令。<sup>[9](https://www.nutanix.dev/2023/07/25/postgresql-high-availability-under-the-hood/)</sup> CloudNativePG 用 [Kubernetes](https://www.edgepedia.org/zh/kubernetes) 租约支持选举，实例在提升前必须持有该租约；由于租约无法对失去 API 服务器连接但仍在运行的主节点施加围栏，一个补充的主节点隔离检查会阻止被隔离的主节点接受写入。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup> pg_auto_failover 在备用节点接受写入之前降级主节点，而一个与监视器和辅助节点都断开超过 30 秒的主节点会自行关闭。<sup>[7](https://github.com/citusdata/pg_auto_failover/blob/v1.6.3/docs/fault-tolerance.rst)</sup><sup> • </sup><sup>[10](https://pg-auto-failover.readthedocs.io/en/v2.0/failover-state-machine.html)</sup> 在提升之前，CloudNativePG 的法定人数检查应用 Dynamo 规则：在固定成员关系和严格读/写法定人数下，若 \( R + W > N \)，则每个读法定人数都与每个写法定人数相交；然而，仅凭这一条件并不能确立安全的 CloudNativePG 提升，也不能保证至少有一个可提升副本持有所有同步提交。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup> [莫尼·纳奥尔](https://www.edgepedia.org/zh/moni-naor)（Moni Naor）和阿维沙伊·伍尔（Avishai Wool）从负载、容量和可用性角度分析了作为可用性抽象的法定人数系统，该文于 1998 年发表在《SIAM 计算杂志》（SIAM Journal on Computing）上<sup>[11](https://doi.org/10.1137/s0097539795281232)</sup>；共识方法则源自[莱斯利·兰波特](https://www.edgepedia.org/zh/leslie-lamport)（Leslie Lamport）1998 年发表在《ACM 计算机系统汇刊》（ACM Transactions on Computer Systems）上的论文《兼职议会》（The Part-Time Parliament）中描述的 Paxos 算法。<sup>[12](https://doi.org/10.1145/279227.279229)</sup>

## 如何实施

实践者配置备用节点、调整检测、配置路由与持久性，并测试结果。持久性通过 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 的 `synchronous_commit` 设置：异步提交不等待备用节点确认，而同步提交至少等待一个备用节点，从而以性能为代价降低 RPO。<sup>[13](https://docs.hashicorp.com/terraform/enterprise/v202507-1/deploy/manage/failover)</sup> 路由必须跟随提升：AWS 建议将 JVM DNS TTL 配置为不超过 60 秒，以便应用在 RDS 故障转移后获取变更后的记录。<sup>[3](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts-failover.html)</sup> 最后，故障转移要经过演练：一个五阶段测试模型（检测、选举、路由、恢复、证据）在同步/半同步复制下以 60 秒 RTO 目标和 RPO 0 为目标，测得端到端 RTO 为 32 秒。<sup>[14](https://www.mydbops.com/blog/how-to-set-up-automated-database-failover-testing)</sup> 观测到的故障转移时间差异很大：Tandem 的接管给出的平均修复时间以毫秒计<sup>[15](https://jimgray.azurewebsites.net/papers/TandemTR86.2_FaultToleranceInTandemComputerSystems.pdf)</sup>，一个经过调优的 Patroni + HAProxy + Etcd 实验室观测到约 5 秒的客户端停机时间<sup>[9](https://www.nutanix.dev/2023/07/25/postgresql-high-availability-under-the-hood/)</sup>，RDS 多可用区通常可在 35 秒内完成<sup>[3](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts-failover.html)</sup>，而仅 Kubernetes 检测就消耗 40 至 55 秒。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup> 已发表的 Patroni 测量结果并不一致：[Nutanix](https://www.edgepedia.org/zh/nutanix) 实验室观测到约 5 秒的客户端停机时间<sup>[9](https://www.nutanix.dev/2023/07/25/postgresql-high-availability-under-the-hood/)</sup>，而 HashiCorp 在 Terraform Enterprise 上使用 Patroni 进行的注入式故障转移测试测得 RTO 为 2:18 至 4:56；这一分歧尚未解决。<sup>[16](https://docs.hashicorp.com/terraform/enterprise/deploy/configuration/storage/connect-database/failover-resilience)</sup>

## 起源

这套词汇比计算的商业时代更古老：一份 1957 年的会议论文集描述了同时具有紧急切换（故障转移）和为维护而设的计划故障转移的[计算](https://www.edgepedia.org/zh/computing)机系统；“failover”一词本身可在 1962 年一份解密的 NASA 报告中找到。<sup>[17](https://handwiki.org/wiki/Failover)</sup> 20 世纪 60 年代，容错技术主要用于电话交换和航空航天；贝尔系统 1 号电子交换系统（Bell System No. 1 ESS）使用双份中央处理器，其中备用处理器接管控制以提供连续电话服务，设计目标是每年停服不超过几分钟。<sup>[18](https://tcm.computerhistory.org/ComputerTimeline/Chap28_ESS_CS2.pdf)</sup><sup> • </sup><sup>[19](https://research.cs.wisc.edu/areas/os/Qual/papers/ha-systems.pdf)</sup> 20 世纪 70 年代出现了使用进程对与复制相结合的商用容错系统：<sup>[19](https://research.cs.wisc.edu/areas/os/Qual/papers/ha-systems.pdf)</sup> NonStop I 是一套商用容错计算机系统<sup>[15](https://jimgray.azurewebsites.net/papers/TandemTR86.2_FaultToleranceInTandemComputerSystems.pdf)</sup>，运行一个主进程，将状态变化以检查点形式写入另一处理器中的备份，以便备份可以接管。<sup>[2](https://tcm.computerhistory.org/ComputerTimeline/Chap29_tandem16_CS2.pdf)</sup> 克里斯蒂安（Cristian）1991 年的综述描述了此类架构如何通过自动提升备份来掩盖主节点故障，并比较了 Tandem、VAX Cluster、IBM XRF、Stratus 和 Sequoia。<sup>[20](https://eecs.wsu.edu/~bakken/562-files/cristian-ftds-CACM91.pdf)</sup>

## 变体

备用深度是分层的。Azure 区分以下层级：可随时接受生产流量的热备用、需要一些配置或扩缩容的温备用、以最小配置部分部署的长明灯备用，以及可能完全未部署的冷备用。<sup>[21](https://docs.azure.cn/en-us/reliability/concept-failover-failback)</sup> AWS 量化了这种权衡：备份/恢复的 RPO 以小时计，RTO 为 24 小时或更短（时间点恢复可将 RPO 降至最低 5 分钟）；长明灯，RPO 以分钟计，RTO 以数十分钟计；温备用，一个缩容但功能完整的副本始终运行，RPO 以秒计，RTO 以分钟计（扩容到完整规模后即为热备用）；以及多区域双活，RPO 接近零，RTO 可能为零。<sup>[5](https://docs.aws.amazon.com/wellarchitected/2023-04-10/framework/rel_planning_for_recovery_disaster_recovery.html)</sup> 在主动-被动设计中，只有一个集群服务流量：Azure 的 AKS 模式在两个区域部署两个独立集群，除非由 Azure Front Door 引导，否则辅助集群不接受流量。<sup>[22](https://learn.microsoft.com/en-us/azure/aks/active-passive-solution)</sup> 自动故障转移需要某种机制检测不可用性，通常是一个带宽限期的健康检查；故障转移是自动还是手动，是风险容忍度的选择。<sup>[21](https://docs.azure.cn/en-us/reliability/concept-failover-failback)</sup>

## 应用

数据库是使用最密集的用户。当内部监控检测到写入器不健康时，Amazon RDS 多可用区会在不同的可用区提升一个读取器实例。<sup>[3](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts-failover.html)</sup> MySQL 组复制（Group Replication）在故障检测后选举新的主节点<sup>[23](https://dev.mysql.com/doc/refman/26.7/en/group-replication-failure-detection.html)</sup>，而 PostgreSQL 在 CloudNativePG 和 pg_auto_failover 等 Operator 下运行。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup><sup> • </sup><sup>[10](https://pg-auto-failover.readthedocs.io/en/v2.0/failover-state-machine.html)</sup> 网络硬件也会故障转移：四层/七层交换机通常成对出现，支持热故障转移，一台交换机自动接管另一台，通过监控打开的 TCP 连接来检测宕机节点。<sup>[24](https://people.cs.rutgers.edu/~rmartin/teaching/spring03/cs553/readings/brewer01.pdf)</sup> Kubernetes 平台通过 AKS 主动-被动等模式实现区域故障转移<sup>[22](https://learn.microsoft.com/en-us/azure/aks/active-passive-solution)</sup>，云灾难恢复策略跨区域应用同样的机制。<sup>[5](https://docs.aws.amazon.com/wellarchitected/2023-04-10/framework/rel_planning_for_recovery_disaster_recovery.html)</sup>

## 局限与替代方案

**脑裂**是典型的故障：网络分区使两个节点都充当主节点，接受写入并产生分歧；法定人数或围栏令牌可防止这种情况。<sup>[25](https://quality.arc42.org/approaches/standby-failover)</sup> 实践者指南称围栏必不可少，并列出方法：在法定人数存储中使用短 TTL 的基于租约的领导权、存储级写锁，以及旧主节点的网络隔离。<sup>[26](https://thelinuxcode.com/failover-mechanisms-in-system-design-production-patterns-that-survive-real-incidents/)</sup> CloudNativePG 和 pg_auto_failover 都实现了这种选举加上旧主节点降级或隔离的配对。<sup>[4](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)</sup><sup> • </sup><sup>[10](https://pg-auto-failover.readthedocs.io/en/v2.0/failover-state-machine.html)</sup> **抖动**是不稳定的主节点触发反复提升；可用迟滞缓解，例如在 \( p_{95} > 500 \) 毫秒时故障转移，但要求 \( p_{95} < 250 \) 毫秒持续 5 分钟才切回。<sup>[25](https://quality.arc42.org/approaches/standby-failover)</sup><sup> • </sup><sup>[26](https://thelinuxcode.com/failover-mechanisms-in-system-design-production-patterns-that-survive-real-incidents/)</sup> **误报**代价高昂，因为每次不必要的故障转移都是自伤事故；LogScale 默认的 180 秒故障转移前验证就是为了防止误触发故障转移。<sup>[26](https://thelinuxcode.com/failover-mechanisms-in-system-design-production-patterns-that-survive-real-incidents/)</sup><sup> • </sup><sup>[27](https://library.humio.com/reference-architectures/dr-oci-operations-dr-failover-timing.html)</sup> 其他有记录的模式包括复制延迟导致的陈旧备用节点，以及半同步复制静默回退到异步模式并提升陈旧副本。<sup>[25](https://quality.arc42.org/approaches/standby-failover)</sup><sup> • </sup><sup>[14](https://www.mydbops.com/blog/how-to-set-up-automated-database-failover-testing)</sup> 还存在检测下界：对于带有周期性存活消息的崩溃故障，故障转移时间趋近于检测超时 f，在附加协议属性下这也是一个下界。<sup>[1](https://www.cs.cornell.edu/fbs/publications/DSbook.c8.pdf)</sup>

故障转移是若干可用性机制之一，并且它以备用容量为前提。布鲁尔（Brewer）对超大规模服务的分析指出，副本组中五个节点失去两个后，剩余三个节点将承担正常负载的 166%，因此仅有复制而没有多余容量是不够的。<sup>[24](https://people.cs.rutgers.edu/~rmartin/teaching/spring03/cs553/readings/brewer01.pdf)</sup> 对于重定向流量，基于 DNS 的故障转移可能响应缓慢，由于生存时间设置，最多可达数小时；这就是为什么能自行重定向的智能客户端被描述为灾难容忍最有吸引力的替代方案。<sup>[24](https://people.cs.rutgers.edu/~rmartin/teaching/spring03/cs553/readings/brewer01.pdf)</sup> 在超大规模下，统一的故障转移容量正被分层服务级别目标取代：Uber 的故障转移架构仅通过预留的专用 CPU 容量为最关键的服务提供即时故障转移，允许 T3-T5 服务接受最长 1 小时的中断，并将稳态配置从 2 倍降至 1.3 倍，将利用率从约 20% 提高到约 30%，同时维持 99.97% 的可用性。<sup>[6](https://www.usenix.org/system/files/nsdi26-bansal.pdf)</sup>

可用性管理也正从被动故障转移转向预测：一篇关于高可用系统 AI 的综述描述了从仅在故障后激活冗余，转向能预判故障的异常检测与时间序列预测的转变，并报告了 MTBF 和 RTO 的改进<sup>[28](https://www.mdpi.com/2073-431X/15/4/231)</sup>；一个使用编码器-解码器 LSTM 处理 CPU 利用率和网络延迟序列的云灾难恢复模型实现了分钟级 RTO。<sup>[29](https://link.springer.com/article/10.1186/s13677-026-00846-0)</sup>

## References

1. [Principles of Computer System Design / Distributed Systems chapter 8: The Primary Backup Approach (Birman & van Renesse)](https://www.cs.cornell.edu/fbs/publications/DSbook.c8.pdf)
2. [The Tandem 16 NonStop System (Katzman, 1977 reprint)](https://tcm.computerhistory.org/ComputerTimeline/Chap29_tandem16_CS2.pdf)
3. [Failing over a Multi-AZ DB cluster for Amazon RDS (AWS documentation)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts-failover.html)
4. [Failover, CloudNativePG documentation](https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/failover/)
5. [Planning for recovery (AWS Well-Architected Framework)](https://docs.aws.amazon.com/wellarchitected/2023-04-10/framework/rel_planning_for_recovery_disaster_recovery.html)
6. [Uber's Failover Architecture: Reconciling Reliability and Efficiency in Hyperscale Microservice Infrastructure (NSDI '26)](https://www.usenix.org/system/files/nsdi26-bansal.pdf)
7. [pg_auto_failover: Failover and Fault Tolerance (docs/fault-tolerance.rst, v1.6.3)](https://github.com/citusdata/pg_auto_failover/blob/v1.6.3/docs/fault-tolerance.rst)
8. [Understanding Patroni Failovers (Chris Travers, Stormatics, May 8, 2024)](https://stormatics.tech/blogs/understanding-patroni-failovers)
9. [PostgreSQL High Availability: Under the Hood (Patroni + HAProxy case study)](https://www.nutanix.dev/2023/07/25/postgresql-high-availability-under-the-hood/)
10. [pg_auto_failover: The Failover State Machine](https://pg-auto-failover.readthedocs.io/en/v2.0/failover-state-machine.html)
11. [Moni Naor, Avishai Wool (1998). The Load, Capacity, and Availability of Quorum Systems. SIAM Journal on Computing.](https://doi.org/10.1137/s0097539795281232)
12. [Leslie Lamport (1998). The part-time parliament. ACM Transactions on Computer Systems.](https://doi.org/10.1145/279227.279229)
13. [PostgreSQL database failover | Terraform Enterprise](https://docs.hashicorp.com/terraform/enterprise/v202507-1/deploy/manage/failover)
14. [How to set up automated database failover testing](https://www.mydbops.com/blog/how-to-set-up-automated-database-failover-testing)
15. [Fault Tolerance in Tandem Computer Systems (Tandem TR 86.2)](https://jimgray.azurewebsites.net/papers/TandemTR86.2_FaultToleranceInTandemComputerSystems.pdf)
16. [Measure failover resilience | Terraform | HashiCorp Developer](https://docs.hashicorp.com/terraform/enterprise/deploy/configuration/storage/connect-database/failover-resilience)
17. [Failover - HandWiki](https://handwiki.org/wiki/Failover)
18. [Bell System Electronic Switching System (ESS) maintenance design (Bell Telephone Laboratories chapter)](https://tcm.computerhistory.org/ComputerTimeline/Chap28_ESS_CS2.pdf)
19. [High-availability computer systems (Gray and Siewiorek, IEEE Computer)](https://research.cs.wisc.edu/areas/os/Qual/papers/ha-systems.pdf)
20. [Understanding fault-tolerant distributed systems (Cristian, CACM 1991)](https://eecs.wsu.edu/~bakken/562-files/cristian-ftds-CACM91.pdf)
21. [Failover and failback (Azure Docs)](https://docs.azure.cn/en-us/reliability/concept-failover-failback)
22. [Recommended active-passive disaster recovery solution overview for Azure Kubernetes Service (AKS)](https://learn.microsoft.com/en-us/azure/aks/active-passive-solution)
23. [MySQL Reference Manual 20.1.4.2 Failure Detection](https://dev.mysql.com/doc/refman/26.7/en/group-replication-failure-detection.html)
24. [Lessons from Giant-Scale Services (Eric Brewer, IEEE Internet Computing, 2001)](https://people.cs.rutgers.edu/~rmartin/teaching/spring03/cs553/readings/brewer01.pdf)
25. [Standby/Failover (arc42 Quality Model)](https://quality.arc42.org/approaches/standby-failover)
26. [Failover Mechanisms in System Design: Production Patterns That Survive Real Incidents (TheLinuxCode)](https://thelinuxcode.com/failover-mechanisms-in-system-design-production-patterns-that-survive-real-incidents/)
27. [DR Failover Timing | LogScale Reference Architectures](https://library.humio.com/reference-architectures/dr-oci-operations-dr-failover-timing.html)
28. [Artificial Intelligence for High-Availability Systems: A Comprehensive Review](https://www.mdpi.com/2073-431X/15/4/231)
29. [Cloud disaster recovery model based on failure prediction (CDR-TSP)](https://link.springer.com/article/10.1186/s13677-026-00846-0)
30. [Failover State Machine, pg_auto_failover 2.2 documentation](https://pg-auto-failover.readthedocs.io/en/main/failover-state-machine.html)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Networking fundamentals and architecture

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/failover

English original: https://www.edgepedia.org/failover
