How We Scaled Cross-Border Order Reads With a Read-Replica Setup
How We Scaled Cross-Border Order Reads With a Read-Replica Setup
我们的订单数据库在很长一段时间内运行良好。直到一个“黑色星期五”将其击垮——不是因为写入压力,而是因为读取压力。结账流程虽然正常,但面向买家的页面(订单历史、物流追踪、“我的包裹在哪里”)变得极其缓慢,导致客服工单激增。这个悖论很简单:促销越火爆,用户刷新订单页面的频率就越高,而刷新频率越高,系统就越慢。Taocarts 是支撑这一技术栈的跨境平台,事实证明,使用只读副本(Read Replicas)是我们能采取的侵入性最小的修复方案。以下是我们所做的工作以及令我们感到意外的地方。
Diagnose before you split
It is tempting to jump straight to “add replicas.” We first confirmed the shape of the problem with query metrics, and it mattered. Our workload was overwhelmingly read-heavy. Roughly 95% of queries were reads — order lists, tracking nodes, item lookups. Writes (new orders, status changes, inventory decrements) were a small fraction but obviously critical. That ratio is exactly the scenario replicas are good at. If your workload were write-heavy, splitting reads would barely help. The slow queries themselves were not even badly written. They were just numerous, and they were all hitting the same primary that was also serving checkout writes. Read contention was the issue.
在拆分前先进行诊断
直接跳到“添加副本”的做法很诱人。我们首先通过查询指标确认了问题的形态,这一点至关重要。我们的工作负载绝大多数是读取密集型的。大约 95% 的查询是读取操作——如订单列表、追踪节点、商品查询等。写入操作(新订单、状态变更、库存扣减)占比很小,但显然至关重要。这种比例正是副本发挥作用的典型场景。如果你的工作负载是写入密集型的,拆分读取几乎没有帮助。那些缓慢的查询本身写得并不差,只是数量太多,而且它们全都挤在同一个同时处理结账写入的主库上。读取竞争才是问题的核心。
The topology we landed on
One primary handles all writes. Two replicas handle reads. Simple, boring, effective. Checkout, order creation, and inventory updates go to the primary. Buyer-facing reads — order history, tracking pages, product lookups — go to replicas. Admin analytics and internal reports go to a dedicated replica so a heavy report cannot slow down customer pages. The routing lives in the data access layer, not scattered across controllers. We have a single place that decides “write or read,” and a context flag that pins a request to the primary when needed. Scattering replica routing through the codebase is how you end up with subtle bugs.
我们最终采用的拓扑结构
一个主库处理所有写入,两个副本处理读取。简单、枯燥但有效。结账、订单创建和库存更新指向主库。面向买家的读取操作——如订单历史、追踪页面、商品查询——指向副本。管理分析和内部报表则指向一个专用副本,这样繁重的报表就不会拖慢客户页面。路由逻辑位于数据访问层,而不是分散在各个控制器中。我们有一个统一的地方来决定“写入还是读取”,并使用一个上下文标志在需要时将请求锁定到主库。如果将副本路由逻辑分散在代码库中,最终会导致难以察觉的 Bug。
The consistency trap nobody warns you about
Here is the part that cost us a week. A buyer places an order (write to primary) and immediately lands on the order confirmation page (read from replica). Replication lag means the replica does not have the row yet. The page shows “order not found.” The buyer panics and places a second order. Now you have a duplicate — and a support ticket. We fixed this with a read-after-write rule: any request that just performed a write is pinned to the primary for a short window (we use a few seconds). After that, reads can go back to replicas. Most frameworks let you set a per-request “use primary” flag; that is enough. The general lesson: replicas are eventually consistent, and user flows that assume immediate consistency will break. Identify those flows explicitly and route them to the primary.
没人提醒过你的“一致性陷阱”
这是让我们耗费了一周时间的部分。买家下单(写入主库)后立即跳转到订单确认页面(从副本读取)。由于复制延迟,副本中还没有这条记录。页面显示“未找到订单”。买家感到恐慌并下了第二个订单。现在你有了重复订单,也多了一张客服工单。我们通过“写后读”规则解决了这个问题:任何刚执行过写入的请求,都会在短时间内(我们设为几秒钟)被锁定到主库。之后,读取操作可以回到副本。大多数框架都允许你设置单次请求的“使用主库”标志,这就足够了。通用的教训是:副本是最终一致的,任何假设即时一致性的用户流程都会出错。请明确识别这些流程,并将它们路由到主库。
Connection pooling and failover
Two practical things: Pool per node, not global. We keep separate connection pools for the primary and each replica. A global pool with mixed targets causes headaches when one node is slow — you cannot isolate it. Health checks and automatic failover. If a replica lags too far or goes down, the router should skip it. We monitor replica lag and pull a node out of rotation past a threshold. If the primary fails, we do not auto-promote a replica — that is a deliberate manual decision, because a bad auto-promotion is worse than downtime.
连接池与故障转移
有两件务实的事情:按节点建立连接池,而不是全局连接池。我们为每个主库和副本维护独立的连接池。当某个节点变慢时,混合目标的全局连接池会引发头痛的问题——你无法将其隔离。其次是健康检查和自动故障转移。如果副本延迟过高或宕机,路由器应跳过它。我们监控副本延迟,一旦超过阈值就将该节点移出轮询。如果主库故障,我们不会自动提升副本——这是一个审慎的人工决策,因为错误的自动提升比停机更糟糕。
What the numbers looked like
After the split, buyer-facing page p95 latency dropped by more than half during peak, and checkout stayed flat because it no longer competed with hundreds of tracking refreshes. Support tickets about “slow pages” essentially disappeared. We also learned to watch replica lag as a first-class metric. It is the number that quietly determines whether your users see fresh data.
数据表现如何
拆分后,在高峰期,面向买家页面的 P95 延迟下降了一半以上,而结账流程保持平稳,因为它不再需要与成百上千次的物流追踪刷新竞争。关于“页面缓慢”的客服工单基本消失了。我们还学会了将副本延迟视为一项核心指标。正是这个数字,默默决定了你的用户看到的是否是最新数据。
Lessons worth keeping
Split reads only after confirming the workload is read-heavy. Measure first. Route reads in one place, with a clear “primary” override for read-after-write flows. Treat replication lag as a product problem, not just an infra metric — stale data confuses users. Pool connections per node so one slow replica does not poison the rest. Keep failover deliberate. Automatic promotion is a decision, not a default. Read replicas are not glamorous. But they are the kind of unglamorous plumbing that lets a checkout survive its busiest hour without falling over.
值得铭记的经验
只有在确认工作负载是读取密集型之后才进行读写拆分。先进行测量。在统一位置进行读取路由,并为“写后读”流程提供明确的“主库”覆盖机制。将复制延迟视为产品问题,而不仅仅是基础设施指标——陈旧的数据会困扰用户。按节点进行连接池管理,这样单个缓慢的副本就不会拖累其他节点。保持故障转移的审慎性。自动提升是一个决策,而不是默认行为。只读副本并不光鲜,但它们正是那种能让结账系统在最繁忙时刻依然稳健运行的、朴实无华的基础设施。