# 功能开关

功能开关（feature toggle，也称功能标志、功能切换器或功能位）是一种机制，让团队能够在运行时开启或关闭某项软件功能，而无需修改代码或重新部署应用。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup><sup> • </sup><sup>[2](https://www.oreilly.com/library/view/feature-flag-best/9781492050452/ch01.html)</sup>开关将部署与发布分离：代码可以静默地存在于生产环境中而不做任何事，是否将其暴露给用户成为一个配置变更，而非工程事件。<sup>[3](https://launchdarkly.com/blog/deployment-strategies/)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心目的 | 将部署与发布解耦；已部署的代码可能并未运行，这支持主干开发和金丝雀发布<sup>[3](https://launchdarkly.com/blog/deployment-strategies/)</sup> |
| 主要类别 | 发布开关、实验开关、运维开关和权限开关（Hodgson）；学术研究增加了第五类——开发开关<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup><sup> • </sup><sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup> |
| 生命周期指导 | Fowler认为发布开关存活“一两周”；Unleash为发布和实验开关设定约40天的预算，运维开关约7天<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup><sup> • </sup><sup>[5](https://www.getunleash.io/blog/what-is-a-feature-toggle)</sup> |
| 规模 | Chromium有3.1K个开关；Microsoft Office约有12,000个活跃标志；Wayfair超过10,000个；据报道Google有150,000以上<sup>[6](https://conf.researchr.org/details/msr-2023/msr-2023-technical-papers/7/Feature-Toggle-Usage-Patterns-A-Case-Study-on-Google-Chromium)</sup><sup> • </sup><sup>[7](https://doi.org/10.1145/3540250.3558942)</sup><sup> • </sup><sup>[8](https://www.aboutwayfair.com/careers/tech-blog/how-wayfair-uses-feature-toggles-to-deliver-business-value)</sup><sup> • </sup><sup>[9](https://kubecon25.nicolai-ort.com/day3/02_open-feature/index.html)</sup> |
| 债务证据 | 53%的Chrome发布开关在10个发布版本后仍然存在；Kubernetes中移除滞后于新增约35%，GitLab约13%<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup><sup> • </sup><sup>[11](https://arxiv.org/html/2604.15872v1)</sup> |
| 事故价值 | Atlassian报告称利用发布标志和安全阀在数秒内从90%的事故中恢复<sup>[12](https://go.launchdarkly.com/rs/850-KKH-319/images/launchdarkly-oreilly-effective-feature-management.pdf)</sup> |
| 标准化 | OpenFeature是CNCF项目，提供供应商中立的标志SDK，可接入任何兼容的提供方<sup>[13](https://openfeature.dev/docs/reference/intro)</sup><sup> • </sup><sup>[14](https://www.dynatrace.com/news/blog/feature-flags-with-openfeature-and-dynatrace/)</sup> |

## 定义与核心概念

开关是代码中的条件分支，其结果由运行时评估的标志数据决定，通常通过SDK向标志管理系统询问当前用户或请求是否应开启该功能。实践者从两个维度对开关分类：开关的存活时长以及切换决策的动态程度。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>

开关与两个相邻概念不同。功能分支将未完成的代码隔离在单独的Git分支中以保护主代码库；而开关则将代码合并到主线，在运行时对用户隐藏。<sup>[15](https://docs.getunleash.io/get-started/what-is-a-feature-flag.md)</sup><sup> • </sup><sup>[16](https://www.statsig.com/perspectives/feature-branch-vs-feature-toggle)</sup>配置选项有独立的研究文献，与开关共享运行时决策机制，通常服务于长期的用户或操作者选择，而非交付控制；研究人员已将这两个概念放在一起研究，以便在它们之间转移测试技术等解决方案。<sup>[17](https://dl.acm.org/doi/10.1145/3377813.3381366)</sup>

## 开关的类别及其生命周期

Pete Hodgson在Fowler文章中广泛使用的分类法按生命周期和动态性将开关分为四类。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>

- **发布开关**让未完成或有风险的代码暗发布，并逐步启用。它们是过渡性的，“通常不应比一两周长太多”，尽管以产品为中心的开关可能存留更久。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>
- **实验开关**通过将每个用户放入一个群组并在运行时将其路由到一条代码路径来执行A/B或多变量测试。它们必须存留足够长的时间以产生统计显著的结果，根据流量大小可能需要数小时或数周。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>
- **运维开关**是长期存在的紧急关闭开关，让操作员在高负载下禁用或降级非关键功能，充当手动管理的断路器。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>
- **权限开关**按用户控制高级功能的访问；它们可以存活数年，且在每次请求时都被评估，因此高度动态。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>

实证研究扩展了这一方案。Rahman等人在[Google Chrome](https://www.edgepedia.org/zh/google-chrome)中区分了开发开关、长期业务开关和发布开关，ESEM 2019的一项调查提出了五种类型，增加了独立的开发开关；在该方案中，发布和实验开关是短命的，而权限、运维和开发开关是长命的。<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup><sup> • </sup><sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup>生命周期指导因来源而异：Fowler关于发布开关一两周的规则与Unleash为发布和实验开关设定约40天、运维开关约7天的预算形成对比，权限开关和紧急关闭开关允许永久存在。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup><sup> • </sup><sup>[5](https://www.getunleash.io/blog/what-is-a-feature-toggle)</sup>实测寿命也因项目而异：一项纵向研究发现Kubernetes中开关寿命中位数为734天，而GitLab为185天，分别约有1.33%和0.73%的开关事实上成为永久性的。<sup>[11](https://arxiv.org/html/2604.15872v1)</sup>

## 开关评估的工作方式

在典型架构中，应用嵌入标志SDK，提供方作为标志值和规则的权威来源；提供方可以是SaaS供应商、自建内部基础设施或开源实现。<sup>[18](https://github.com/open-feature/spec/blob/main/specification/glossary.md)</sup>在某些实现中，SDK针对给定上下文批量获取已评估的标志并缓存它们；当上下文变化时，例如用户登录时，缓存的评估可能失效并需要重新评估，通常涉及网络请求或I/O操作。<sup>[18](https://github.com/open-feature/spec/blob/main/specification/glossary.md)</sup>OpenFeature评估API形式化了这一流程，钩子让团队能够在评估生命周期的各个点附加验证、遥测和日志记录等行为。<sup>[13](https://openfeature.dev/docs/reference/intro)</sup>

**发布机制。** 渐进式发布将功能暴露给一小部分流量并随时间增加，这是一种降低风险的金丝雀或分阶段发布。Unleash的实现对用户ID使用一致性哈希，使每个用户跨会话看到相同的体验，从5%这样的小百分比开始，逐步提升到25%、50%和100%。<sup>[5](https://www.getunleash.io/blog/what-is-a-feature-toggle)</sup>定向规则还可以按用户、群组或属性控制访问，基于时间的激活可以在设定时间开启标志而无需新部署。<sup>[19](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-feature-management)</sup>Azure App Configuration将标志使用分为三种场景：切换、发布和[实验](https://www.edgepedia.org/zh/experiment)，其遥测报告评估次数、独立用户和变体分布，指标记分卡揭示变体之间统计显著的差异。<sup>[19](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-feature-management)</sup>

## 在开发流程中的作用

功能开关是使主干开发变得可行的一种机制：实践者向主分支贡献代码，将未完成的工作隐藏在开关后面，同时保持项目始终可发布。Neely和Stolt（2013）报告称，这种做法减少了合并长期分支的痛苦和风险。<sup>[20](https://das.encs.concordia.ca/pdf/Hoyos2021.pdf)</sup>由于已部署的代码无需运行，团队还可以使用标志进行金丝雀发布和分阶段发布。<sup>[3](https://launchdarkly.com/blog/deployment-strategies/)</sup>包括Google、Facebook和Netflix在内的大公司使用开关来增量集成和测试新功能，而不是仅在功能就绪时才集成。<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup>

Chrome的流程展示了这种模式在规模上的运作。在研究期间，每6周从开发主干创建一个新的发布稳定分支，Chrome在主干上启用所有实验性功能，因此那里只需要测试一种配置。<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup>

**测试问题。** 测试标志状态的每种组合会产生测试爆炸，这是实践者明确表达的担忧。Fowler的建议是只测试生产环境中实际会使用的组合，LaunchDarkly手册建议单独测试每个标志变体，其他标志使用默认值，除非存在已知的交互。<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup><sup> • </sup><sup>[12](https://go.launchdarkly.com/rs/850-KKH-319/images/launchdarkly-oreilly-effective-feature-management.pdf)</sup>对于发布标志，Fowler的bliki认为两种组合通常就足够了。<sup>[21](https://www.martinfowler.com/bliki/FeatureFlag.html)</sup>然而Open edX项目警告说，开关通过多条代码路径和配置操作本质上增加了复杂性，可能导致测试排列爆炸和被遗忘的潜在代码路径，并建议在开关开启和关闭两种状态下都运行测试，除非保证某个标志在生产环境中永远不会启用。<sup>[22](https://docs.openedx.org/projects/openedx-proposals/en/latest/best-practices/oep-0017-bp-feature-toggles.html)</sup>

## 数据一览

大型组织中的标志数量从数千到数十万不等，没有一个单一数字可以概括：

- 在2023年的一项研究中，Chromium在38个组件中包含3.1K个开关；在中位情况下，嵌套开关被5个文件共享，分散开关跨越2个组件，死开关平均覆盖4行代码。<sup>[6](https://conf.researchr.org/details/msr-2023/msr-2023-technical-papers/7/Feature-Toggle-Usage-Patterns-A-Case-Study-on-Google-Chromium)</sup>
- [Microsoft Office](https://www.edgepedia.org/zh/microsoft-office)约有12,000个活跃标志，使得穷举组合测试不可行；对标志查询日志的概率推理以超过90%的精度推断了相互依赖关系。<sup>[7](https://doi.org/10.1145/3540250.3558942)</sup>
- Wayfair报告生产环境中有超过10,000个活跃开关。<sup>[8](https://www.aboutwayfair.com/careers/tech-blog/how-wayfair-uses-feature-toggles-to-deliver-business-value)</sup>
- 据报道Google维护着超过150,000个功能标志，部分归因于人们忘记关闭它们；该数据来自会议演讲笔记，没有更强的已发表来源涵盖此数据。<sup>[9](https://kubecon25.nicolai-ort.com/day3/02_open-feature/index.html)</sup>
- 一项对[Kubernetes](https://www.edgepedia.org/zh/kubernetes)（1000万行代码，8.5年）和GitLab（500万行代码，5年）的纵向研究分析了4,000多个开关事件：Kubernetes新增了603个功能门，其中448个被移除，GitLab新增了3,442个标志，其中3,038个被移除。<sup>[11](https://arxiv.org/html/2604.15872v1)</sup>
- 一项由Atlassian委托的调查发现，28%的组织已实施功能标志，40%已开始实施，后一组中每个应用平均有30个标志。<sup>[23](https://wac-cdn.atlassian.com/dam/jcr:4eaee3a8-1e5e-4741-9466-32d53f0a8ac9/Rising.The_.Flag_.Rollout-1.pdf?cdnVersion=3467)</sup>

Facebook的Gatekeeper系统展示了大规模实验：Andrew Bosworth在2012年解释说，Facebook每天运行数百项测试，使用Gatekeeper防止测试冲突并获得统计上有意义的结果。<sup>[20](https://das.encs.concordia.ca/pdf/Hoyos2021.pdf)</sup>有记载的系统包括Facebook的Gatekeeper、Behalf和[CircleCI](https://www.edgepedia.org/zh/circleci)使用的LaunchDarkly、Envoy使用的Split，以及Microsoft内部的开关实验。<sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup>

## 管理与技术债务

开关带有持有成本。Fowler的指导是，精明的团队将开关视为库存，并努力保持库存尽可能低，因为标志增加了抽象、条件逻辑和测试负担。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>实证记录证实了这一风险：尽管发布开关是寿命最短的类别，但Chrome中53%的发布开关在10个发布版本后仍然存在，表明许多作为技术债务滞留。<sup>[10](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)</sup>在Kubernetes和GitLab的研究中，移除分别滞后于新增约35%和13%，因此库存随时间增长。<sup>[11](https://arxiv.org/html/2604.15872v1)</sup>

**库存实践。** 有记载的对策包括在创建时为每个开关分配负责人、设定预期移除日期、维护开关库存或审计日志，以及通过问题单、标签、命名约定或预先创建的拉取请求为标志设置过期日期。<sup>[24](https://www.flagsmith.com/blog/feature-toggle-management)</sup><sup> • </sup><sup>[12](https://go.launchdarkly.com/rs/850-KKH-319/images/launchdarkly-oreilly-effective-feature-management.pdf)</sup>一些团队创建“定时炸弹”，如果标志存活超过其过期日期，则使测试失败，甚至拒绝启动应用。<sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup>GitHub构建了一个脚本，可在本地或通过工作流调用，自动化清理完全启用的标志留下的死代码和旧测试。<sup>[25](https://github.blog/engineering/infrastructure/ship-code-faster-safer-feature-flags/)</sup>GitLab定义了最大寿命，从标志合并到主分支时算起，而非从GitLab.com上全局启用时算起。<sup>[26](https://handbook.gitlab.com/handbook/engineering/architecture/design-documents/feature_flags_usage_in_dev_and_ops/)</sup>学术工作提出了五阶段生命周期：决策、设计、实现、存在和清理，将移除作为明确的最终阶段。<sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup>

**Knight Capital的失败。** 经典的警示故事是2012年的Knight Capital。开发人员无意中改变了一个功能开关的用途，激活了8年未使用的功能；在2分钟内他们意识到部署的代码行为不正确，但花了45分钟才停止系统。<sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup>来源对损失的描述不一致：基于SEC的记述认为30分钟内损失4.65亿美元，Fowler引用的是4.6亿美元的错误，一项同行评审的研究称近4亿美元。<sup>[27](https://www.infoq.com/articles/feature-flags-gone-wrong/)</sup><sup> • </sup><sup>[1](https://martinfowler.com/articles/feature-toggles.html)</sup><sup> • </sup><sup>[4](https://ar5iv.labs.arxiv.org/html/1907.06157)</sup>

## 工具格局与标准化

平台分为四种模式：托管SaaS、开源或可自托管、标准项目和自建内部系统。比较标准包括SaaS与自托管、定向和细分、治理（如基于角色的访问控制、审计日志和变更审批）、SDK覆盖范围、对OpenFeature的支持，以及通常按月度活跃用户、席位或开关数量计费的定价。<sup>[24](https://www.flagsmith.com/blog/feature-toggle-management)</sup>Unleash自称是GitHub上最受欢迎的开源功能标志解决方案，拥有15个官方客户端和服务器SDK以及超过15个社区SDK，支持定向发布、金丝雀发布、紧急关闭开关、[A/B测试](https://www.edgepedia.org/zh/a-b-testing)以及管理陈旧标志的洞察。<sup>[28](https://github.com/Unleash/unleash/)</sup>

OpenFeature是[Dynatrace](https://www.edgepedia.org/zh/dynatrace)和一群标志供应商提交的CNCF项目，提供供应商中立的规范，旨在减少切换标志后端时的应用代码变更；它默认提供基于OpenTelemetry的可观测性。<sup>[14](https://www.dynatrace.com/news/blog/feature-flags-with-openfeature-and-dynatrace/)</sup>其共享SDK可接入第三方提供方，无论是开源还是商业、自托管还是云托管。<sup>[13](https://openfeature.dev/docs/reference/intro)</sup>

## 近期发展

三项发展标志着2023年之后的时期。第一，OpenFeature标准化已从提案转变为生态系统基础设施，多个平台在支持自有SDK的同时支持该规范。<sup>[14](https://www.dynatrace.com/news/blog/feature-flags-with-openfeature-and-dynatrace/)</sup><sup> • </sup><sup>[24](https://www.flagsmith.com/blog/feature-toggle-management)</sup>第二，成熟平台继续采用标志机制：Kubernetes于2024年8月将功能门引入client-go，扩展了让功能通过Alpha、Beta和GA阶段逐步毕业的开关机制。<sup>[29](https://kubernetes.io/blog/2024/08/12/feature-gates-in-client-go/)</sup>第三，新的交付模式已经出现：2026年5月Cloudflare推出了Flagship，这是一个基于OpenFeature构建的边缘原生功能标志服务，在[Cloudflare Workers](https://www.edgepedia.org/zh/cloudflare-workers)中本地评估标志，而非调用外部标志服务；2026年9月InfoQ报道DoorDash使用多智能体LLM清理了60,000个功能标志。<sup>[30](https://www.infoq.com/feature_toggle/)</sup>

## References

1. [Pete Hodgson / Martin Fowler. Feature Toggles (aka Feature Flags).](https://martinfowler.com/articles/feature-toggles.html)
2. [Feature Flag Best Practices, O'Reilly, Chapter 1.](https://www.oreilly.com/library/view/feature-flag-best/9781492050452/ch01.html)
3. [6 Deployment Strategies (and How to Choose the Best for You), LaunchDarkly.](https://launchdarkly.com/blog/deployment-strategies/)
4. [Software Development with Feature Toggles: Practices used by Practitioners (ESEM 2019).](https://ar5iv.labs.arxiv.org/html/1907.06157)
5. [What is a feature toggle? Unleash.](https://www.getunleash.io/blog/what-is-a-feature-toggle)
6. [Feature Toggle Usage Patterns: A Case Study on Google Chromium (MSR 2023).](https://conf.researchr.org/details/msr-2023/msr-2023-technical-papers/7/Feature-Toggle-Usage-Patterns-A-Case-Study-on-Google-Chromium)
7. [Discovering feature flag interdependencies in Microsoft Office (FSE 2022).](https://doi.org/10.1145/3540250.3558942)
8. [How Wayfair Uses Feature Toggles to Deliver Business Value.](https://www.aboutwayfair.com/careers/tech-blog/how-wayfair-uses-feature-toggles-to-deliver-business-value)
9. [Type-safe feature flagging in OpenFeature: Lessons learned from using feature flags at Google (KubeCon EU 2025 talk notes).](https://kubecon25.nicolai-ort.com/day3/02_open-feature/index.html)
10. [Rahman et al. Feature Toggles: Practitioner Practices and a Case Study (MSR 2016).](https://users.encs.concordia.ca/~pcr/paper/Rahman2016MSR.pdf)
11. [Feature Toggle Dynamics in Large-Scale Systems: Prevalence, Growth, Lifespan, and Benchmarking.](https://arxiv.org/html/2604.15872v1)
12. [Effective Feature Management, O'Reilly report (LaunchDarkly).](https://go.launchdarkly.com/rs/850-KKH-319/images/launchdarkly-oreilly-effective-feature-management.pdf)
13. [Introduction, OpenFeature documentation.](https://openfeature.dev/docs/reference/intro)
14. [Feature flags with OpenFeature and Dynatrace.](https://www.dynatrace.com/news/blog/feature-flags-with-openfeature-and-dynatrace/)
15. [What is a feature flag and why are feature flags used? Unleash docs.](https://docs.getunleash.io/get-started/what-is-a-feature-flag.md)
16. [Feature Branch vs Feature Toggle, Statsig.](https://www.statsig.com/perspectives/feature-branch-vs-feature-toggle)
17. [Exploring differences and commonalities between feature flags and configuration options (ICSE-SEIP 2020).](https://dl.acm.org/doi/10.1145/3377813.3381366)
18. [OpenFeature specification glossary.](https://github.com/open-feature/spec/blob/main/specification/glossary.md)
19. [Understand feature management using Azure App Configuration, Microsoft Learn.](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-feature-management)
20. [Hoyos et al. On the Removal of Feature Toggles.](https://das.encs.concordia.ca/pdf/Hoyos2021.pdf)
21. [FeatureFlag, Martin Fowler bliki.](https://www.martinfowler.com/bliki/FeatureFlag.html)
22. [OEP-17: Feature Toggles, Open edX.](https://docs.openedx.org/projects/openedx-proposals/en/latest/best-practices/oep-0017-bp-feature-toggles.html)
23. [Raising the Flag, Atlassian-commissioned survey.](https://wac-cdn.atlassian.com/dam/jcr:4eaee3a8-1e5e-4741-9466-32d53f0a8ac9/Rising.The_.Flag_.Rollout-1.pdf?cdnVersion=3467)
24. [Feature Toggle Management: A Practical Guide, Flagsmith.](https://www.flagsmith.com/blog/feature-toggle-management)
25. [How we ship code faster and safer with feature flags, GitHub.](https://github.blog/engineering/infrastructure/ship-code-faster-safer-feature-flags/)
26. [Feature Flags usage in GitLab development and operations, GitLab handbook.](https://handbook.gitlab.com/handbook/engineering/architecture/design-documents/feature_flags_usage_in_dev_and_ops/)
27. [When Feature Flags Go Wrong, InfoQ.](https://www.infoq.com/articles/feature-flags-gone-wrong/)
28. [Unleash/unleash, GitHub repository.](https://github.com/Unleash/unleash/)
29. [Introducing Feature Gates to Client-Go, Kubernetes blog (August 2024).](https://kubernetes.io/blog/2024/08/12/feature-gates-in-client-go/)
30. [Feature Toggle news, InfoQ.](https://www.infoq.com/feature_toggle/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software design and architecture › Titles 4 to Mo

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/feature-toggle

English original: https://www.edgepedia.org/feature-toggle
