# Erlang（编程语言）

**Erlang** 是一种并发、容错的函数式编程语言及运行时系统，由爱立信（Ericsson）为电话交换机而创造，这类交换机的规格要求是每年停机时间至多四分钟，且软件升级无需停止系统<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。其进程由 Erlang 运行时系统而非操作系统创建和管理，不共享进程本地内存，通常通过消息传递交换数据<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。1993 年由 Joe Armstrong、Robert Virding 和 Mike Williams 合著的 Prentice Hall 教材将这门语言描述为适合大规模容错实时应用，具备不间断工业使用所必需的特性<sup>[2](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/3E436B38A9A927DC045CD0B1AB80DA46/S0956796800001519a.pdf/concurrent-programming-in-erlang-by-joe-armstrong-robert-virding-and-mike-williams-prentice-hall-1993-281-pp.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 1985 年中始于斯德哥尔摩的爱立信计算机科学实验室，面向每年停机至多四分钟的电话交换机<sup>[1](https://cacm.acm.org/research/erlang/)</sup> |
| 进程模型 | 由运行时管理的进程，拥有私有堆，不共享内存，异步消息传递；自 OTP 27 起默认上限为 1,048,576 个进程<sup>[1](https://cacm.acm.org/research/erlang/)</sup><sup> • </sup><sup>[3](https://www.erlang.org/downloads/27)</sup> |
| 容错 | “让它崩溃”：出错的进程立即崩溃；OTP 监督者检测故障，并按声明的策略重启它们<sup>[1](https://cacm.acm.org/research/erlang/)</sup><sup> • </sup><sup>[4](https://sujeet.pro/articles/whatsapp-erlang-scaling)</sup> |
| 调度 | 抢占式、基于归约计数的调度，具有软实时保证；通常防止 CPU 密集型的 Erlang 进程独占调度器<sup>[5](https://www.skillveris.com/study-notes/erlang/erlang-vs-elixir)</sup><sup> • </sup><sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup> |
| 可用性声明 | 据报道，用 Erlang 构建的某些高可用分布式电话系统达到了九个 9 的可用性，即每年约 31 毫秒的停机时间<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup> |
| 生产规模 | 2012 年 1 月，WhatsApp 在单台主机上达到 200 万个 TCP/IP 连接；2014 年 2 月，32 名工程师服务 4.5 亿月活用户<sup>[8](https://www.erlang-solutions.com/blog/twenty-years-of-open-source-erlang/)</sup><sup> • </sup><sup>[9](https://bytepith.com/article/how-whatsapp-hit-28m-connections-per-server-rebuilt-gorust)</sup> |
| 近期版本 | OTP 27：maybe 表达式、安全破坏性元组更新、JIT 覆盖率；OTP 28：优先级消息、EEP 69 名义类型、严格生成器与 zip 生成器<sup>[3](https://www.erlang.org/downloads/27)</sup><sup> • </sup><sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup> |

## 历史

Erlang 的故事始于 1985 年中，当时 Joe Armstrong 作为爱立信计算机科学实验室（位于斯德哥尔摩）的新员工，受命“在如何编写软件方面做点什么”。爱立信在构建容错电话交换机方面有着悠久传统，这类交换机的规格要求是每年停机时间至多四分钟，这一需求塑造了这门语言<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。

开放电信平台（OTP）的开发始于 1995 年底，1997 年 OTP 项目成为一个产品单元，正式对 Erlang 负责<sup>[11](https://dcatkth.github.io/thesis/joe_thesis.pdf)</sup>。1998 年 2 月，Erlang 在爱立信内部被禁止用于新产品开发；Armstrong 的论文指出，主要原因是爱立信希望成为软件技术的消费者而非生产者。1998 年 12 月，Erlang 和 OTP 库以开源许可证发布<sup>[11](https://dcatkth.github.io/thesis/joe_thesis.pdf)</sup>。Armstrong 后来在《CACM》上的叙述给出了不同的日期，称 Erlang 自 2000 年起以开源许可证自由提供<sup>[1](https://cacm.acm.org/research/erlang/)</sup>；而由这门语言的创造者在更接近事件发生时撰写的论文，给出的日期是 1998 年 12 月。开源后的最初几年采用缓慢，而由 Kenneth Lundin 领导的 OTP 团队继续开发<sup>[8](https://www.erlang-solutions.com/blog/twenty-years-of-open-source-erlang/)</sup>。

## 设计与核心概念

**进程，而非线程。** 进程的概念是根本性的。进程由 Erlang 运行时系统创建和管理，而非操作系统；单个进程不共享内存，通过消息传递交换数据，这简化了多核编程<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。在 BEAM 虚拟机上，Erlang 和 [Elixir](https://www.edgepedia.org/zh/elixir-programming-language) 都获得轻量级隔离进程，主要通过对进程邮箱的异步消息传递进行通信，每个进程拥有自己的私有堆和垃圾回收器<sup>[5](https://www.skillveris.com/study-notes/erlang/erlang-vs-elixir)</sup>。

**让它崩溃。** Erlang 的哲学是：无法完成被要求执行的任务的进程应立即崩溃，而其他进程检测到崩溃并修复它；这在 OTP 监督树中被形式化，链接机制提供了进程间错误检测和错误传播<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。用演员模型的话说，一个演员拥有完全局部的状态，仅通过消息交互，并能监督其他演员，检测故障并采取补救措施，例如重启失败的演员<sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup>。Armstrong 将这一想法追溯到早期电话中的“C 线”，接地 C 线会取消通话，并论证 Erlang“证明”了进程属于编程语言，而非操作系统<sup>[12](https://www.erlang-factory.com/upload/presentations/222/ErlangFactorySFBay2010-JoeArmstrong.pdf)</sup>。

**安全的动态类型。** Erlang 具有动态类型系统，其中不可能创建损坏的数据结构，且一个进程崩溃不会影响未链接的进程<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。

## BEAM 运行时

BEAM 虚拟机实现具有软实时保证的抢占式多任务，调度器公平地执行进程<sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup>。调度器基于归约计数而非协作式让出，在数百万个进程之间切换，通常防止 CPU 密集型的 Erlang 进程独占调度器<sup>[5](https://www.skillveris.com/study-notes/erlang/erlang-vs-elixir)</sup>。逐进程垃圾回收避免了全局停止世界式的垃圾回收暂停，热代码加载允许在不停止系统的情况下部署新代码<sup>[13](https://www.uptime.engineer/p/whatsapp-architecture)</sup>。

自 OTP 24（2021 年）起，BEAM 包含了即时（JIT）编译器，显著提升了 CPU 密集型任务的性能<sup>[14](https://cosmiclearn.com/erlang/history-of-erlang.php)</sup>。OTP 27 将默认进程上限提高到 1,048,576 个进程，并在编译器和运行时中实现了元组的安全破坏性更新，允许虚拟机在安全时原地更新元组，减少复制和垃圾<sup>[3](https://www.erlang.org/downloads/27)</sup>。OTP 28 增加了 erlang:hibernate/0，可将进程置于等待状态，内存占用极小，且不丢弃调用栈；在 100 万个大多空闲进程的基准测试中，内存使用从 2,687.1 MB 降至 827.9 MB，降幅超过 75%<sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup>。

跨节点分布使用相同的消息传递模型，容错通过机器检测崩溃机器的故障并利用足够的数据接管来实现，使用户察觉不到<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。存在可扩展性限制：欧盟 FP7 RELEASE 项目发现，大规模分布式 Erlang 系统必须偏离全连接分布式 Erlang 范式，例如不维护全连接的主机图<sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup>。

## 开放电信平台（OTP）

OTP 是一组设计模式和库，包括 gen_server、gen_statem、supervisor 和 application，用于构建监督树并实现“让它崩溃”哲学，即监督者重启失败的进程<sup>[5](https://www.skillveris.com/study-notes/erlang/erlang-vs-elixir)</sup>。当进程崩溃时，其监督者按照声明的策略重启它；WhatsApp 依靠这一点实现自愈，因此连接处理器可以崩溃并重启而不影响其他用户<sup>[4](https://sujeet.pro/articles/whatsapp-erlang-scaling)</sup>。

## 数字一览

- **进程创建。** 创建新的 Erlang 进程比大多数操作系统中的进程创建和大多数编程语言中的线程创建快几个数量级<sup>[11](https://dcatkth.github.io/thesis/joe_thesis.pdf)</sup>。BEAM 进程每个约占 0.5 KB，而 Go 中每个 goroutine 为 2 KB，隔离堆在进程结束时单独回收<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。
- **连接密度。** 2012 年 1 月，WhatsApp 宣布通过修改 FreeBSD 和 BEAM，在单台虚拟机和主机上实现了 200 万个 TCP/IP 连接，旨在降低运营成本<sup>[8](https://www.erlang-solutions.com/blog/twenty-years-of-open-source-erlang/)</sup>。
- **团队规模。** 2014 年 2 月，WhatsApp 有 32 名工程师服务 4.5 亿月活用户，其中约 10 名工程师直接接触 Erlang 服务器代码。广为流传的“50 名工程师服务 20 亿用户”的说法，在 2012 年和 2014 年的演讲以及涵盖这两次演讲的两篇 High Scalability 文章中均未出现。<sup>[9](https://bytepith.com/article/how-whatsapp-hit-28m-connections-per-server-rebuilt-gorust)</sup>
- **代码库规模。** 截至 2003 年，AXD301 电话交换机包含超过 170 万行 Erlang 代码，可能是当时用函数式风格编写的最大的系统，并被认为是爱立信有史以来最可靠的产品之一<sup>[11](https://dcatkth.github.io/thesis/joe_thesis.pdf)</sup>；Armstrong 在《CACM》上的叙述给出的数字是超过 160 万行<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。
- **可用性。** 据报道，用 Erlang 构建的某些分布式电话系统达到了九个 9 的可用性，即每年约 31 毫秒的停机时间<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。
- **服务器基准测试。** 在一项受控的 BEAM 比较中，极简的 Elixir 和 Erlang HTTP 处理器使用约 73 至 196 焦耳和 64 至 85 秒的运行时间，成功交付了约 80,000 个请求，而完整框架栈（Cowboy、Yaws、Phoenix、Mist）在同一步骤中仅完成约 56,500 至 57,000 个请求，使用约 171 至 207 焦耳和约 122 秒<sup>[15](https://dl.acm.org/doi/pdf/10.1145/3830434.3830942)</sup>。Erlang Cowboy 达到了最高的短时静态请求突发吞吐量，接近每秒 1,323 个请求，[WebSocket](https://www.edgepedia.org/zh/websocket) 突发接近每秒 359 兆字节，而 Elixir Cowboy 在稳定流中领先，接近每秒 293 兆字节<sup>[15](https://dl.acm.org/doi/pdf/10.1145/3830434.3830942)</sup>。

## 与 Elixir、Go 及其他语言的比较

一项针对 12 种高并发服务器语言的同行评审研究详细分析了 Erlang、Go 和带 Akka 的 Scala。Go 和 Erlang 最小化了消息延迟和进程创建时间；Scala/Akka 支持最多的休眠进程，约 1,100 万个，归因于 JVM 中更高效的内存管理；而 Erlang 拥有最快的进程生成速度<sup>[16](https://eprints.gla.ac.uk/159529/7/159529.pdf)</sup>。同一研究将 Erlang、Elixir 和 Akka 归类为演员并发语言，其中计算单元是通过消息传递通信的演员<sup>[16](https://eprints.gla.ac.uk/159529/7/159529.pdf)</sup>。

**与 Go 的调度对比。** Go 和 Erlang 使用不同的调度方法；Elixir 和 Erlang 使用具有预设执行窗口的抢占式调度，这可以使负载下的行为更加一致，代价是一些开销<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。

**Elixir 作为入口。** Elixir 运行在 BEAM 虚拟机上，自 2011 年起由 José Valim 开发，保持与 Erlang 生态系统的兼容性，因此 Erlang 库可用于 Elixir 代码，反之亦然<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。在同一种虚拟机上的生产环境中使用 Elixir 的公司包括 Discord、Bleacher Report 和 Teachers Pay Teachers<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。

对于原始连接处理工作负载，用 [Rust](https://www.edgepedia.org/zh/rust-programming-language) 重建的 200,000 连接基准测试在五次运行中的中位数为 0.21 秒，在该工作负载上比 Go 快约 1.7 倍<sup>[9](https://bytepith.com/article/how-whatsapp-hit-28m-connections-per-server-rebuilt-gorust)</sup>。Erlang 还影响了 Clojure、F#、Java 的 Kilim、Cloud Haskell 以及 C#、F# 和 Scala 的 Akka<sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup>。

## 近期发展

**OTP 27** 默认启用了 maybe 表达式，在 JIT 中为 cover 工具实现了原生覆盖率支持，通过 erlang:system_monitor/2 增加了对长消息队列的监控，并支持多个跟踪会话<sup>[3](https://www.erlang.org/downloads/27)</sup>。

**OTP 28** 引入了优先级消息，这是一种可选机制，允许选定的消息插入到进程队列中所有普通消息之前，同时保留 Erlang 的保证：从一个进程发送到另一个进程的信号，如果到达，则按发送顺序到达；该特性保留了选择性接收优化，并且对普通消息或优先级消息不施加性能惩罚<sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup>。用户可预期在 OTP 28 中使用 [TLS 1.3](https://www.edgepedia.org/zh/tls-1-3) 时获得 15% 至 25% 的加速，无需更改代码<sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup>。OTP 28 还根据 EEP 70 为推导式实现了严格生成器，根据 EEP 73 实现了 zip 生成器，严格生成器被推荐为默认，因为其符合“让它崩溃”哲学<sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup>。

**类型。** EEP 69 名义类型为 Dialyzer 添加了另一种类型系统，用 -nominal 语法声明，其主要用例是防止意外误用结构相同的类型<sup>[10](https://www.erlang.org/blog/highlights-otp-28/)</sup>。

## 采用与当前使用

Erlang 已用于爱立信 AXD301 交换机、Facebook 聊天服务器和 WhatsApp 即时通讯服务器<sup>[6](https://dl.acm.org/doi/pdf/10.1145/3107937)</sup>。RabbitMQ 是[高级消息队列协议](https://www.edgepedia.org/zh/advanced-message-queuing-protocol)标准的 Erlang 实现，在互联网规模上提供可靠的异步消息传递<sup>[1](https://cacm.acm.org/research/erlang/)</sup>。Erlang 还支撑着亚马逊的 CloudFront CDN、CouchDB、Riak 以及全球约一半的电话系统<sup>[7](https://www.cloudbees.com/blog/comparing-elixir-go)</sup>。

## References

1. [Erlang, Communications of the ACM (Joe Armstrong, 2010)](https://cacm.acm.org/research/erlang/)
2. [Review of Concurrent Programming in Erlang (Armstrong, Virding, Williams; Prentice Hall 1993), Journal of Functional Programming](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/3E436B38A9A927DC045CD0B1AB80DA46/S0956796800001519a.pdf/concurrent-programming-in-erlang-by-joe-armstrong-robert-virding-and-mike-williams-prentice-hall-1993-281-pp.pdf)
3. [Erlang/OTP 27 release notes, erlang.org](https://www.erlang.org/downloads/27)
4. [WhatsApp: 2 Million Connections Per Server with Erlang, sujeet.pro](https://sujeet.pro/articles/whatsapp-erlang-scaling)
5. [Erlang vs Elixir: Comparing the Two BEAM Languages, Skillveri](https://www.skillveris.com/study-notes/erlang/erlang-vs-elixir)
6. [Scaling Reliably: Improving the Scalability of the Erlang Distributed Actor Platform, CACM (2017)](https://dl.acm.org/doi/pdf/10.1145/3107937)
7. [Comparing Elixir and Go, CloudBees](https://www.cloudbees.com/blog/comparing-elixir-go)
8. [Twenty Years of Open Source Erlang, Erlang Solutions](https://www.erlang-solutions.com/blog/twenty-years-of-open-source-erlang/)
9. [How WhatsApp Hit 2.8M Connections Per Server, Rebuilt in Go/Rust, bytepith](https://bytepith.com/article/how-whatsapp-hit-28m-connections-per-server-rebuilt-gorust)
10. [Erlang/OTP 28 Highlights, erlang.org](https://www.erlang.org/blog/highlights-otp-28/)
11. [Joe Armstrong, Making reliable distributed systems in the presence of software errors (PhD thesis, KTH, 2003)](https://dcatkth.github.io/thesis/joe_thesis.pdf)
12. [The Ideas in Erlang, Joe Armstrong, Erlang Factory SF Bay 2010](https://www.erlang-factory.com/upload/presentations/222/ErlangFactorySFBay2010-JoeArmstrong.pdf)
13. [WhatsApp Architecture, uptime.engineer](https://www.uptime.engineer/p/whatsapp-architecture)
14. [History of Erlang, CosmicLearn](https://cosmiclearn.com/erlang/history-of-erlang.php)
15. [Green Benchmarking of BEAM Languages, ACM](https://dl.acm.org/doi/pdf/10.1145/3830434.3830942)
16. [Comparing Languages for Engineering Server Software: Erlang, Go, and Scala with Akka, University of Glasgow](https://eprints.gla.ac.uk/159529/7/159529.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › Functional, logic, and array languages

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/erlang-programming-language

English original: https://www.edgepedia.org/erlang-programming-language
