5D Library › 零人工多站点容错架构、支付防欺诈状态机与被动现金流排队论 · English

第八章 九项自动审计与一项预注册实验

九个问题

本章把全书归结为九个可以由脚本检查的问题,用于任何一个「零人工」站群的部署配置,包括作者自己的。它们不是安全或经营建议。

  1. 回单是否只核对少于 8 位的尾号?(第一章)
  2. 即时交付的商品,被抓时是否一无所失?(第一章)
  3. 自购返佣的关联图是否使用了 IP 子网?(第二章)
  4. 用自购解锁二级是否比标价便宜?(第二章)
  5. 订单处理是否没有转移表?(第三章)
  6. 幂等键的有效期是否短于 511 分钟的重试计划?(第三章)
  7. 工作进程是否少于突发所需的 4 个、且没有限流?(第四章)
  8. 服务流式请求时,排空时间是否短于 300 秒?(第五章)
  9. 现金储备是否低于三个月冲击所需的 90,000?(第六章)

审计程序

程序把九个问题写成九个自动标志,用于两套配置:一套是常见的「快速搭建」(核对 4 位尾号,即时交付且无抵押,关联图含子网,邀请 3 人解锁,无转移表,幂等键 10 分钟,1 个工作进程,立即切换,储备 15,000),另一套是按本书各章修正后的「加固」配置。

推导

# Chapter 8. [p15] Eight automatic flags for a zero-touch site network.
# F1 receipts matched on fewer than 8 trailing digits (p01);
# F2 instant delivery with nothing at stake when caught (p02);
# F3 shared-subnet edges in the self-referral graph (p03);
# F4 level 2 cheaper by self-referral than its list price (p04);
# F5 order handlers without a transition table (p05);
# F6 idempotency keys kept shorter than the retry schedule (p06);
# F7 fewer workers than the burst requirement, and no shedding (p08);
# F8 drain time under 300 s while long streams are served (p10);
# F9 cash reserve below what a three-month shock needs (p13).

DESIGNS = {
    "quick": dict(tail=4, instant=True, stake=0, subnet=True, m=3,
                  table=False, ttl=10, workers=1, shed=False, drain=0,
                  reserve=15000),
    "hardened": dict(tail=None, instant=True, stake=200, subnet=False,
                     m=8, table=True, ttl=1440, workers=4, shed=True,
                     drain=300, reserve=90000),
}

def flags(d):
    return [
        ("F1 tail-digit receipt match", d["tail"] is not None
         and d["tail"] < 8),
        ("F2 instant delivery, no stake", d["instant"] and d["stake"] == 0),
        ("F3 subnet edges in referral graph", d["subnet"]),
        ("F4 self-referral below list", d["m"] * (39 + 3) < 299),
        ("F5 no transition table", not d["table"]),
        ("F6 key TTL < 511 min", d["ttl"] < 511),
        ("F7 workers < 4 without shedding", d["workers"] < 4
         and not d["shed"]),
        ("F8 drain < 300 s", d["drain"] < 300),
        ("F9 reserve < 90,000", d["reserve"] < 90000),
    ]

if __name__ == "__main__":
    for name, d in DESIGNS.items():
        fl = flags(d)
        n = sum(f for _, f in fl)
        print("   {} design: {} of {} flags".format(name, n, len(fl)))
        for label, f in fl:
            print("     [{}] {}".format("X" if f else " ", label))
    print("\nReading: each flag is a sentence from an earlier chapter")
    print("turned into a test that a script can run on a deployment's")
    print("settings. The quick build trips all nine. The hardened one")
    print("trips none, which says only that its settings agree with")
    print("this book's models - not that the models are right.")
#    quick design: 9 of 9 flags
#      [X] F1 tail-digit receipt match
#      [X] F2 instant delivery, no stake
#      [X] F3 subnet edges in referral graph
#      [X] F4 self-referral below list
#      [X] F5 no transition table
#      [X] F6 key TTL < 511 min
#      [X] F7 workers < 4 without shedding
#      [X] F8 drain < 300 s
#      [X] F9 reserve < 90,000
#    hardened design: 0 of 9 flags
#      [ ] F1 tail-digit receipt match
#      [ ] F2 instant delivery, no stake
#      [ ] F3 subnet edges in referral graph
#      [ ] F4 self-referral below list
#      [ ] F5 no transition table
#      [ ] F6 key TTL < 511 min
#      [ ] F7 workers < 4 without shedding
#      [ ] F8 drain < 300 s
#      [ ] F9 reserve < 90,000
#
# Reading: each flag is a sentence from an earlier chapter
# turned into a test that a script can run on a deployment's
# settings. The quick build trips all nine. The hardened one
# trips none, which says only that its settings agree with
# this book's models - not that the models are right.

