Cloudflare D1控制平面架构:如何支撑百万级数据库扩展
Under the hood, off the hot path: how D1 scales to millions of databases
面向开发者与架构师,详细拆解了D1控制平面在百万级规模下的工程实践与架构取舍,具有极高的复用参考价值。
Traditional database architecture assumes you have one or two big database clusters per application, usually accompanied by a connection pooling solution. AI-assisted development and vibe coding platforms turn this model inside out and are driving applications toward many small databases: one per user, application, preview environment, or tenant.
传统数据库架构假设每个应用程序拥有一个大或两个大型数据库集群,通常还会搭配连接池解决方案。AI辅助开发和氛围编码平台彻底颠覆了这一模式,推动应用程序走向许多小型数据库:每个用户、应用程序、预览环境或租户一个数据库。
That model fits D1 particularly well. D1 is Cloudflare's managed, serverless database based on SQLite. It allows you to create millions of databases and only pay for what you use.
该模型特别适合 D1。D1 是 Cloudflare 基于 SQLite 的托管无服务器数据库。它允许你创建数百万个数据库,并仅为你使用的部分付费。
The question becomes: how do you scale the control plane for millions of databases without making every query slower by sending it through centralized bottlenecks?
问题变成了:如何在不过度依赖集中式瓶颈导致每次查询变慢的情况下,为数百万个数据库扩展控制平面?
We made a lot of architectural changes to scale out D1 to support platforms with millions of created databases, hundreds of thousands of which are active at any point.
我们进行了大量架构变更,以将 D1 扩展到支持拥有数百万已创建数据库的平台,其中在任何时间点都有数十万个处于活跃状态。
This post is a behind-the-scenes deep dive into how the D1 control plane works. Many of the architectural patterns we will describe can be used by anyone building on top of Cloudflare’s Developer Platform. The post also gives a glimpse into how we build our own control planes.
本文深入探讨了 D1 控制平面的工作原理。我们将描述的许多架构模式可供任何在 Cloudflare Developer Platform 之上构建的人使用。本文还展示了我们如何构建自己的控制平面。
Growth in numbers
数量的增长
There was already a steady growth in D1 usage when we started planning these changes, but we knew we would need to support a much bigger scale once large platforms started building with D1.
在我们开始规划这些更改时,D1 的使用量已经稳步增长,但我们知道一旦大型平台开始使用 D1 进行构建,我们需要支持更大规模的扩展。
Here is how things have played out so far.
以下是目前的发展情况。
D1 has passed 5.5 million databases and has seen a 15x growth in the number of live non-deleted databases over the past one year, and a 65x growth over the past 2 years (as of October 8, 2026).
D1 已突破 550 万个数据库,在过去一年中(截至 2026 年 10 月 8 日),存活且未删除的数据库数量增长了 15 倍,过去两年增长了 65 倍。
We have also seen 18x growth in the number of daily active databases over the past year (as of October 8, 2026). A database is considered active if it executed at least one query during that day.
在过去一年中(截至 2026 年 10 月 8 日),每日活跃数据库的数量也增长了 18 倍。如果数据库在该天内执行了至少一次查询,则被视为活跃。
Established and successful platforms are already building with D1, using the same tools available to all Cloudflare users.
成熟且成功的平台已经在使用 D1 进行构建,使用的是所有 Cloudflare 用户均可使用的相同工具。
"ChatGPT Sites lets anyone turn ideas into shareable experiences. D1 gives our users a simple, scalable way to add features like custom metrics, leaderboards, and comments to their dashboards, games, and tools."
"ChatGPT Sites 让任何人都能将想法转化为可分享体验。D1 为我们的用户提供了一种简单、可扩展的方式,为其仪表板、游戏和工具添加自定义指标、排行榜和评论等功能。"
- Jon Abrams, ChatGPT Sites @ OpenAI
While we were making the changes described in this blog post, we periodically load-tested against a target well above our expected growth. This guided where to focus our optimization efforts.
在我们实施本博客文章中描述的更改的同时,我们定期针对远高于预期增长的目标进行负载测试。这指导了我们优化工作的重点方向。
Guiding engineering principles
指导性的工程原则
Applications built on the Cloudflare Developer Platform range from full-stack applications to service APIs, from internal single-user portals to massive multi-tenant vibe coding platforms, and everything in between.
基于 Cloudflare Developer Platform 构建的应用程序范围广泛,从全栈应用程序到服务 API,从内部单用户门户到大规模多租户 vibe coding 平台,以及介于两者之间的各种应用。
While building out the D1 control plane to support millions of databases, the following tenets and principles were guiding our architecture decisions and are useful for anyone building their control and data planes on our platform.
在构建 D1 控制平面以支持数百万个数据库的过程中,以下准则和原则指导着我们的架构决策,对于任何在我们平台上构建其控制和数据平面的人来说都很有用。
- Design all processes with failure and idempotency in mind. Failures will happen, so the system needs to self-correct where possible and continue making progress.
- Avoid centralized single-region components in any hot path for data plane operations (in our case all query requests).
- Use Workers KV, a globally distributed KV store, for critical routing metadata so that cache misses only rarely need to query central databases.
- Use Hyperdrive to access central Postgres databases for efficient connection pooling and optimized routing and caching, using read replicas wherever possible.
- Periodically precompute aggregations in the database to avoid scan queries during user request processing.
- Use eventual consistency and queuing between components that don’t need strong consistency to increase reliability and availability.
- Load-test the application with realistic workloads to discover bottlenecks and inefficiencies, and focus on optimizing those first.
- 在设计所有流程时,需考虑故障处理和幂等性。故障必然会发生,因此系统需要在可能的情况下自我纠正并继续推进。
- 避免在数据平面操作(在我们的案例中为所有查询请求)的任何热路径中使用集中式的单区域组件。
- 使用 Workers KV(一个全球分布的 KV 存储)来存储关键的路由元数据,以便缓存未命中时极少需要查询中央数据库。
- 使用 Hyperdrive 访问中央 Postgres 数据库,以实现高效的连接池、优化的路由和缓存,并在可能的情况下使用只读副本。
- 定期在数据库中预计算聚合结果,以避免在用户请求处理期间进行扫描查询。
- 在不强求一致性的组件之间使用最终一致性和队列机制,以提高可靠性和可用性。
- 使用真实负载对应用程序进行负载测试,以发现瓶颈和低效之处,并首先专注于优化这些方面。
There are more engineering principles we follow internally to build our systems, but the above are generic enough and apply to anyone building on Cloudflare.
我们在内部遵循更多的工程原则来构建我们的系统,但上述原则足够通用,适用于任何在 Cloudflare 上构建系统的人。
The following sections will dive into the main system components and processes of the D1 control plane regarding the lifecycle of a D1 database, and showcase how to put these principles into practice with a real-world system.
接下来的部分将深入探讨 D1 控制平面的主要系统组件和流程,涉及 D1 数据库的生命周期,并展示如何在现实世界的系统中实践这些原则。
High-level architecture
高层级架构
Let’s start off by introducing the main components of D1.
让我们先从介绍 D1 的主要组件开始。
There are a few more arrows and boxes missing from the diagram for secondary processes supporting D1, but these are the ones providing D1’s public feature set. We will explore the rest in dedicated sections in the rest of the blog post.
图中缺少一些用于支持 D1 的次要流程的箭头和方框,但这些是提供 D1 公共功能集的部分。我们将在博客文章的其余部分的专门章节中探讨其余部分。
First of all, each D1 database is backed by one SQLite Durable Object, which makes it very easy to create millions of them.
首先,每个 D1 数据库都由一个 SQLite Durable Object 提供支持,这使得创建数百万个数据库变得非常容易。
The “HTTP API” shape represents all our API surfaces including the Cloudflare dashboard, Wrangler, and Cloudflare CLI. All API requests go through Cloudflare’s edge gateway and are served by the D1 Edge API Worker.
"HTTP API" 形状代表我们所有的 API 表面,包括 Cloudflare 仪表板、Wrangler 和 Cloudflare CLI。所有 API 请求都通过 Cloudflare 的边缘网关,并由 D1 Edge API Worker 提供服务。
The diagram shows the HTTP endpoints that allow users to execute SQL queries against their databases as the “Query endpoints”. The remaining endpoints include the usual database operations like creating, deleting, and listing databases, along with import/export and time travel features. These are shown as the “CRUD endpoints”.
该图展示了允许用户以“查询端点”身份对其数据库执行 SQL 查询的 HTTP 端点。其余端点包括创建、删除和列出数据库等常规数据库操作,以及导入/导出和时间旅行功能。这些被显示为“CRUD 端点”。
The CRUD endpoints use Hyperdrive to query a Postgres cluster in our central data centers (referred to as “Core”). There are two configurations. One configuration has caching enabled and targets the Postgres read replicas. The other configuration has caching disabled and targets the Postgres primary.
CRUD 端点使用 Hyperdrive 查询我们中央数据中心(称为“Core”)中的 Postgres 集群。有两种配置。一种配置启用了缓存并指向 Postgres 只读副本。另一种配置禁用了缓存并指向 Postgres 主节点。
The Edge API Worker might invoke the D1 Eyeball Worker (depending on the type of request). The Eyeball Worker is our proxy for all requests that need to be handled by the underlying D1 Durable Objects (DO). This allows us to put all query handling inside the Eyeball Worker, including observability, automatic retries, error handling, and other query-specific behaviors.
Edge API Worker 可能会调用 D1 Eyeball Worker(取决于请求类型)。Eyeball Worker 是我们用于处理所有需要由底层 D1 Durable Objects (DO) 处理的请求的代理。这使得我们可以将所有查询处理放在 Eyeball Worker 内部,包括可观测性、自动重试、错误处理和其他特定于查询的行为。
Workers KV acts as our caching layer at the edge for read-heavy metadata and can be read by all the D1 Workers.
Workers KV 作为我们在边缘针对读取密集型元数据的缓存层,并且可以被所有 D1 Workers 读取。
The “Worker Binding” shape represents all users’ Workers with a D1 Worker binding. Requests from the D1 Binding go directly to the D1 Eyeball Worker, which in turn forwards the request to the Durable Object backing the database and proxies the response back to the User Worker.
“Worker Binding”形状代表所有具有 D1 Worker 绑定的用户 Workers。来自 D1 Binding 的请求直接发送到 D1 Eyeball Worker,后者再将请求转发给支持该数据库的 Durable Object,并将响应代理回 User Worker。
We have two methods for users to execute queries in their databases. One is over the HTTP API query endpoints, and the other is through a Worker they deploy in their accounts with a D1 Binding.
我们有两种方法让用户在其数据库中执行查询。一种是通过 HTTP API 查询端点,另一种是通过他们在账户中部署的带有 D1 Binding 的 Worker。
In terms of operations, we fold each operation into one of the following categories:
在操作方面,我们将每个操作归入以下类别之一:
- Data plane: any database query related request; should be as fast as possible and always at the edge.
- Control plane: any HTTP API operation or internal flows that run in response to a non-query user request; they can depend on central single-region components.
- Background asynchronous plane: any analytical and billing-related workflow; can be eventually consistent and runs anywhere.
- 数据平面:任何与数据库查询相关的请求;应尽可能快,并且始终位于边缘。
- 控制平面:任何 HTTP API 操作或响应非查询用户请求而运行的内部流程;它们可以依赖于中央单区域组件。
- 后台异步平面:任何分析和计费相关的工作流;可以最终一致,并在任何地方运行。
There is a lot more under the hood, so let’s dive into the key flows.
幕后还有很多内容,让我们深入探讨关键流程。
Fast on-demand database creation close to the user
靠近用户的快速按需数据库创建
Multi-tenant platforms and AI agent-driven development require creating a lot of database instances in a fast, cost-efficient, and reliable way.
多租户平台和 AI 代理驱动的开发需要以快速、成本效益高且可靠的方式创建大量数据库实例。
The following is an example Wrangler command to create a D1 database:
以下是创建 D1 数据库的 Wrangler 命令示例:
% time wrangler d1 create "demo-01"
✅ Successfully created DB 'demo-01' in region WEUR
...
0.56s user 0.13s system 42% cpu 1.614 totalWhen a user creates a D1 database, several things need to happen before the database is ready to receive queries.
当用户创建 D1 数据库时,在数据库准备好接收查询之前,需要发生几件事。
- Check that the account can create more databases against the free tier limit or its account limits.
- Generate a unique D1 database UUID, a unique Durable Object ID (newUniqueId()), and initialize the underlying Durable Object (DO) with the account and database information.
- Persist the newly created database’s information in Postgres (one row per database, including metadata and the mapping to the initialized Durable Object ID).
- 检查该账户在免费层级限制或账户限制内是否可以创建更多数据库。
- 生成一个唯一的 D1 数据库 UUID、一个唯一的 Durable Object ID(newUniqueId()),并使用账户和数据库信息初始化底层的 Durable Object(DO)。
- 将新创建的数据库信息持久化到 Postgres 中(每个数据库一行,包括元数据和与已初始化的 Durable Object ID 的映射关系)。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力