OpenAI 发布技术博客第二部分,介绍其在线存储平台 Habitat 如何通过 Azure Cosmos DB 的多租户方案支撑超过 10 亿用户、40 个地理区域和 500PB 数据。文章重点讨论单租户与多租户之间的权衡、逻辑分区键冲突和吵闹邻居问题,并称读取性能提升 12 倍。对大规模 AI 产品的基础设施团队而言,这是一份关于在线存储扩容取舍的一手经验。
OpenAI published the second part of a technical blog explaining how its online storage platform Habitat scales on Azure Cosmos DB to serve over 1 billion users across 40 regions and more than 500 petabytes of data. The post covers single- versus multi-tenant tradeoffs, logical partition key collisions and noisy-neighbor problems, and cites 12x faster reads. It offers first-hand lessons on scaling online storage for large AI products.
我们如何优化应用存储平台 Habitat 中的 Azure Cosmos DB,以应对前所未有的增长。
How we optimized Azure Cosmos DB in our application storage platform, Habitat, to manage unprecedented growth.
作者:Jon Lee、Hrachya Hakobyan 和 Chaomin Yu,技术团队成员
By Jon Lee, Hrachya Hakobyan, and Chaomin Yu, Members of Technical Staff
所有 OpenAI 产品都需要快速、可靠的数据访问,这由我们的存储平台 Habitat 提供。它必须在 40 个地理区域内为超过 10 亿用户提供毫秒级响应。我们的基础设施面临一项非凡挑战:在持续提升可靠性、性能和产品开发者效率的同时,支撑爆炸式增长和新用例。解决这一挑战需要重新思考我们的在线存储平台如何设计与运营。我们不断评估权衡,遵循这样的模式:先做到足够可靠以承载关键任务产品流量,再做到足够快以服务全球用户,最后做到能够在大规模下灵活运营。
All OpenAI products require fast, reliable access to data, provided by our storage platform, Habitat. It has to respond in milliseconds to over 1 billion users across 40 geographic regions. Our infrastructure faced an extraordinary challenge: supporting explosive growth and new use cases while continuing to improve reliability, performance, and product developer velocity. Solving that challenge required rethinking how our online storage platform was designed and operated. We constantly evaluated tradeoffs, following the pattern of: first become reliable enough for mission-critical product traffic, then fast enough for global users, and, finally, to deftly operate at massive scale.
这个由两部分组成的系列文章解释了,我们如何将 Habitat 从一个简单的客户端库和数据库发展为服务超过 500 PB 数据的全球分布式存储平台。第一部分聚焦于 Habitat 从 Python 客户端到服务的演进,还介绍了使我们能够将一个基于 Python 的服务推进到远超许多人认为可行程度的技术。
This two-part series explains how we developed Habitat from a simple client library and database into a globally distributed storage platform serving over 500 petabytes of data. Part I focused on Habitat’s evolution from a Python client to a service. It also covered the techniques that allowed us to push a Python-based service far beyond what many would consider practical.
在本篇第二部分中,我们将介绍我们如何扩展底层存储组件:Azure Cosmos DB。通过与 Azure 团队紧密合作,我们找到了一种可行的多租户方案,即共同管理的容器,并在大规模全球范围内实现了出色的读取性能。
In this post, Part II, we walk through how we scaled the underlying storage component: Azure Cosmos DB. Through a close working partnership with the Azure team, we arrived at a viable multi-tenancy approach of co-managed containers, with excellent read performance at massive, global scale.
Habitat 是我们构建的在线存储平台,使 OpenAI 产品能够快速、可靠地访问所需信息。
Habitat is the online storage platform we built so OpenAI products can quickly and reliably access needed information.
随着公司规模扩大,基础设施团队通常会从运行单租户系统演进为运行多租户系统。单租户模式下,每个产品界面团队维护自己的系统,这能缩小故障影响范围,并提供清晰的归属边界和工作负载隔离。随着团队和产品界面增多,这种模式的运行成本会变得高昂。单租户模式使每个产品团队都要负责容量规划,并且往往导致显著的过度配置。
As companies scale, infra teams often evolve from running single-tenant systems to multi-tenant ones. Single-tenancy, where every product surface team maintains their own system, reduces blast radius and provides easy ownership boundaries and workload isolation. As teams and product surfaces grow, this becomes expensive to run. Single-tenancy makes every product team responsible for capacity planning and often results in significant over-provisioning.
相反,多租户模式将控制权交给集中化的数据库团队,该团队在资源配置方面拥有更丰富的知识和经验。但多租户模式也有其自身的问题,例如吵闹邻居问题,即某个租户消耗了异常大份额的资源,从而损害了其他租户的利益。
Conversely, multi-tenancy assigns control to a centralized database team, with better knowledge and experience for provisioning resources. But multi-tenancy creates its own problems, such as noisy-neighbor issues, where one tenant consumes an unusually large share of the resources to the detriment of the other tenants.
在 Habitat 中,我们必须权衡所有这些取舍,以设计一个可靠、高效的大规模存储系统。如今,Habitat 是为许多 OpenAI 产品服务的多租户数据库层,但我们经历了一个循环式的历程:从使用一个多租户 Azure Cosmos DB 容器,到配置单租户后端容器,再到默认回归多租户容器。我们在 Habitat 的多租户之路上仍处于非常早期的阶段:我们支持大量用例,但通过尽可能利用 Azure Cosmos DB 的内置原语,我们得以推迟在这一领域的投入。
In Habitat, we had to weigh all these tradeoffs to design a reliable, efficient, storage system at scale. Today, Habitat is a multi-tenant database layer for many OpenAI products, but we went through a circular journey from using one multi-tenant Azure Cosmos DB container to provisioning single-tenant backing containers to reverting to a multi-tenant container by default. We’re still very early in our multi-tenancy story on Habitat: we support a large set of use cases, but we’ve been able to defer investment in this area by leveraging Azure Cosmos DB’s built-in primitives wherever possible.
在详细介绍我们的 Azure Cosmos DB 历程之前,先说明一些基本背景:
Before walking through our Azure Cosmos DB journey, some basic context:
一个 Azure Cosmos DB 账户定义了一组容器和一项复制策略
An Azure Cosmos DB account defines a set of containers and a replication policy
每个容器都会预配置一定量的吞吐量,以每秒请求单位(RU/s)计量
Each container is provisioned for a certain amount of throughput in request units per second (RU/s)
每个容器内的数据以典型的按指定逻辑分区键分片的方式存储,并且对每个逻辑分区键的存储和吞吐量都有明确记录的限制
Data within each container is stored in a typical shard-by-specified-logical-partition-key manner with documented limits on storage and throughput per logical partition key
Azure Cosmos DB 会将逻辑分区键分片到较少的后端物理分区上,当存储超过阈值或容器需要更多物理分区来维持预配置吞吐量时,会将其拆分为多个物理分区
Azure Cosmos DB handles sharding logical partition keys across fewer backing physical partitions, splitting them into multiple physical partitions when storage exceeds a threshold or when the container requires more physical partitions to sustain the provisioned throughput
我们最初允许任何客户端负载通过,依赖所声明的每个逻辑分区键 10K RU/s 的限制来提供背压。最初起步时,我们在每个账户中也只使用一个默认的 Azure Cosmos DB 容器。这运行得足够好,但事实证明存在几个问题:
We originally allowed any client load through, relying on the stated 10K RU/s limit per logical partition key to provide back pressure. When we first set out, we also used only one default Azure Cosmos DB container in each account. This worked well enough, but proved problematic for a few reasons:
由于早期的 API 设计选择,我们直接将逻辑分区键的选择暴露给了客户端。结果,在不同产品用例中,常用的分区键(如 user_id 或 workspace_id)频繁发生冲突。由于这些用例共享 Azure Cosmos DB 对相同逻辑分区键的限制,该设计制约了特定用户的整体吞吐量。
Due to early API choices, we exposed logical partition key selection directly to clients. As a result, we have frequent collisions on commonly used partition keys, such as user_id or workspace_id , across different product use cases. Because these use cases shared Azure Cosmos DB limits on the same logical partition keys, the design constrained overall throughput for a given user.
在迁移到 Habitat 和 Azure Cosmos DB 之前,我们在扩展 Postgres 时面临的一个常见问题是:来自某个非常热门的组织或热键的大量请求——即某个租户产生不成比例的高负载——可能导致整个数据库瘫痪。我们投入了大量集体精力来缓存这些键,并在缓存未命中时引入租约机制,以确保由此产生的缓存未命中负载不会冲击底层数据库。
One common problem we faced in scaling Postgres before we moved to Habitat and Azure Cosmos DB was the ability for many requests from a very hot organization or hot key—one tenant generating a disproportionately high load—to take out the whole database. Significant collective effort was spent to cache those keys and build leases into cache misses so that the resulting cache miss load does not stomp on the underlying database.
图 02a · 逻辑分区键限制
Figure 02a · Logical partition key limits
虽然文档中记录的数据大小和吞吐量限制适用于逻辑分区键,但实际执行限制的是物理分区。在实践中,这意味着逻辑分区无法达到 10,000 RU/s。相反,它们与该物理分区上共置的所有其他逻辑分区键共享物理分区的限制。
While documented data size and throughput limits apply to logical partition keys, the physical partition is actually where limits are enforced. In practice, this means logical partitions are unable to reach 10,000 RU/s. Instead, they share the physical partition’s limits with every other logical partition key colocated on that physical partition.
这使得吵闹邻居能够影响许多不相关的逻辑分区键,针对小众产品功能数据的工作负载会干扰核心的登录和注册流程。
This allowed noisy neighbors to impact many unrelated logical partition keys, with workloads operating on data for niche product features disrupting core login and signup flows.
给定物理分区的吞吐量限制也是可变的,取决于分配给 Azure Cosmos DB 容器的总吞吐量。容器的预配吞吐量大致均匀分布在该容器的各个物理分区上。
The throughput limit for a given physical partition is also variable, dependent on the total throughput assigned to the Azure Cosmos DB container. The container’s provisioned throughput is distributed roughly evenly across the physical partitions for that container.
随着数据量增长,物理分区会分裂,增加总分区数,而总预配吞吐量保持不变。结果是,随着数据大小增长,每个分区的有效吞吐量限制会缩小。
As data volume grows, physical partitions split, increasing the total partition count while total provisioned throughput remains unchanged. The result is that the effective throughput limit per partition shrinks as data size grows.
对于我们存储最密集、类似 blob 的用例,这会造成一种故障模式:即使请求速率和使用量保持稳定,仅存储增长就会降低活跃工作负载的吞吐能力。
For our most storage-heavy, blob-like use cases, this creates a failure mode where storage growth alone reduces throughput capacity for active workloads even while request rates and usage remain stable.
当分区达到其存储上限时就会发生拆分。每个子分区获得一半的吞吐容量。
A partition splits when it reaches its storage limit. Each child receives half the throughput capacity.
拆分后:5K RU/s 容量 × 2 个分区
After split: 5K RU/s capacity × 2 partitions
使用独立容器有助于部分解决这些分区键限制和吵闹邻居问题。通过使用独立容器,每个容器都可以分配到自己独立的物理分区,只有该容器自身变化的工作负载才可能成为吵闹邻居或在分区键空间上发生重叠。不利的一面是,配置新容器存在非零开销——每个容器都需要单独进行容量规划——因此我们只想在合理界定服务或用例边界时才创建容器。
Separate containers help partially solve these partition key limits and noisy neighbors. By using separate containers, each can be assigned its own independent physical partitions and only the varying workloads on that container itself could be noisy neighbors or overlap in partition key space. On the downside, there is non-zero overhead in provisioning new containers—each container requires individualized capacity planning—so we want to create containers only at a service or use case boundary that makes sense.
我们开始为需要强隔离的最关键新用例使用独立的 Azure Cosmos DB 容器。结合一个通过将每个容器预配的总吞吐量与其物理分区数同步来管理弱分区的脚本,我们基本避免了此前面临的所有问题。
We began to use separate Azure Cosmos DB containers for the most critical new use cases requiring strong isolation. Combined with a script for managing weak partitions by synchronizing the total throughput provisioned for each container with its number of physical partitions, we mostly avoided all the problems we were facing.
然而,由于我们最初使用的是默认容器,许多遗留的关键用例不幸被困在其中,无法进行数据迁移。为了保护仍依赖共享默认容器中数据的关键服务,我们经常建议团队为新服务或主要产品功能创建独立容器。随着规模扩大,默认容器承受的压力不断增加,生产事故也随之增多。我们开始为任何新服务或大型产品功能预配容器,只是为了保护默认容器免受过多的负载。
However, since we started with a default container, many legacy critical use cases were unfortunately stuck there without a data migration. To protect critical services that still relied on data in the shared default container, we often advised teams to create separate containers for new services or major product features. As we scaled, pressure on the default container grew and production incidents also grew. We began to provision containers for any new service or large product feature just to protect the default container from more load.
这使我们避免了许多全站范围的故障,将影响范围限制在功能或服务降级类故障内。然而,预配新容器的开销最终追上了我们:
This saved us from many site-wide outages, limiting the blast radius to feature or service degradation outages. However, the overhead of provisioning new containers eventually caught up to us:
在旧的共享默认容器中,任何特定的新用例从 0 QPS 增长到 100 万 QPS,都不太可能显著改变总体的聚合用量。然而在隔离容器上,一个新用例从 0 增长到 100 万 QPS,可能意味着比仅来自内部 dogfooding 的基线负载高出 1000 倍的负载。
In the old shared default container, any particular new use case going from 0 QPS to 1 million QPS was unlikely to significantly shift the total aggregate usage. On an isolated container however, a new use case going from 0 to 1 million QPS might represent 1,000x more load than the baseline attributed to only internal dogfooding.
使用专用容器后,团队现在需要记住按照发布预期的吞吐量水平预先配置容器。产品团队常常把功能发布到生产环境后忘记预先扩容,而我们则在发布日坐在 SEV 会议室里,试图在最后一刻扩容容器。
With dedicated containers, teams now needed to remember to pre-provision containers to the expected level of throughput for their launches. Product teams would often ship their features to production and forget to pre-scale and we’d sit in SEV rooms on launch day attempting to scale up containers last minute.
这是多租户系统混合核心工作负载带来的好处,但采用这种容器策略后,我们为了获得更好的隔离性而失去了这一好处。
This is a core workload mixing benefit of multi-tenant systems that we lose in favor of better isolation with this container strategy.
图 03 · 缺乏稳定的聚合增长
Figure 03 · Lack of stable aggregate growth
负载测试期间为 1K RU/s → 发布时为 1M RU/s
1K RU/s during load testing → 1M RU/s at launch
随着时间的推移,监控专用容器的使用情况还需要对产品有深入的了解,知道流量何时会转移或增长。
Over time, monitoring usage of dedicated containers also required intimate product knowledge of when traffic would shift or grow.
我们可以提供一个基本的控制平面脚本,根据增长趋势为每个容器扩容预配置吞吐量,但如果没有稳定的聚合增长,正确扩容这些容器实际上需要 OpenAI 的每个产品团队都参与容量规划。
We can provide a basic control plane script to scale up the provisioned throughput for each container based on growth trends, but without the stable aggregate growth, scaling these correctly enabled essentially every product team at OpenAI to be involved in capacity planning.
这在组织层面无法扩展,因为每个服务的所属团队对 Azure Cosmos DB 预配置的尽职程度差异很大。
This failed to scale organizationally with a wide variance in how much diligence each service’s owning team paid to their Azure Cosmos DB provisioning.
随着组织的发展,这些运营问题变得难以扩展。没有一个单独的中央团队来做规划,导致许多团队规划不善。结果,我们最终又转向多租户解决方案,并与 Azure 团队密切合作,通过共享租户设计为我们最初的多租户问题设计修复方案:
As the organization grew, those operational problems became difficult to scale. No lone, central team doing planning resulted in many teams planning poorly. As a result, we ended up shifting back towards a multi-tenant solution and worked closely with the Azure team to engineer fixes for our original multi-tenancy issues with a shared-tenancy design:
首先对最热的键进行限流,可以防止吵闹邻居消耗给定物理分区的所有可用吞吐量。这减少了吵闹邻居效应,即一个热键可能造成全站范围的影响。
Throttling the hottest keys first prevents noisy neighbors from consuming all of the available throughput for a given physical partition This reduces the noisy neighbor effect, where one hot key can have a site-wide blast radius.
请求分层 QoS 同样可以防止离线工作负载(例如批量插入或数据提取)占用给定物理分区的所有吞吐量。通过为这些工作负载的请求分配较低的优先级,它们会首先被限流,从而防止对服务最终用户的更高优先级交互式请求进行限流。
Request tiering QoS similarly prevents offline workloads, such as bulk inserts or data extracts from utilizing all of the throughput for a given physical partition. By assigning the requests for these workloads a lower priority, they are throttled first, preventing throttling on higher priority, interactive requests that serve end users.
Azure Cosmos DB 原本已有无服务器模式,本可以解决我们在预配模式下分区较弱的问题。然而,它有一个主要缺点:它仅根据存储使用量进行扩展,并将每个物理分区的 RU 使用量限制在 5K RU/s。这能解决我们分区较弱的问题,但会阻止我们扩展 RU 密集型用例。
Azure Cosmos DB already had a serverless mode that would’ve fixed our weak partitions problem with the provisioned mode. However, there was a major downside: it scaled based on storage usage only and limited RU usage to 5K RU/s per physical partition. This would solve our weak partitions problem, but prevent us from scaling up RU-heavy use cases.
Azure Cosmos DB 团队为我们构建的新改进无服务器模式允许每个物理分区扩展到 10K RU/s,并在基于存储使用量的拆分之外引入了基于 RU 使用量的拆分,使我们能够停止手动监控和扩展容器。
A new improved serverless mode Azure Cosmos DB team built for us allowed every physical partition to scale up to 10K RU/s and introduced RU-usage-based splits in addition to storage-usage-based splits allowed us to stop monitoring and scaling up containers manually.
借助这些变更,我们得以转回使用一个共享容器,这显著简化了产品团队进行任何前期或持续容量规划的需求,同时保留了工作负载之间的隔离。此后数月,尽管产品开发加速,我们没有发生任何预配或吵闹邻居事件。
With these changes, we’ve been able to shift back to using one shared container which has significantly simplified the need to do any upfront or continued capacity planning for product teams while preserving isolation between workloads. We have not had any provisioning or noisy neighbor incidents in the months since, despite accelerating product development.
当公司发展到 OpenAI 的规模时,托管服务变得不那么“托管”。许多工作负载成为云提供商基础设施中的异常值。随着 OpenAI 成为 Azure Cosmos DB 最大的用户之一,成功运营它需要一种不同的模式:共享测试、共享仪表板、直接工程渠道,以及对我们规模下才会出现的故障模式共同负责。如今,我们最大的 Azure Cosmos DB 账户存储数十 PB 数据,并服务超过 20M QPS。据我们所知,我们是唯一一家在此规模部署 Azure Cosmos DB 的公司。
When companies grow to OpenAI’s scale, managed services become less “managed.” Many workloads become outliers in the cloud provider’s infrastructure. As OpenAI became one of the largest users of Azure Cosmos DB, operating it successfully required a different model: shared tests, shared dashboards, direct engineering channels, and joint ownership of failure modes that only appear at our scale. Today, our largest Azure Cosmos DB account stores 10s of petabytes of data and serves more than 20M QPS. To our knowledge, we are the only company which has deployed Azure Cosmos DB at this scale.
2024 年底,一次区域中断清楚地表明了为什么这种合作是必要的。当时,停电袭击了我们最大的 Azure Cosmos DB 账户之一的主 Azure 区域。在随后的故障转移演练中,我们意识到我们最大的账户需要数小时才能故障转移到次要区域,这是我们团队和 Azure Cosmos DB 团队都未在我们规模下测试过的事情。
In late 2024, a regional outage made it clear why this collaboration was necessary. At the time, a power outage struck the primary Azure region for one of our largest Azure Cosmos DB accounts. In the resulting failover exercise, we realized that our largest accounts would take several hours to fail over to a secondary region, something neither our team nor the Azure Cosmos DB team had tested at our scale.
大约一个月后,在将这些账户故障回切到原始区域时,我们触发了另一次严重中断。到原始区域的连接蜂拥而至,压垮了原始区域的 Azure Cosmos DB 网关。网关模式 Azure Cosmos DB 客户端处理故障转移的方式存在一个特殊问题,导致所有客户端立即建立连接并向原始区域发送请求,尽管只有一部分物理分区迁回了该区域。
About a month later, while failing those accounts back to the original region, we triggered another serious outage. A connection stampede to the original region overwhelmed the original region’s Azure Cosmos DB gateway. A quirk in how the gateway-mode Azure Cosmos DB client handled failovers caused all clients to immediately establish connections and send requests to the original region, even though only a subset of physical partitions had moved back to that region.
在每次中断中,OpenAI 和 Azure 都走完了各自的事件管理流程,确定了若干根本原因并部署了修复措施。但这些中断是我们团队与 Azure Cosmos DB 团队在运营和测试方式上更大系统性问题的症状。起初,利用托管服务让我们能够快速行动并专注于业务目标。随着规模扩大,托管服务变得不那么“托管”,需要在从设计到运营的各个层面与服务提供商进行深度集成。
In each outage, both OpenAI and Azure ran through their incident management processes, with a number of root causes identified and fixes deployed. But these outages were symptoms of a larger systemic issue between the way our team and Azure Cosmos DB team operated and tested. At first, leveraging managed services allowed us to move fast and focus on business goals. At scale, managed services become less managed and require deep integrations with the service provider at all levels from design to operations.
如今,Habitat 和 Azure 工程团队联合监控 Habitat 的每一层,从我们的服务层一直到 Azure Cosmos DB 的物理存储层。我们针对一个生产规模但非生产的 Azure Cosmos DB 账户进行运营和变更测试。我们共同测试和迭代 SDK,并确定如何扩展和调整 Azure Cosmos DB 账户规模的最佳实践。我们通过 Teams 拥有直接的工程访问权限,甚至开放了无需经过支持层级即可直接呼叫对方的方式。
Today, the Habitat and Azure engineering teams jointly monitor every layer of Habitat from our service layers down into Azure Cosmos DB’s physical storage layers. We operate and test changes against a production-sized but non-production Azure Cosmos DB account. We test and iterate on SDKs together and determine best practices for how we scale and size our Azure Cosmos DB accounts. We have direct engineering access to each other through Teams and have even exposed ways to directly page each other without going through support tiers.
过去一年中,双方开展了大量联合工作,以改善 Azure Cosmos DB 控制平面和网关的可扩展性,在其网关中增加了负载保护和对各种 Azure Cosmos DB 元数据的缓存,以保护 Azure Cosmos DB 元数据分区;通过连接池和更高的连接复用率,减轻了我们 Python habitat-service 进程集群带来的压力;尽可能将工作负载共置;并通过不同的 VPC 将关键工作负载与离线工作负载隔离。
A substantial amount of joint work has occurred over the last year to improve Azure Cosmos DB control-plane and gateway scalability, adding load protection and caching of various Azure Cosmos DB metadata in their gateway to protect the Azure Cosmos DB metadata partition, reducing pressure from our fleet of Python habitat-service processes through connection pooling and higher connection reuse, colocating workloads where possible, and isolating critical workloads from offline workloads via different VPCs.
这种程度的协同工程在过去一年中对我们的 Azure Cosmos DB 稳定性至关重要。任何规模足够大或足够关键的系统都需要这种团队协作:打破所有权壁垒,跨组织边界运营。
This level of co-engineering has been critical to our Azure Cosmos DB stability in the last year. Any system large or critical enough necessitates this kind of teamwork: breaking down ownership walls and operating across organizational boundaries.
正如第一部分所述,我们的核心理念是:先让它能用,再让它快,最后让它便宜。可靠性优先,其次是性能,最后是效率。Habitat 已成为 OpenAI 的关键基础设施,因此首要任务是让它可靠。随着系统在 2025 年趋于稳定、OpenAI 在全球扩张,延迟成为用户和产品团队面临的下一个制约因素。
As mentioned in Part I, our core philosophy is: make it work, make it fast, make it cheap. Reliability first, then performance, then efficiency. Habitat had become critical infrastructure for OpenAI, so the first priority was making it reliable. As the system stabilized in 2025 and OpenAI expanded globally, latency became the next constraint for users and product teams.
计算和存储容量常常位于不同区域,导致跨区域请求不可避免。
Compute and storage capacity were often located in different regions, making cross-region requests unavoidable.
图 04 · 在全球范围内提升性能
Figure 04 · Improving performance at a global scale
计算和存储的地理位置仅用于示意。
Geographical locations of compute and storage are for illustrative purposes only.
延迟因数据存储位置和请求处理位置的不同而差异很大。为了让 Habitat 请求具备良好性能,产品开发者依赖额外的客户端缓存、请求批处理、尽量减少数据库读写,并尽可能将工作移出关键路径。虽然这催生了一些对数据库问题更具韧性的有趣产品设计,但也让产品变得更复杂,并拖慢了产品迭代:这未能实现 Habitat 的目标,即鼓励快速的产品开发,且无需了解数据库。
Latency varied widely depending on where the data was stored and where the request was served. To make Habitat requests performant, product developers relied on additional client-side caching, request batching, minimizing database reads and writes, and moving work off the critical path wherever possible. While this led to some interesting product designs that were more resilient to database issues, it also made products more complex and slowed down product iteration: a failure to achieve Habitat’s goal to encourage fast product development and not needing to understand the database.
这一直是我们 2026 年的一大重点,我们制定了一套分层策略来优化读取性能,而读取占 Habitat 请求的 90% 以上:
This has been a big focus for us in 2026, and we’ve developed a layered strategy for optimizing read performance, which represents over 90% of Habitat’s requests:
我们在全球不同的 Azure 区域拥有多个 Azure Cosmos DB 账户,服务于有数据驻留要求的用户。即使对于没有严格数据驻留要求的用户,将数据与用户就近放置也能通过更低的延迟改善其体验。
We have a number of Azure Cosmos DB accounts in different Azure regions around the world for users with data residency requirements. Even for users without hard data residency requirements, colocating data near users also improves their experience through lower latency.
在实践中,除了数据驻留场景之外,我们无法广泛实现这一点,因为数据放置受到物理云容量的限制。由于我们数据集的规模,许多区域无法容纳其按用户流量比例份额所需的大量存储空间。
In practice, we’re not able to achieve this broadly outside of data residency use cases because placement is constrained by physical cloud capacity. Due to the size of our data sets, many regions cannot accommodate the massive storage footprint required for their proportional share of user traffic.
在数据中心容量、电力或冷却能力有限的区域,我们有时会面临直接的权衡:是将容量保留给同地存储以降低 Habitat 延迟,还是将该容量用于额外的服务计算并将存储请求路由到更远的地方。
In regions with limited datacenter capacity, power, or cooling, we sometimes face a direct tradeoff: reserve capacity for colocated storage and lower Habitat latency, or use that capacity for additional serving compute and route storage requests farther away.
因此,数据放置决策往往是产品层面的权衡:A)针对不同产品和用户子集优化延迟,B)原始存储和计算容量。
As a result, placement decisions are often a product tradeoff between: A) optimizing latency for different products and user subsets, and B) raw storage and compute capacity.
对于延迟最关键的工作负载,例如每个请求都需要的身份验证元数据,我们通过使用具有多个次要区域的高度复制 Azure Cosmos DB 账户来减少计算与 Habitat 之间的 RTT。有些账户复制到我们提供流量的全部 40 个 Azure 区域;其他账户则至少复制到每个大洲的一个区域。
For the most latency-critical workloads, like authentication metadata that is needed on every request, we reduce RTT between compute and Habitat by using highly replicated Azure Cosmos DB accounts with many secondary regions. Some accounts replicate to all 40 Azure regions where we serve traffic; others replicate to at least one region per continent.
我们不得不比预期更早地在 Habitat 中构建透明的数据移动和路由层,以便我们能够在具有不同复制拓扑的账户之间移动数据集。
We’ve had to build a transparent data movement and routing layer into Habitat earlier than desired so that we can move datasets between accounts with different replication topologies.
然而,这种方法最终也受到容量限制,因此仅限于少数精心挑选的数据集。
However, this approach is also ultimately capacity constrained and therefore limited to a few hand-selected datasets.
Habitat 工作负载绝大多数是读密集型的,应用程序不断获取设置、状态和其他属性;不成比例的流量可归因于相对较小的一组频繁访问类型的工作集。这使得该系统特别适合分布式缓存。虽然缓存在单区域基础设施中能提高性能,但在跨区域工作时改进会叠加,因为缓存可以用来“隐藏”跨区域延迟。
Habitat workloads are overwhelmingly read-heavy, with applications constantly fetching settings, state, and other properties; a disproportionate amount of traffic is attributable to a relatively small working set of frequently accessed types. This makes the system particularly well suited for distributed caching. While caching improves performance in single-region infrastructure, the improvements compound when working across regions, where it can be used to “hide” cross-region latency.
我们在 Habitat 中集成了一个分布式旁路缓存层。它会在 Valkey 服务器中存储最近访问过的对象和边的副本,这些服务器部署在我们基础设施中靠近用户请求处理位置的地方。通常,这类缓存会使用 TTL,在命中率和数据陈旧度之间进行权衡。较短的 TTL 可减少数据陈旧度,但会削弱缓存的正面效果,因为条目会很快过期,必须更频繁地重新获取。较长的 TTL 可提高命中率,但会在较长时间内提供陈旧数据,导致糟糕的用户体验和不可预测的产品行为。
We’ve integrated a distributed lookaside caching layer into Habitat. It stores a copy of recently accessed objects and edges in Valkey servers placed in our infrastructure close to where user requests are processed. Typically, these sorts of caches use a TTL that trade off between hit rate and staleness. Short TTLs reduce staleness but blunt the positive impact of caching, because items expire out of the cache quickly and must be retrieved more frequently. Longer TTLs improve hit rates, but serve stale data for extended periods, leading to poor user experiences and unpredictable product behavior.
另一种方法是在应用程序对同一底层数据执行写操作时,执行显式的失效操作。这能大幅提升缓存数据的新鲜度,但很难做对。它仍然受制于许多边缘情况,例如部分失败,这会导致缓存保持陈旧。多区域部署会加剧这些问题,因为跨区域网络可靠性较低,而且缓存会与底层存储的复制过程发生竞争。用 TTL 对此进行补充可以限制最大陈旧度,但仍然受制于上文讨论的相同底层 TTL 权衡。
Another approach is to execute explicit invalidation operations when the application is performing write operations on the same underlying data. This dramatically improves the freshness of cached data, but can be hard to get right. It also remains subject to a number of edge cases, like partial failures, which leave the cache stale. Multi-region deployments compound these problems, with less reliable cross-region networks, and the cache racing with the replication of the underlying storage. Complementing this with TTLs bounds the maximum staleness, but still suffers from the same underlying TTL trade-off discussed above.
我们采用了更先进的失效方法,追随 Meta 的 TAO 等系统的脚步。Habitat 的集成缓存依赖通过 Azure Cosmos DB 变更源实现的变更数据捕获(CDC),在数秒内在全球范围内传播失效,从而实现高命中率并保持低陈旧度。我们基础设施中的一个服务会消费该变更源,并在底层数据更新时对本地缓存执行失效操作。这会导致下次读取数据时出现未命中,随后缓存会被填入最新版本的数据。
We’ve taken a more advanced approach to invalidation, following in the footsteps of systems like Meta’s TAO. Habitat’s integrated cache relies on change data capture (CDC) through Azure Cosmos DB’s change feed to propagate invalidations globally within seconds, achieving high hit rates while keeping staleness low. A service in our infrastructure consumes this change feed and executes invalidations to the local caches when the underlying data is updated. This causes a miss the next time the data is read, and the cache is filled with the latest version of the data.
我们还通过用 Rust 重写代码,消除了 Habitat 服务本身的开销,从而显著降低了延迟;Rust 比原来的 Python 性能更高。
We’ve also significantly reduced latency by removing overhead in the Habitat service itself through rewriting the code in Rust, which is more performant than the original Python.
这些改动共同将我们的全球读取 p50 从大约 120 毫秒降低到大约 10 毫秒,自今年早些时候我们开始关注性能以来提升了 12 倍。
Together, these changes have reduced our overall global read p50 from roughly 120 ms to roughly 10 ms, a 12x improvement since we began focusing on performance earlier this year.