快速搭建九项全中;加固配置一项也不中。但一项不中只说明它的配置与本书的模型一致,并不说明模型本身是对的:模型的参数(突发的强度、伪造的比例、冲击的长度)都需要用真实数据检验。这正是下面的实验要做的事的一部分。

一项预注册实验

假说 · 尚未验证

研究方案。 买家按账户随机分入「核验后放行」(HOLD)组或「先放行后抽查」(OPTIMISTIC)组;后者抽查三分之一的订单,并按第一章设置抵押与级联撤销。主要结局:发起结账者中完成购买的比例,预期从 0.27 升到 0.30。必须报告的伤害:事后查出的伪造订单比例,预期从 0.010 升到 0.015。检验:双侧 0.05,把握度 0.90 (Kohavi et al. 2020)。由与作者无经济关系的独立分析者按预先登记的代码执行;结果不论正负都发表。

推导

# Chapter 8. [p16] Sample sizes for a pre-registered release experiment.
# Buyers are randomised by account between HOLD (access after the
# receipt is verified, typically hours later) and OPTIMISTIC (access
# at once; a third of orders audited; stake and cascading revocation
# as in p02). Two-sided 0.05, power 0.90, normal approximation.
# (a) primary: completed purchases among checkout starts, 0.27 under
#     HOLD vs 0.30 under OPTIMISTIC;
# (b) harm: share of orders later found fraudulent, 0.010 vs 0.015;
# (c) the same harm if the audit share were raised to catch a 0.010
#     vs 0.012 difference.
import math
from statistics import NormalDist

def n_props(p1, p2, alpha=0.05, power=0.90):
    z = NormalDist().inv_cdf
    pb = (p1 + p2) / 2
    a = z(1 - alpha / 2) * math.sqrt(2 * pb * (1 - pb))
    b = z(power) * math.sqrt(p1 * (1 - p1) + p2 * (1 - p2))
    return math.ceil((a + b) ** 2 / (p1 - p2) ** 2)

if __name__ == "__main__":
    a = n_props(0.27, 0.30)
    b = n_props(0.010, 0.015)
    c = n_props(0.010, 0.012)
    print("   (a) checkout starts per arm, 0.27 vs 0.30  : {:,d}".format(a))
    print("   (b) orders per arm, fraud 0.010 vs 0.015   : {:,d}".format(b))
    print("   (c) orders per arm, fraud 0.010 vs 0.012   : {:,d}".format(c))
    print("\nReading: the conversion question needs a few thousand")
    print("checkouts per arm. The fraud question needs over ten thousand")
    print("orders per arm - more than a year's orders at 40 a day split")
    print("into two arms - and a finer difference several years. Fraud")
    print("must be tracked by audit for the life of the policy, not")
    print("settled by one experiment.")
#    (a) checkout starts per arm, 0.27 vs 0.30  : 4,757
#    (b) orders per arm, fraud 0.010 vs 0.015   : 10,375
#    (c) orders per arm, fraud 0.010 vs 0.012   : 57,154
#
# Reading: the conversion question needs a few thousand
# checkouts per arm. The fraud question needs over ten thousand
# orders per arm - more than a year's orders at 40 a day split
# into two arms - and a finer difference several years. Fraud
# must be tracked by audit for the life of the policy, not
# settled by one experiment.

转化率的问题每组需要 4,757 位发起结账者。伪造率的问题需要每组 10,375 笔订单;若要分辨 0.010 与 0.012 这样更细的差别,每组需要 57,154 笔。按每天 40 笔订单计算,后者要好几年。因此方案把转化率定为主要结局,把伪造率作为必须报告的伤害,并在整个政策的生命周期内持续抽查,而不是指望一次实验就给出答案。

小结

九个问题——尾号、抵押、子网、解锁价格、转移表、幂等期限、工作进程、排空时间、现金储备——都可以写成自动审计。检验「先放行后抽查」的实验以转化率为主要结局,每组 4,757 位发起结账者;伪造率需要长期抽查来跟踪。

参考文献

Kohavi, Ron, Diane Tang, and Ya Xu. 2020. Trustworthy Online Controlled Experiments: A Practical Guide to a/b Testing. Cambridge University Press.

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).