Inside Sub-Second FX Routing: A Streaming Liquidity Graph
A cross-border payment is not one transaction. It is a chain of conversions, each with its own spread, liquidity, and settlement delay. Most routing engines evaluate that chain once, at the moment of authorization, using rates that may be seconds old. At RoaPay we treat routing as a continuously updated graph problem: every quote, fill, and settlement event changes the graph, and every payment is routed against the latest version.
Why sub-second routing is hard
The naive approach is to query every liquidity provider, collect quotes, and pick the cheapest path. That works at low volume and collapses under load. At peak, RoaPay evaluates more than 40,000 candidate paths per second. Provider APIs have different latencies, rate limits, and quote validity windows. A path that is optimal at t may be stale by t+200ms.
We needed three properties at once:
- Freshness. No payment should route on a rate older than its validity window.
- Determinism. The same inputs and graph state must produce the same route, so audits can reconstruct decisions.
- Backpressure safety. When a provider degrades, the engine must shed load without failing payments unnecessarily.
A streaming liquidity graph
The graph has nodes for currencies, accounts, and clearing venues. Edges represent conversion or transfer capacity, annotated with spread, fee, available liquidity, and expected settlement time. Edges are updated by a stream processor that consumes provider quotes, internal fills, and treasury balance changes.
type Edge = {
from: Currency;
to: Currency;
venue: VenueId;
spreadBps: number;
feeMinor: number;
availableMinor: number;
settlesInMs: number;
validUntil: number; // epoch ms
};
function route(payment: Payment, graph: Graph): Route {
return graph
.paths(payment.source, payment.target, { maxHops: 3 })
.filter((p) => p.every((e) => e.validUntil > now() + payment.slaMs))
.map((p) => score(p, payment))
.sort((a, b) => a.cost - b.cost)[0];
}
Quotes are not stored as a list of provider responses. They are folded into the current graph state with a monotonic version. Each routing decision records the graph version it used, which makes replay exact: given the same version and payment, the engine produces the same route.
Routing objectives
Cost is only one dimension. A route that is 3 basis points cheaper but settles two hours later is usually the wrong choice for a payout with a same-day SLA. The scorer combines:
- All-in cost — spread, explicit fees, and expected FX exposure.
- Settlement confidence — historical on-time rate for the venue and corridor.
- Liquidity headroom — available capacity relative to the payment size and near-term forecast.
- Concentration risk — how much of the day's flow already depends on the same venue.
Merchants can choose a profile — cost, balanced, or speed — per corridor. The profile changes the weights, not the candidate set, so behavior stays explainable.
Results
Over the last quarter, the streaming graph reduced both cost and latency while handling a 3.4x increase in peak volume:
| Metric | Before | Now |
|---|---|---|
| Routing decision p50 | 310 ms | 42 ms |
| Routing decision p95 | 1.8 s | 180 ms |
| Average all-in FX cost | 38 bps | 26 bps |
| Same-day payout SLA attainment | 96.1% | 99.4% |
Failure handling
When a venue stops publishing quotes, its edges are marked stale and excluded from new routes within one tick. In-flight payments that already locked a rate are honored; new payments fall back to the next-best path. If the graph loses too many edges for a corridor, the engine switches that corridor to a conservative mode with wider spreads and explicit manual review thresholds.
“The most important property is not the cheapest route. It is that a rate we quoted is a rate we can settle.”
— RoaPay FX engineering
What we are exploring
Next we are testing predictive liquidity: using short-horizon payment forecasts to reserve capacity on venues before demand arrives. We are also adding a public route-explanation endpoint so merchants can see why a payment took a particular path, including the graph version and the scoring contributions. Both are aimed at the same goal — making a millisecond decision legible to a human auditor.
一笔跨境支付并不是一次交易,而是一条兑换链:每一次兑换都有自己的点差、流动性与清算延迟。多数路由引擎只在授权那一刻评估一次,而且使用的汇率可能已经滞后数秒。在 RoaPay,我们把路由视为一张持续更新的图:每一次报价、成交与清算事件都会改变这张图,每一笔支付都基于最新版本进行路由。
亚秒级路由难在哪里
朴素做法是查询所有流动性提供方、收集报价、选择最便宜的路径。低峰期可行,高并发时立刻崩溃。峰值时 RoaPay 每秒评估超过 40,000 条候选路径。各提供方 API 的延迟、限流与报价有效期都不一样;在 t 时刻最优的路径,到 t+200ms 可能已经失效。
我们同时需要三个性质:
- 新鲜度。 任何支付都不能使用超过有效期 window 的汇率。
- 确定性。 相同输入与相同图状态必须产生相同路由,审计才能复现决策。
- 背压安全。 某个提供方降级时,引擎要能卸掉负载,而不是无谓地让支付失败。
流式流动性图
图中的节点是币种、账户与清算场所;边代表兑换或调拨能力,附带点差、费用、可用流动性与预计清算时间。流处理器消费提供方报价、内部成交与资金余额变化,持续更新这些边。
type Edge = {
from: Currency;
to: Currency;
venue: VenueId;
spreadBps: number;
feeMinor: number;
availableMinor: number;
settlesInMs: number;
validUntil: number; // epoch ms
};
function route(payment: Payment, graph: Graph): Route {
return graph
.paths(payment.source, payment.target, { maxHops: 3 })
.filter((p) => p.every((e) => e.validUntil > now() + payment.slaMs))
.map((p) => score(p, payment))
.sort((a, b) => a.cost - b.cost)[0];
}
报价不会以“提供方响应列表”的形式保存,而是带着单调递增的版本号折叠进当前图状态。每次路由决策都会记录使用的图版本,因此回放是精确的:给定相同版本与支付,引擎会得出相同路由。
路由目标
成本只是其中一个维度。一条便宜 3 个基点、但晚两小时清算的路径,对有当日 SLA 的下发来说通常是错误选择。评分函数会综合:
- 全成本 —— 点差、显性费用与预期汇率敞口。
- 清算确定性 —— 该场所与走廊的历史准时率。
- 流动性余量 —— 可用能力相对支付金额与近期预测的比例。
- 集中度风险 —— 当日流量已经有多少依赖同一场所。
商户可以为每条走廊选择 cost、balanced 或 speed 配置。配置只改变权重,不改变候选集,因此行为始终可解释。
结果
过去一个季度,流式图在峰值流量增长 3.4 倍的同时,降低了成本与延迟:
| 指标 | 之前 | 现在 |
|---|---|---|
| 路由决策 p50 | 310 ms | 42 ms |
| 路由决策 p95 | 1.8 s | 180 ms |
| 平均全成本汇兑 | 38 bps | 26 bps |
| 当日下发 SLA 达成率 | 96.1% | 99.4% |
故障处理
某个场所停止发布报价后,其边会在一个 tick 内标记为过期并被排除出新路由。已经锁定汇率的在途支付继续履约;新支付回退到次优路径。如果某条走廊失去过多边,引擎会将该走廊切换为保守模式:扩大点差并设置显式人工复核阈值。
“最重要的不是最便宜的路由,而是我们报出的汇率一定能够清算。”
—— RoaPay 汇兑工程团队
下一步探索
我们正在测试预测性流动性:利用短周期支付预测,在需求到来前预占场所能力。同时计划开放路由解释端点,让商户看到某笔支付为何选择特定路径,包括图版本与各项评分贡献。两者服务于同一个目标——让毫秒级决策对人类审计者保持可读。