重新思考 lakebase 架构下的只读副本

这使副本变成一种可按工作负载使用的工具:用于发布、仪表盘、后台任务、为 AI 智能体提供只读访问,以及让智能体为自身任务增加读取容量。
只读副本背后的想法是合理的:当应用繁忙时,你增加几个只读副本,把繁重的读取路由到它们,让主库专注于写入。
但在实践中,这很少能顺利奏效。在传统的托管 Postgres 中,副本是主库的物理克隆:拥有自己的实例、自己的存储卷,以及每一页的自己的一份拷贝。这使得副本不适合用来即时扩展读取。创建副本需要几分钟到几小时,每个副本都会在你的账单上增加一份完整的数据拷贝,而且在流量高峰过去很久后,它们仍在继续运行。原本应该是一种灵活扩展读取的方式,却变成了又一个需要管理的实例。
单体副本会复制整台机器,包括磁盘。Lakebase 副本只在数据的一份共享副本之上增加计算。
与 OLTP 数据库的其他开发者体验(DX)问题一样,这是一个源于以单体方式运行系统的架构问题。在 Lakebase Postgres 中,只读副本不复制任何东西——它只是挂载到主库已使用的存储之上的又一份计算资源。它能在几秒内就绪,不增加任何存储,并且在没人读取它时可缩容到零。
Postgres 中的副本被赋予两项职责:用于高可用的备用副本,以及用于扩展读取的只读副本。本文讨论的是第二项。(高可用副本也很有意思,我们将在未来的文章中介绍我们在 Lakebase 架构中处理 HA 的方式。)
支持只读副本的理由
设想一个在 Amazon RDS 这类托管服务上的典型 Postgres 部署。Postgres 运行在单个实例上,数据存放在挂载到该实例的存储卷上。备份和快照发送到 S3。应用发出的每一次查询,无论是读还是写,都落在那台机器上。
流量持续增长。其中大部分是读请求,到了某个阶段,实例的 CPU 和内存就不足以处理它们了。在 RDS 这样的单体服务上,扩容有两条路:换到更大的实例,或者增加一个只读副本,把一部分读请求发过去。
部署一个副本,就是部署一个新实例
在 RDS 上,添加只读副本基本等同于再构建一台机器:
RDS 对主实例做一次快照
它基于该快照配置一个新实例,该实例拥有自己的计算资源和自己的存储卷,而这个存储卷必须被快照数据填满
在此期间主实例始终在接收写入,因此新实例启动后,会通过 Postgres 流式复制来追上自快照以来发生的每一次变更,并从此之后持续保持同步
只有到这时,你才能开始把读请求路由到它上面
这项工作会随数据库规模而扩展。数据库越大,意味着快照越大、需要填充的卷也越大。数据库越繁忙,意味着副本追赶上来之前要重放的 WAL 越多。在小型数据库上,这可能要花几分钟;在大型数据库上,则可能要花几个小时。
而且“可用”并不等于完成。在 RDS 上,新副本上线后,其卷仍会在后台持续从 S3 加载数据块,AWS 指出,在加载完成之前,性能可能无法达到最佳状态。这个过程对主库也并非没有代价:创建快照会短暂暂停主库的 I/O,除非它运行在 Multi-AZ 模式下;而主库上长时间运行的事务会拖慢整个过程。
你最终要面对的问题
把这一切放在一起看,你会得到一系列同根同源的问题:在单体架构中,副本就是这台机器的物理克隆。
每个副本都会复制数据:你每增加一个副本,就会复制整个数据集,而这份存储是要计费的。如果目标是读容量,那么为第二份数据副本付费纯属额外开销
副本需要时间才能就绪:由于创建一个副本要花几分钟到几小时,你无法针对流量高峰临时创建副本。你必须提前预判高峰并为此预先配置资源
副本始终处于开启状态:暂停和恢复副本都是手动过程,而恢复后的副本必须追赶暂停期间错过的所有 WAL。因此大多数团队干脆不管它:副本 7×24 小时运行,你也要为其计算和存储 7×24 小时付费
副本无法自行扩展:例如,RDS 不会自动扩展只读副本。它不会在需求上升时增加副本,也不会在需求下降时移除副本,而调整副本规格就意味着调整实例规格
结果与副本当初承诺的恰恰相反。你得到的不是一种弹性扩展读取能力的方式,而是又多了一份静态的、过度配置的基础设施需要维护。于是许多团队做出了理性的选择:为应对峰值把主库配置得过大,干脆完全不用副本;或者保留一个副本全天候运行,以防万一。
重新思考 lakebase 中的只读副本
只读副本背后的原理是成立的。真正让它在实践中失效的,是那种单体式的实现方式。
lakebase 架构为以不同方式构建副本创造了条件,让副本变得好用得多:
副本共享数据:副本是挂载在与主库相同存储之上的计算资源。新增副本不会复制任何数据,因此你只需为它所用的计算付费,存储上没有任何额外开销
副本几秒内就绪:无需创建快照,也无需填充数据卷,因此无论数据库多大,创建一个副本只需几秒钟
副本可缩容至零:当没有人从副本读取数据时,它会自行挂起,并在下一次查询时重新启动,几乎立即与主库保持同步
副本自动扩缩容:在你设定的范围内,每个副本的计算资源随读取负载增减,因此你可以按实际流量而非峰值来配置其规模
一切都始于计算与存储的分离
像 RDS 这样的服务以单体方式运行 Postgres:一台机器同时承担执行和持久化状态。Lakebase Postgres 则把这些职责拆分开来:
计算层运行 Postgres。它执行 SQL,规划并执行查询,实现 MVCC,并生成 WAL。它拥有用于共享缓冲区的内存和用于计算缓存的本地 NVMe,但不拥有任何持久化状态
存储层负责持久性和历史记录。Safekeeper 从计算层接收 WAL,并通过基于 Paxos 的协议进行复制;一旦多数派确认,事务即告提交。Pageserver 将这些 WAL 转换为 Postgres 读取的页面,并且可以重建任意 LSN 时刻的任意页面(GetPage@LSN)。对象存储保存长期、不可变的历史记录
传统副本的四个问题可以归结为两个:
它们的部署和管理很笨重。一个副本就是一台存有完整数据副本的新机器,因此构建需要几分钟到几小时,而一旦它存在,就没人愿意把它暂停、调整规格或删除
它们很昂贵。无论有没有人从它上面读取数据,这台机器都要全天候运行,而且每一个副本都会给你的存储账单增加一份完整的数据副本
将计算与存储分离可以同时解决这两个问题。由于计算层不拥有任何持久化数据,就没有什么能阻止多个计算实例挂载到同一份存储上。这就是 Lakebase 中副本背后的全部诀窍。
让副本的部署和管理变得轻量
Lakebase 副本不过是另一个计算实例
Lakebase Postgres 中的只读副本是一个独立的只读计算实例,它读取与主实例相同的存储。创建一个副本的过程如下:
控制平面在该分支上启动一个新的计算实例,并标记为只读
副本由 pageserver 提供的精简基础备份初始化,与主实例的初始化方式完全一样
它连接到 safekeepers 以流式传输 WAL
它连接到与主实例相同的 pageservers 来读取数据页
就是这样。上述步骤中没有任何一步取决于数据库的大小,因此无论数据库是小是大,创建副本都只需几秒钟。它也不会给主实例增加任何复制工作:主实例照常持续向 safekeepers 流式传输 WAL,而副本从 safekeepers 读取,而不是从主实例读取。
在 Lakebase Postgres 中,只读副本是与主库共用同一存储的第二个 Postgres 计算实例。它拥有自己的共享缓冲区和缓存,但不保存任何数据副本
Lakebase 副本不会重复复制存储
在传统架构中,N 个副本意味着数据库有 N + 1 份拷贝:一份在主库的卷上,其余每份分别在各副本的卷上,每一份都要单独配置、填充并单独计费。而在 Lakebase Postgres 中,N 个副本仍然只对应一份数据。数据在存储层只存在一份,该分支上的每一个计算实例——主库及其所有副本——都从这一份数据读取。
副本确实会保留本地缓存,包括共享缓冲区和本地 NVMe 上的缓存,但缓存不是副本。它初始为空,由 pageserver 按副本实际使用的数据页进行填充,且不保存任何持久化内容。
对于副本的部署和运行而言,这意味着:
无需配置或填充任何卷。你不必为副本挑选、配置或扩展磁盘,也不必在它能够提供读服务之前预先加载任何数据
原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(Rethinking read replicas for the lakebase architecture)
That turns replicas into a tool you can use per workload: for launches, dashboards, background jobs, read-only access for AI agents, and agents that add read capacity for their own tasks.

