Skip to content

perf(payments): in-memory vouchers with periodic + pre-redeem durable flush (#1672) - #1706

Merged
alpergundogdu merged 12 commits into
mainfrom
claude/vouchers-memory-fsync-15073f
Aug 17, 2026
Merged

alpergundogdu merged 12 commits into
mainfrom
claude/vouchers-memory-fsync-15073f

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What & why

Closes #1672. Moves seller-side voucher persistence off the per-voucher fsync hot path (~3.18 ms/voucher, ~313 MiB/s lane ceiling). The per-lane voucher watermark now lives in memory and is mirrored to disk on a background timer, with a durable flush before every on-chain redeem.

The durability model

Lane state splits into two halves with different needs:

  • Frontier (un-redeemed watermark growth since the last redemption) is safe to lose on a crash: an honest client resumes forward (max(local, client) only advances), and a replay of un-redeemed vouchers is still on-chain-redeemable, so re-serving pays the node.
  • Redeemed watermark is floored by flush-before-redeem: the store is fsynced after a redeem batch is planned and before it is submitted on-chain, so post-crash on-disk owed ≥ submitted — the node never re-serves already-redeemed bytes for free.

Worst case on an unclean restart is one flush interval of frontier (safe) and zero already-redeemed replay.

Changes

  • Store: PoolStateStore gains flush() (default no-op); the redb impl buffers record/forget in an in-memory map hydrated at open() and fsyncs the dirty set in one transaction on flush().
  • Background flush: a task flushes every payment.voucher_commit_interval_ms (repurposed from a 5 ms group-commit gather-wait to a 5 s flush period; 0 now rejected), plus a final flush on shutdown. New lane_flush_failures metric.
  • Serve loop: the group-commit batching layer is removed (there is no VoucherAck on the wire — acceptance is implicit); vouchers advance the lane watermark in memory one at a time via commit_one_voucher.
  • Redemption: purely periodic — the threshold-hint fast path (channel, metric, top-up redrive) is deleted; the redeemer flushes then sweeps each tick; the close-monitor and shutdown sweeps flush first too.
  • Wire cleanup: the now never-emitted RetryLater reject reason is removed (a store fault aborts the stream; RetrySignal stays as an internal marker).
  • ADRs: ADR 003 §persistence and ADR 005 updated to canon.

Net +738 / −1359 across 24 files.

Testing

  • cargo clippy --workspace --all-targets clean; cargo fmt --check clean.
  • cargo nextest run -p decdn-node 860/860; -p decdn-protocol -p decdn-incentive -p decdn-node 1239/1239.
  • cargo test --no-run -p decdn-e2e --features anvil-e2e compile gate clean. The anvil_settlement_e2e redeem path was adapted (2 s redeem interval) but not executed against a live chain in this environment — please run the anvil-e2e settlement suite on CI.

Review notes

Every commit was reviewed individually; a whole-branch review caught and fixed one money-path bug (commit 613e2cc1): the redeemer originally flushed before reading/submitting the buffer, so a submitted cumulative could exceed the durable watermark and re-open the free-replay window on a crash. The flush now happens after planning and before the on-chain send, guaranteeing on-disk owed ≥ submitted.

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings August 17, 2026 15:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Note

This error may be related to your runner configuration. You can now configure runners for Copilot code review separately from Copilot cloud agent by creating a copilot-code-review.yml file with your setup steps. Read the docs for details.

alpergundogdu and others added 3 commits August 17, 2026 17:15
…y-fsync-15073f

# Conflicts:
#	crates/node/src/handlers/client/mod.rs
#	crates/node/src/handlers/client/serve_leg.rs
#	crates/node/src/handlers/client/voucher.rs
#	crates/node/src/payment_settlement.rs
#	crates/node/tests/anvil_settlement_e2e.rs
…y-fsync-15073f

# Conflicts:
#	crates/node/src/handlers/client/voucher.rs
#	crates/node/tests/client_loopback.rs
@alpergundogdu
alpergundogdu merged commit 687b2a1 into main Aug 17, 2026
50 of 52 checks passed
@alpergundogdu
alpergundogdu deleted the claude/vouchers-memory-fsync-15073f branch August 17, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

design: best-effort async voucher-watermark persistence (chain-floored) — drop fsync-before-ack

2 participants