5D Library › 零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论 · English
第六章 流量分配、升级独立域名与现金储备
30 个站点之间分配付费流量
共享内核上的 30 个子站点,转化率各不相同,事先也不知道。每天有一笔固定的付费流量(这里设为 3,000 位访客)要分给它们。平均分配最简单,但会把大量流量浪费在早已看出转化很差的站点上。这是一个多臂老虎机问题。汤普森抽样是最早也最简洁的解法之一:对每个站点的转化率保持一个后验分布,每次按「从后验中抽一个值、谁最大给谁」的方式分配,于是越可能最好的站点得到越多流量,而尚不确定的站点仍有机会被试 (Thompson 1933; Russo et al. 2018)。
程序让 30 个站点的真实转化率从 Beta(2, 98) 抽取(平均 2%),连续 60 天,比较平均分配与按天批量的汤普森抽样。
推导
# Chapter 6. [p11] Thompson sampling of paid traffic across 30 sites.
# Thirty sites on the shared kernel convert visitors with unknown rates
# p_i drawn once from Beta(2, 98) (mean 2%). A daily budget of 3,000
# paid visitors is split across them for 60 days. UNIFORM gives each
# site 100 a day; THOMPSON (in daily batches) draws 200 samples from
# every site's Beta(1 + conversions, 1 + misses) posterior and gives
# each site the share of draws in which it came out best.
# 50 replications with fixed seeds. Reported: conversions per run,
# regret against always using the best site, and the share of traffic
# that went to the three best sites.
import random
SITES, DAILY, DAYS, REPS, DRAWS = 30, 3000, 60, 50, 200
def run(rng, rates, policy):
conv = [0] * SITES
miss = [0] * SITES
sent = [0] * SITES
for _ in range(DAYS):
if policy == "uniform":
alloc = [DAILY // SITES] * SITES
else:
wins = [0] * SITES
for _ in range(DRAWS):
d = [rng.betavariate(1 + conv[k], 1 + miss[k])
for k in range(SITES)]
wins[d.index(max(d))] += 1
alloc = [DAILY * w // DRAWS for w in wins]
for i, n in enumerate(alloc):
c = sum(rng.random() < rates[i] for _ in range(n))
sent[i] += n
conv[i] += c
miss[i] += n - c
top = sorted(range(SITES), key=lambda k: -rates[k])[:3]
return sum(conv), sum(sent[k] for k in top) / sum(sent)
if __name__ == "__main__":
out = {"uniform": [], "thompson": []}
best = []
for r in range(REPS):
rng = random.Random(75011 + r)
rates = [rng.betavariate(2, 98) for _ in range(SITES)]
best.append(max(rates) * DAILY * DAYS)
for pol in out:
out[pol].append(run(rng, rates, pol))
ideal = sum(best) / REPS
print(" ideal (best site only): {:.1f} conversions".format(ideal))
print(" policy conversions regret share to top 3")
for pol, res in out.items():
c = sum(x for x, _ in res) / REPS
s = sum(y for _, y in res) / REPS
print(" {:9s} {:12.1f} {:8.1f} {:16.3f}".format(
pol, c, ideal - c, s))
print("\nReading: Thompson sampling moves most paid traffic to the")
print("few sites that convert best and recovers most of the gap")
print("to the ideal; uniform splitting wastes it on sites whose low")
print("rate was obvious within days.")
# ideal (best site only): 9905.2 conversions
# policy conversions regret share to top 3
# uniform 3473.4 6431.9 0.100
# thompson 9435.2 470.0 0.911
#
# Reading: Thompson sampling moves most paid traffic to the
# few sites that convert best and recovers most of the gap
# to the ideal; uniform splitting wastes it on sites whose low
# rate was obvious within days.若一开始就知道哪个站点最好、把全部流量给它,60 天可得 9905.2 次转化。平均分配只得到 3473.4 次,与理想相差 6431.9;汤普森抽样得到 9435.2 次,只差 470.0,并把 0.911 的流量给了最好的三个站点。
什么时候把子站升级为独立域名
子站点表现好时,可以把它从共享内核的一个子路径升级为独立域名,以获得独立的品牌与搜索流量。设独立域名每年额外花费 6,000(域名、独立内容、外链与监控),能让每年 20,000 次自然访问提高 30%,每次转化价值 60。升级划算的条件是转化率高于 。每个站点每天得到 100 位访客,程序比较三种升级规则:后验规则( 时升级, 时放弃)、计数规则(满 20 次转化就升级)、观测率规则(第 7 天起观测转化率一超过 就升级,60 天仍不够就放弃)。
推导
# Chapter 6. [p12] When to promote a site from a sub-path to its own domain.
# A standalone domain costs K = 6,000 a year (domain, separate content,
# links, monitoring) and is assumed to lift a site's 20,000 organic
# visits a year by u = 30%; a conversion is worth 60. Promotion pays
# when p > c* = K / (u 20,000 60) = 1/60. Each site receives 100
# visitors a day; its rate p ~ Beta(2, 98) as in p11. Rules, checked
# daily for up to 365 days:
# BAYES - promote when P(p > c* | data) >= 0.95 under a Beta(1, 1)
# prior, drop when it is <= 0.05;
# COUNT - promote after 20 conversions (a common rule of thumb);
# PEEK - promote as soon as the observed rate exceeds c* after at
# least 7 days, drop after 60 days below it.
# 2,000 simulated sites. Reported: wrong promotions (p < c*), missed
# good sites, mean days to a decision, undecided after a year.
import math
import random
K, U, VISITS, VALUE = 6000.0, 0.30, 20000, 60.0
C_STAR = K / (U * VISITS * VALUE)
def betacf(a, b, x):
qab, qap, qam = a + b, a + 1, a - 1
c, d = 1.0, 1 - qab * x / qap
d = 1 / d
h = d
for m in range(1, 300):
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1 / (1 + aa * d)
c = 1 + aa / c
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1 / (1 + aa * d)
c = 1 + aa / c
h *= d * c
if abs(d * c - 1) < 1e-12:
break
return h
def beta_cdf(x, a, b):
lbt = (math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b)
+ a * math.log(x) + b * math.log(1 - x))
if x < (a + 1) / (a + b + 2):
return math.exp(lbt) * betacf(a, b, x) / a
return 1 - math.exp(lbt) * betacf(b, a, 1 - x) / b
def decide(rng, p, rule):
conv = n = 0
for day in range(1, 366):
c = sum(rng.random() < p for _ in range(100))
conv, n = conv + c, n + 100
if rule == "BAYES":
up = 1 - beta_cdf(C_STAR, 1 + conv, 1 + n - conv)
if up >= 0.95:
return True, day
if up <= 0.05:
return False, day
elif rule == "COUNT":
if conv >= 20:
return True, day
else:
if day >= 7 and conv / n > C_STAR:
return True, day
if day >= 60:
return False, day
return None, 365
if __name__ == "__main__":
print(" break-even rate c* = {:.4f}".format(C_STAR))
print(" rule wrong promotions missed good mean days undecided")
for rule in ("BAYES", "COUNT", "PEEK"):
rng = random.Random(75012)
wrong = missed = und = days = good = bad = 0
for _ in range(2000):
p = rng.betavariate(2, 98)
d, t = decide(rng, p, rule)
days += t
if p > C_STAR:
good += 1
missed += d is not True
else:
bad += 1
wrong += d is True
und += d is None
print(" {:6s} {:17.3f} {:12.3f} {:10.1f} {:10.3f}".format(
rule, wrong / bad, missed / good, days / 2000, und / 2000))
print("\nReading: the count rule promotes nearly every site, since")
print("twenty conversions arrive within weeks at almost any rate;")
print("peeking at the observed rate promotes almost a fifth of the")
print("sites that do not pay. The posterior rule decides most sites")
print("within weeks, with about one wrong promotion in seventeen;")
print("sites near c* stay undecided longest, and for them the")
print("choice hardly matters.")
# break-even rate c* = 0.0167
# rule wrong promotions missed good mean days undecided
# BAYES 0.060 0.054 23.2 0.024
# COUNT 0.995 0.000 20.3 0.003
# PEEK 0.182 0.011 29.0 0.000
#
# Reading: the count rule promotes nearly every site, since
# twenty conversions arrive within weeks at almost any rate;
# peeking at the observed rate promotes almost a fifth of the
# sites that do not pay. The posterior rule decides most sites
# within weeks, with about one wrong promotion in seventeen;
# sites near c* stay undecided longest, and for them the
# choice hardly matters.计数规则几乎把所有站点都升级了(不该升级的站点有 0.995 被升级),因为在任何转化率下,几周之内都会攒满 20 次转化。观测率规则把 0.182 的不该升级的站点升级了:早期的观测率噪声很大,一看到超过就升级,相当于反复偷看。后验规则平均 23.2 天作出决定,错误升级 0.060,漏掉该升级的站点 0.054,一年后仍未决定的只有 0.024——那些都是转化率接近 、升不升级差别不大的站点。
「被动现金流」需要多少储备
现金流也是一个队列:订单的钱要过一段时间才到账,成本却每天都要付。程序用复合泊松过程描述它:每天平均 40 笔订单,单笔金额对数正态(平均 60、变异系数 4,少数高价牌照占大头),钱在下单 7 天后到账,3% 的订单在 30 天后退款或拒付,固定成本每天 1,800。期望的日净收入是 528.0。
推导
# Chapter 6. [p13] "Passive" cash flow as a compound Poisson process.
# Orders arrive at 40 a day (Poisson); an order's value is lognormal
# with mean 60 and CV 4 (a few licences dominate). Money reaches the
# account 7 days after the order (settlement delay); 3% of orders are
# refunded or charged back 30 days after the order. Fixed costs are
# 1,800 a day (servers, AI inference, part-time staff). Starting from a
# reserve R0, the program simulates 400 years of daily balances and
# reports the share of years in which the balance ever goes negative,
# with and without a revenue shock (orders halve for 90 days from day
# 30, as after a search-ranking loss).
import math
import random
LAM, MEAN, CV, COST = 40.0, 60.0, 4.0, 1800.0
SIG = math.sqrt(math.log(1 + CV * CV))
MU = math.log(MEAN) - SIG * SIG / 2
def poisson(rng, lam):
k, p, L = 0, 1.0, math.exp(-lam)
while True:
p *= rng.random()
if p < L:
return k
k += 1
def year(rng, r0, shock):
inflow = [0.0] * 400
out = [0.0] * 400
bal, low = r0, r0
for d in range(365):
lam = LAM / 2 if shock and 30 <= d < 120 else LAM
for _ in range(poisson(rng, lam)):
v = rng.lognormvariate(MU, SIG)
inflow[d + 7] += v
if rng.random() < 0.03:
out[min(399, d + 30)] += v
bal += inflow[d] - out[d] - COST
low = min(low, bal)
return low < 0, bal
if __name__ == "__main__":
rng = random.Random(75013)
print(" expected net per day: {:.1f}".format(
LAM * MEAN * 0.97 - COST))
print(" costs before the first settlement: {:,.0f}".format(7 * COST))
print(" reserve R0 P(negative) plain P(negative) with shock")
for r0 in (15000, 20000, 30000, 60000, 90000):
a = sum(year(rng, r0, False)[0] for _ in range(400)) / 400
b = sum(year(rng, r0, True)[0] for _ in range(400)) / 400
print(" {:10,d} {:19.3f} {:24.3f}".format(r0, a, b))
print("\nReading: a business that earns on average still needs a")
print("reserve, because cash arrives a week late and the costs do")
print("not wait. A few weeks of costs covers ordinary noise; a")
print("three-month traffic shock needs several times as much. The")
print("queue of money is as real as the queue of requests.")
# expected net per day: 528.0
# costs before the first settlement: 12,600
# reserve R0 P(negative) plain P(negative) with shock
# 15,000 0.028 0.995
# 20,000 0.000 0.980
# 30,000 0.000 0.970
# 60,000 0.000 0.318
# 90,000 0.000 0.000
#
# Reading: a business that earns on average still needs a
# reserve, because cash arrives a week late and the costs do
# not wait. A few weeks of costs covers ordinary noise; a
# three-month traffic shock needs several times as much. The
# queue of money is as real as the queue of requests.平均每天赚 528.0 的生意,在第一笔回款到账之前就要先付 12,600 的成本。储备 15,000 时,平常年份有 0.028 的概率出现负余额;储备 20,000 以上,平常年份就不再出现负余额。但遇到订单减半三个月的冲击(例如搜索排名下跌),储备 30,000 时 0.970 的年份会出现负余额,60,000 时仍有 0.318,90,000 时才降到 0。「被动现金流」的「被动」,是以储备为前提的。复合泊松过程的破产概率是保险数学的经典问题,这里只用模拟给出具体参数下的数字。
假说 · 尚未验证
作者的诠释(尚未验证): 作者的站群收入在单个搜索引擎上的集中度很高,三个月的流量冲击并非罕见;因此储备应当按冲击情景而不是平常年份设定,至少相当于 50 天的固定成本。什么会证明这一判断错:若过去三年的真实流量记录中,没有任何连续 30 天的订单下降超过三成,那么冲击情景可能过于悲观,储备可以按平常年份设定。
小结
汤普森抽样把付费流量集中到最好的几个站点,与理想只差 470.0 次转化,而平均分配差 6431.9 次。升级独立域名应当用后验规则:计数规则几乎全部升级,观测率规则错误升级 0.182。现金流需要储备:平常年份 20,000 已够,三个月的订单减半需要 90,000。
参考文献
Fan, Y. (2026). Zero-Touch Multi-Site Fault Tolerance, Payment Anti-Fraud State Machines and Passive Cashflow Queuing: Receipt-Hash Deduplication, Self-Referral Blocking, Host-Header Routing and Availability Bounds [零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090201 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).