The idea behind read replicas makes sense: when your app gets busy, you add a couple of read-only replicas, route the heavy reads to them, and let the primary focus on writes.
In practice, that rarely works well. In traditional managed Postgres, a replica is a physical clone of the primary: its own instance, its own storage volume, and its own copy of every page. That makes replicas impractical for scaling reads on the go. They take minutes to hours to create, each one adds a full copy of the data to your bill, and they keep running long after the traffic spike is gone. What was supposed to be a nimble way to scale reads turns into one more instance to manage.
A monolith replica copies the whole machine, disk included. A lakebase replica adds compute only, on top of one shared copy of the data
Like other DX problems with OLTP databases,this is an architecture problem that comes from running things as a monolith. In Lakebase Postgres, a read replica doesn't copy anything - it's one more compute attached to the storage the primary already uses. It's ready in seconds, adds no storage, and scales to zero when nobody is reading from it.
Replicas in Postgres are asked to do two jobs: standbys for high availability, and read replicas for scaling reads. This post is about the second one. (High availability replicas are also fascinating, and we'll cover how we approach HA in the lakebase architecture in a future post.)
The case for read replicas
Picture a typical Postgres deployment on a managed service like Amazon RDS. Postgres runs on a single instance, with its data on a storage volume attached to that instance. Backups and snapshots go to S3. Every query the app sends, reads and writes, lands on that one machine.
Traffic keeps growing. Most of it is reads, and at some point the instance runs out of CPU and memory to serve them. On a monolithic service like RDS, you have two ways to scale: move to a bigger instance, or add a read replica and send some of the reads there.
Deploying a replica is deploying a new instance
On RDS, adding a read replica pretty much means building a second machine:
RDS takes a snapshot of the primary instance
It provisions a new instance from that snapshot, with its own compute and its own storage volume, and the volume has to be filled with the snapshot's data
The primary keeps taking writes the whole time, so once the new instance is up, it uses Postgres streaming replication to catch up with every change made since the snapshot, and to keep up from then on
Only then can you start routing reads to it
The work scales with the database. A bigger database means a bigger snapshot and a bigger volume to fill. A busier one means more WAL to replay before the replica catches up. On a small database, that can take minutes. On a large one, it can take hours.
And "available" doesn't mean done. On RDS, the new replica's volume keeps loading data blocks from S3 in the background after the replica comes online, and AWS notes that performance might not be at its fullest until that finishes. The process isn't free for the primary either: creating the snapshot briefly pauses its I/O unless it runs Multi-AZ, and a long-running transaction on it slows the whole thing down.
The problems you are left with
Put all of that together, and you get a set of problems that share one root cause: in a monolith, the replica is a physical clone of the machine.
Every replica duplicates the data: each replica you add copies the entire dataset, and you are billed for that storage. If the goal is read capacity, paying for a second copy of the data is pure overhead
Replicas take time to be ready: since creating one takes minutes to hours, you cannot create a replica in response to a traffic spike. You have to guess the spike in advance and provision for it
Replicas are always on: pausing and resuming a replica is a manual process, and a resumed replica has to catch up on all the WAL it missed while paused. So most teams don't bother: the replica runs 24/7, and you pay for its compute and storage 24/7
Replicas don't scale on their own: RDS, for example, does not autoscale read replicas. It won't add replicas when demand increases or remove them when it drops, and resizing a replica means resizing an instance
The result is the opposite of what replicas promised. Instead of an elastic way to scale reads, you get one more static, overprovisioned piece of infrastructure to maintain. So many teams do the rational thing: they oversize the primary for the peak and skip replicas altogether, or they keep one replica running full time, just in case.
Rethinking read replicas in the lakebase
The principle behind read replicas is sound. It's the monolithic implementation that broke them in practice.
The lakebase architecture sets the stage for building replicas differently, making them much more usable:
Replicas share the data: a replica is a compute attached to the same storage as the primary. Adding one copies nothing, so you pay for the compute it uses and nothing extra for storage
Replicas are ready in seconds: there is no snapshot to take and no volume to fill, so creating a replica takes a few seconds however large the database is
Replicas scale to zero: when nobody is reading from a replica, it suspends on its own and starts again on the next query, up to date with the primary almost immediately
Replicas autoscale: each replica grows and shrinks its compute with read load, within the limits you set, so you size it for the traffic you have, not for the peak
It all starts with compute and storage being separate
Services like RDS run Postgres as a monolith: a single machine that owns both execution and durable state. Lakebase Postgres splits those responsibilities apart:
The compute layer runs Postgres. It runs SQL, plans and executes queries, enforces MVCC, and generates WAL. It has RAM for shared buffers and local NVMe for the compute cache, and it owns no durable state
The storage layer owns durability and history. Safekeepers receive the WAL from compute and replicate it with a Paxos-based protocol; a transaction is committed once a quorum acknowledges it. Pageservers turn that WAL into the pages Postgres reads, and they can reconstruct any page as of any LSN (GetPage@LSN). Object storage keeps the long-term, immutable history
The four problems of traditional replicas come down to two:
They are heavy to deploy and manage. A replica is a new machine with a full copy of the data, so it takes minutes to hours to build, and once it exists, nobody wants to pause, resize, or remove it
They are expensive. That machine runs around the clock whether anyone is reading from it or not, and each one adds a full copy of the data to your storage bill
Separating compute from storage fixes both. Since compute owns nothing durable, nothing stops more than one compute from attaching to the same storage. That is the whole trick behind replicas in the lakebase.
Making replicas lightweight to deploy and manage
A lakebase replica is just another compute
A read replica in Lakebase Postgres is an independent, read-only compute that reads from the same storage as the primary. Creating one looks like this:
The control plane starts a new compute on the branch, marked as read-only
The replica is initialized from a slim base backup served by the pageserver, exactly like a primary is
It connects to the safekeepers to stream the WAL
It connects to the same pageservers as the primary to read pages
That is it. Nothing in that list depends on the size of the database, which is why creating a replica takes seconds on a small database and on a large one alike. It also adds no replication work to the primary: the primary keeps streaming WAL to the safekeepers as usual, and the replica reads from the safekeepers, not from the primary.
In Lakebase Postgres, a read replica is a second Postgres compute on the same storage as the primary. It has its own shared buffers and cache, and no copy of the data
Lakebase replicas don't duplicate storage
In a traditional setup, N replicas means N + 1 copies of the database: one on the primary's volume and one on each replica's volume, each provisioned, filled, and billed separately. In Lakebase Postgres, N replicas still means one copy. The data lives once in the storage layer, and every compute on the branch, the primary and all of its replicas, reads from it.
A replica does keep a local cache, in shared buffers and on its local NVMe, but a cache is not a copy. It starts empty, fills from the pageserver with the pages the replica actually uses, and holds nothing durable.
For deploying and running replicas, that means:
There is no volume to size or fill. You don't pick, provision, or grow a disk for a replica, and there is nothing to load before it can serve reads
The primary's WAL is processed once. The pageserver turns WAL into pages once for every compute on the branch. A replica only applies WAL to the pages it has cached, and reads everything else from the pageserver as of its own position in the WAL
Lakebase replicas autoscale and scale to zero
Since a replica is just a compute, it gets everything a Lakebase compute gets, configured independently of the primary:
Each replica has its own autoscaling range.The same algorithm that resizes the primary tracks the replica's CPU, memory, and working set, and resizes it in place as read load moves. Resizing never means provisioning a new instance
Replicas scale to zero. By default, a replica suspends after 5 minutes of inactivity and starts automatically with the next query. There is no WAL backlog to replay on wake: storage kept ingesting WAL while the replica was suspended, so it reads current pages and is up to date with the primary almost immediately
In Lakebase Postgres, the primary and the replica autoscale independently with traffic. The replica scales to zero when it's not needed, and both read from the same shared storage
Being lighter to deploy is half of the story. The other half is that Lakebase replicas cost much less to run, for two reasons:
You pay for compute only while it's working
A traditional replica is an instance, and an instance bills by the hour, whether it serves one query or a million. You size it for the peak, and you pay for the peak around the clock.
A Lakebase replica bills for the compute it actually uses:
Scale to zero means a replica that nobody is reading from costs no compute. A replica for a nightly export that runs for 30 minutes is billed for those 30 minutes (plus the few minutes before it suspends), not for 24 hours
Autoscaling means a replica that is active runs at the size the load needs right now. During the quiet hours it runs small, and it only grows during the spike
You don't pay for a second copy of the data
On RDS, every replica comes with its own storage volume, sized for the full dataset and billed like the primary's. Three replicas on a 1 TB database means paying for 4 TB of storage.
In Lakebase Postgres, the same setup still means paying for 1 TB. Storage is billed for the data in the storage layer, and replicas add nothing to it. Replicas add compute, never storage.
Put together, the cost of a replica follows the reads it serves, not the size of the database or the number of hours in a day. That is what turns a replica from something you budget for in advance into something you can create for a single job and delete afterwards.
Read replicas suddenly get useful: 7 use cases for lakebase replicas
When a replica is ready in seconds, adds no storage, and costs nothing while it's idle, patterns that never made sense with traditional replicas start to make sense. You can create one replica per workload instead of sharing one replica across everything. When you're done, you can delete it, or let it scale to zero.
Just a few examples on how Lakebase customers use them:
Scaling application reads
Absorbing a launch or a traffic spike
Analytics, BI, and reporting
Ad-hoc queries and production investigations
Read-only access for agents, partners, and tools
Isolating background jobs
Agents scaling reads for their own jobs
Splitting reads and writes
If your app is heavy on reads, or you want the primary to spend its resources on writes, you can send reads like feeds, product pages and search to one or more replicas. Writes, and any read that needs to see a write it just made, stay on the primary.
Why it's better on the lakebase: a lakebase replica is just more compute on the same data - nothing gets copied or moved. Reads run on the replica's own compute, so they don't slow down the primary, and each side scales on its own: the replica with read traffic, the primary with writes.
Absorbing temporary traffic spikes
Every time there’s a launch or you’re experiencing a traffic spike, you can immediately add a read replica and let it scale to zero as traffic settles.
Why it's better on the lakebase: a replica takes seconds to create, independently on how large your primary compute is or how many TBs you have in storage. You or your agent can add replicas ad-hoc as you need them, and let them autoscale and scale to zero, without worrying about sizing them or pausing them manually.
Offload analytics, BI, and reporting
You can also give dashboards, BI tools, and reports their own compute, so heavy scans don't compete with your app for CPU and cache.
Why it's better on the lakebase: the replica wakes up when a tool connects and scales to zero when it's done, so you only pay while dashboards are in use. It also has its own memory, so a big scan can't push your app's hot data out of the primary's cache. And because a replica can't write, nobody running a report can change production data by accident.
Production investigations
the primary, like how many orders are stuck in pending right now or which customers hit a checkout error in the last hour.
Why it's better on the lakebase: you can spin up a replica just for the investigation and delete it when you're done. It reads the same live data as the primary, and nothing you run on it can slow the primary down.
Read-only access for agents, partners, and tools
You can of course set up read-only roles in Postgres, but when an agent, a partner, an auditor, or a third-party tool needs to look at live data, it's often quicker to give them a read replica. They get current data, and there's no way for them to change it.
Why it's better on the lakebase: a replica rejects every write, with the guarantee coming from the compute itself - also, their queries run on the replica’s compute, without affecting the primary.
Isolating background jobs
Plenty of background work mostly reads - nightly CSV exports, a search index rebuild, a newsletter going out to your whole list. When you run them on the primary, these jobs compete with your users for CPU and cache, and the app slows down whenever they kick in. If you point them at a replica instead, your users will never notice (and you’ll gain peace of mind).
Why it's better on the lakebase: the replica wakes up when the job starts, scales up for the heavy part, and scales back to zero when the job finishes. Between runs it costs nothing in compute.
The lakebase is the right architecture for replicas
The idea behind read replicas was always right. They keep heavy, non-critical read jobs from slowing down production, and they're a way to add compute capacity temporarily. But when Postgres ran as a monolith, that was hard to do in practice: every replica was an expensive physical clone with its own upkeep.
The lakebase architecture lets you use read replicas the way they were meant to be used. A read replica is just a compute attached to storage that already exists, so it's ready in seconds, adds no storage, and only costs compute while it's running. Read replicas in the lakebase are finally what they were supposed to be: a lightweight, short-lived way to scale reads without touching the primary.
Get the latest posts in your inbox
原文为英文,正文由自动抓取并保留原文。
原文发布于 Databricks Blog(Rethinking read replicas for the lakebase architecture),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。