苹果完成核心服务向Swift迁移,性能提升40%
Apple Completes Migration of Key Service to Swift, Gains 40% Performance Uplift

原始链接: https://www.infoq.com/news/2025/06/apple-swift-migration/

苹果公司将其全球密码监控服务从Java迁移到Swift,实现了40%的吞吐量提升和近50%的Kubernetes容量节省。苹果工程师详细介绍了这次重写,使该服务能够处理每天数十亿次请求,同时提高了响应速度和可维护性。Swift相较于Java的优势包括更低的内存开销、更快的启动时间和简化的并发性,从而使代码行减少了85%。 之前的Java实现饱受垃圾回收暂停、启动缓慢和高内存使用率的困扰。Swift利用确定性内存管理并借助Vapor框架,消除了与GC相关的延迟峰值,将内存消耗从吉字节降低到兆字节,并改善了冷启动时间。这支持了苹果的全球自动伸缩需求。 这种转变反映了一种趋势,即针对极大规模的服务,越来越多的公司选择使用像Swift、Rust(Meta,AWS)和Go(Netflix)这样的注重性能的语言,因为在这些场景中,确定的性能和低资源使用至关重要。虽然Java仍然具有相关性,但当通用运行时变得不足时,这些语言就能提供优势。

这篇 Hacker News 帖子讨论了苹果将一项关键服务从 Java 迁移到 Swift 的过程,据报道性能提升了 40%,代码行数减少了 85%。评论者指出,原文标题省略了从 Java 迁移到 Swift 的信息。讨论质疑了将性能作为评估技术迁移的唯一指标的做法,主张应考虑开发者效率和可维护性等因素。一些用户推测,性能提升源于架构或算法的更改,而不仅仅是语言的转换。其他人则对 Swift 的构建时间表示担忧,提及之前一篇关于 Swift 构建速度慢的文章,并质疑其是否适合性能关键型服务器端代码,尽管也有人为其辩护。最后,一位评论者批评了企业环境中“提升”(uplift)一词的过度使用。该帖子还链接到之前关于同一主题的更长篇讨论。
相关文章

原文

Apple has migrated its global Password Monitoring service from Java to Swift, achieving a 40% increase in throughput and significantly reducing memory usage—freeing up nearly 50% of previously allocated Kubernetes capacity. 

In a recent post, Apple engineers detailed how the rewrite helped the service scale to billions of requests per day while improving responsiveness and maintainability. The team cited lower memory overhead, improved startup time, and simplified concurrency as key reasons for choosing Swift over further JVM optimization.

Swift allowed us to write smaller, less verbose, and more expressive codebases (close to 85% reduction in lines of code) that are highly readable while prioritizing safety and efficiency.

Apple's Password Monitoring service, part of the broader Password app's ecosystem, is responsible for securely checking whether a user's saved credentials have appeared in known data breaches, without revealing any private information to Apple. It handles billions of requests daily, performing cryptographic comparisons using privacy-preserving protocols.

This workload demands high computational throughput, tight latency bounds, and elastic scaling across regions. Traffic fluctuates significantly over the course of a day, with regional peaks differing by up to 50%. To accommodate these swings, the system must quickly spin up or wind down instances while maintaining low-latency responses.

Apple's previous Java implementation struggled to meet the service's growing performance and scalability needs. Garbage collection caused unpredictable pause times under load, degrading latency consistency. Startup overhead—from JVM initialization, class loading, and just-in-time compilation, slowed the system's ability to scale in real time. Additionally, the service's memory footprint, often reaching tens of gigabytes per instance, reduced infrastructure efficiency and raised operational costs.

Originally developed as a client-side language for Apple platforms, Swift has since expanded into server-side use cases. Apple's engineering team selected Swift not just for its ecosystem alignment, but for its ability to deliver consistent performance in compute-intensive environments. 

The rewrite also used Vapor, a popular Swift web framework, as a foundation. Additional custom packages were implemented to handle elliptic curve operations, cryptographic auditing, and middleware specific to the Password Monitoring domain.

Swift's deterministic memory management, based on reference counting rather than garbage collection (GC), eliminated latency spikes caused by GC pauses. This consistency proved critical for a low-latency system at scale. After tuning, Apple reported sub-millisecond 99.9th percentile latencies and a dramatic drop in memory usage: Swift instances consumed hundreds of megabytes, compared to tens of gigabytes with Java.

Startup times also improved. Without JVM initialization overhead or JIT warm-up, Swift services could cold-start more quickly, supporting Apple's global autoscaling requirements.

Apple's migration reflects a broader trend: the shift toward performance-oriented languages for services operating at extreme scale. Meta has a long history with Rust from hyper-performant Source control solutions to programming languages for the blockchain. Netflix introduced Rend, a high-performance proxy written in Go, to take over from a Java-based client interacting with Memcached. AWS increasingly relies on Rust in services where deterministic performance and low resource usage improve infrastructure efficiency.

While this isn't a sign that Java and similar languages are in decline, there is growing evidence that at the uppermost end of performance requirements, some are finding that general-purpose runtimes no longer suffice.

 

联系我们 contact @ memedata.com