Skip to content

1.16 Phase 1 Consolidation, Baseline Benchmark and Exit Review: Exit Evidence

Evidence · Step: 1.16 Phase 1 Consolidation, Baseline Benchmark and Exit Review

Step 1.16 Phase 1 Consolidation, Baseline Benchmark and Exit Review
Date 2026-11-13
Benchmark machine GitHub-hosted runner: 4 vCPUs (AMD EPYC 7763), 16 GB, disk sync 0.08 ms; PostgreSQL 18.7
Result Passed: the Phase 1 gate is met, and Phase 2 can start
Deliverable Where
Baseline benchmark crates/bench (mezgeb-bench); the bench workflow; the baseline below
Coverage report The coverage workflow, after every merge to main; the numbers below
Phase 1 review notes Phase 1 review below
Release tag v0.1.0-engine, on the commit that merges this evidence
Documentation invariants.md, Phase 1 tests; db-schema.md, What one posting costs; ADR evidence at the end of Phase 1; ADR-0007, step 1.16

The work arrived in four pull requests: the benchmark (#40), coverage and two new engine tests (#41), the lock-wait fix (#42), and this one.

Task Done
Fix all open Phase 1 issues Yes. No issue is open on GitHub. The baseline found one bug, fixed in #42: see the lock wait
A baseline benchmark: postings/s and p50/p99 latency for 2-line and 4-line entries, uniform accounts and one hot account Yes: four scenarios, with p50, p90, p99, p99.9 and max
Update the docs: invariants traceability, db-schema, ADR evidence Yes: see Deliverables
Tag v0.1.0-engine After this evidence merges

187 tests pass across the workspace, on every push in CI, against PostgreSQL 18:

Kind Where
Unit Every crate, mostly crates/core
Property proptest in account.rs, ids.rs, money.rs, validate.rs and the reference ledger’s state machine (model/tests.rs)
Database crates/engine/tests/: schema, posting, corrections, idempotency, holds, expiry, verification
Differential differential.rs: PostgreSQL against the reference ledger, 64 random sequences
Concurrency concurrency.rs, expiry.rs, and concurrent repeats in idempotency.rs
Crash faults.rs (every write at 4 fault points); in CI, crash_writer killed mid-posting, then verified; scripts/chaos.sh, 20 kills under load (1.15)
Simulation mezgeb-sim in CI: 10,000 operations on the reference ledger, and 2,000 on PostgreSQL with faults. In step 1.15: 1,000,000 on PostgreSQL

New in this step, in concurrency.rs:

  • a_locked_account_is_busy_after_the_lock_timeout: a posting that waits out its lock timeout is refused with Busy and writes nothing. Once the lock is released, the same posting succeeds
  • serialization_failures_are_retried_or_refused_cleanly: under SERIALIZABLE, conflicting postings either give up with Contention, writing nothing, or are retried until each posts once
  • the_lock_timeout_bounds_the_whole_wait: see below

The first two cover engine paths that no test had run before: the coverage report showed them.

cargo llvm-cov --workspace, database tests included, from the coverage workflow after #42:

Crate Lines Missed Line coverage
mezgeb-core 3,185 35 98.9%
mezgeb-engine 1,795 65 96.4%
Workspace 6,278 843 86.6%

What the engine misses is mostly defensive: mappings of database errors (unique and foreign-key violations) that the engine’s own checks prevent, which only a race could reach. In core, most misses are in model/api.rs, the trait that lets tests drive the reference ledger and PostgreSQL alike. The tools (sim, bench, cli) are lower because their database modes run as programs, not under cargo test.

The benchmark repeated 3 times with variance under 10%

Section titled “The benchmark repeated 3 times with variance under 10%”

Each scenario ran 3 times, interleaved with the others. The spread is (max − min) / mean of the three runs’ throughput, stricter than the standard deviation.

Scenario Runs (postings/s) Spread
2-line, uniform accounts 710, 696, 704 2.1%
4-line, uniform accounts 516, 524, 497 5.3%
2-line, one hot account 297, 302, 287 5.1%
4-line, one hot account 220, 223, 219 1.6%

Pass: every spread is under 10%.

Why GitHub Actions, not this PC. On Docker Desktop on Windows, a disk sync took from 5 ms to 35 ms from one minute to the next (pg_test_fsync run six times). Every commit waits for one, so runs there differed by 25–80%. On the GitHub runner a sync takes 0.08 ms, and the runs agree.

Runners differ. GitHub does not always give the same CPU. Earlier runs of the same code got an EPYC 9V45 or 9V74, and posted 1.4–2× faster on uniform accounts. Statements unaffected by any change, such as the line insert, were slower by the same factor. A comparison with this baseline must therefore use the same runner model, recorded in the run’s summary.

mezgeb-bench through the engine, as it ships (without test-hooks). Each run uses a fresh database with 1,000 wallets, and 16 connections posting at once in a closed loop (each posts again as soon as its last posting returns), measured for 60 s after 5 s of warm-up. Every run ended with a clean full verification. There were no deadlocks or retries. Workflow run 43488208349, 2026-11-13.

Scenario Postings/s p50 p99 p99.9 Max Refused Busy
2-line, uniform accounts 704 22.6 ms 41.2 ms 54.0 ms 88 ms 0
4-line, uniform accounts 513 30.1 ms 66.8 ms 87.1 ms 121 ms 0
2-line, one hot account 295 10.5 ms 638 ms 4,127 ms 4,985 ms 171 (about 0.3%)
4-line, one hot account 220 13.9 ms 609 ms 4,026 ms 5,000 ms 257 (about 0.6%)

Latencies are means over the three runs; Max is the highest of the three. There is no target yet: this is the baseline that Phase 3 (step 3.01) measures against.

Where the time goes (pg_stat_statements, one 10 s run per scenario):

  • Uniform accounts: the line insert is the costliest statement (2.2 ms for 2 lines, 3.7 ms for 4), then the account updates. The deferred balance check runs once per row, 3 times for a 2-line entry and 5 times for a 4-line one. See db-schema.md.
  • Hot account: the lock SELECT averages 40–45 ms, almost all of it queueing for the one row. The rest of the posting takes under 2 ms.

The hot account’s tail. The queue for one row is not fair. The median posting waited about 10 ms, but one in a thousand waited about 4 s, and a few reached the 5 s lock timeout and were refused with Busy. That is the engine’s designed answer: the posting writes nothing, and the client retries. The benchmark now reports Busy as a rate and fails only on other errors. In run 43488208349 it still counted them as errors, which is why that run’s summary says FAIL. Splitting hot accounts is step 3.03.

Coordinated omission. In a closed loop, a slow posting delays the worker’s next one, so the latencies are those of a saturated system, not of clients arriving at their own pace. mezgeb-bench --rate N runs an open loop instead, measuring each latency from when the posting was due. The baseline above is closed loop.

Found: the lock timeout did not bound the wait

Section titled “Found: the lock timeout did not bound the wait”

PostOptions::lock_timeout (5 s) was documented as the longest a posting waits for its accounts. PostgreSQL, however, applies lock_timeout to each lock wait separately. On a hot account, one SELECT … FOR NO KEY UPDATE can wait several times in a row: first behind another waiter, then behind that waiter’s transaction.

In the first 60 s run on GitHub, the slowest postings waited up to 29 s. Some reached the mezgeb_app role’s 30 s transaction_timeout, which closes the session. Each was refused with Busy and wrote nothing, and every verification was clean, so no money was wrong. But clients waited six times longer than promised and lost their connections. The benchmark then failed millions of postings in a row, because each worker kept its one connection for the whole run.

Fix (#42).

  • Each write’s transaction also sets statement_timeout to the same value, in the same round trip, so a whole statement is bounded. A cancellation (57014) is Busy.
  • the_lock_timeout_bounds_the_whole_wait makes a posting wait twice, 200 ms and then 300 ms, with a 400 ms timeout. Before the fix it posted after 633 ms; after it, the posting is refused with Busy at 400 ms.
  • The benchmark takes a pooled connection per posting, as a service does.

In the baseline after the fix, the longest posting took 5.000 s, and no session was closed.

Criterion Result
Every Phase 1 invariant has a passing test Pass. Every test planned for steps 1.01–1.15 is implemented and passing: Phase 1 tests. The rest are planned for Phases 2 and 3
No open correctness bugs Pass. No open issues; the one bug found here is fixed (#42)
Benchmark numbers recorded (no target yet; this is the baseline) Pass: above
Gate Evidence Result
Balanced posting INV-1 in the types (1.06), the deferred trigger (1.08) and verification (1.13) Pass
Immutable posting Triggers and role privileges; corrections are new entries (1.08, 1.12) Pass
Idempotent posting One entry per key, under concurrent repeats and after a lost response (1.11); at every fault point and through 20 database kills (1.15) Pass
Concurrent-safe posting Exactly one of 100 overspends succeeds; no deadlocks (1.10); none in this baseline either Pass
Holds and reversals 1.12, 1.14: the capture and expiry race had one winner in 20,000 holds Pass
A verifier that catches all planted corruption 7 of 7 planted corruptions found, each with its account and entry (1.13); a drifted held account found (1.14) Pass
Chaos runs clean 20 kills under load, 0 violations; 1,000,000 simulated operations with 82,556 injected faults, 0 differences (1.15) Pass

Result: passed. Phase 2 can start.

What Phase 1 built. A posting engine on PostgreSQL that:

  • posts any balanced multi-line entry, atomically
  • keeps running totals that verification proves against the lines
  • refuses overdrafts under concurrency
  • applies each idempotency key once, through crashes and lost responses
  • reverses and adjusts without changing history
  • holds money in two phases, with expiry

A reference ledger (MemLedger) gives the right answer to every operation, and the property tests, the differential test and the simulator compare the engine with it.

Known limits, and the step that addresses each:

Limit Step
One hot account handles about 300 postings/s on this runner. Its lock queue is unfair: about 0.3–0.6% of postings wait out the 5 s timeout 3.03 (hot accounts)
The deferred balance check runs once per row instead of once per entry 3.01 (performance)
Benchmark numbers depend on the runner’s CPU, which GitHub varies 3.01 (reference hardware)
The simulator’s reference ledgers grow with the run, so a million operations on PostgreSQL ran as ten runs of 100,000 2.14 (simulator v2)
Building the whole workspace turns on the engine’s test-hooks for every crate (Cargo merges features). The service must be built on its own (cargo build -p mezgeb-cli) 3.11 (deployment)
No API, periods, hash chain or receipts yet (INV-10, INV-11, INV-26) 2.05–2.09

Waiting on others, outside the code:

  • the accountant’s review of the accounting foundations and the chart of accounts (1.01, 1.05)
  • answers to the regulatory questions (1.03)

Neither blocks Phase 2’s engineering. Both must be answered before real money.