Skip to content

relay: skip TTL deadline bump for known-permanent channels (T1a write-amp)#2125

Merged
tlongwell-block merged 2 commits into
mainfrom
eva/wamp-t1a-ttl-skip
Jul 19, 2026
Merged

relay: skip TTL deadline bump for known-permanent channels (T1a write-amp)#2125
tlongwell-block merged 2 commits into
mainfrom
eva/wamp-t1a-ttl-skip

Conversation

@tlongwell-block

Copy link
Copy Markdown
Collaborator

What

Skip the bump_ttl_deadline write on the hot ingest path when the channel is known-permanent (ttl_seconds IS NULL on the already-prefetched channel row).

Every stored channel-scoped event currently executes bump_ttl_deadline, whose predicate matches zero rows on permanent channels — a wasted write-shaped round trip and implicit transaction per message. This is T1a of the write-amplification workstream.

Safety

  • Skip only when the prefetched row positively shows ttl_seconds IS NULL. A missing row (kind:9007 pre-create, transient get_channel error) still bumps — an ephemeral channel can never archive early due to a failed prefetch.
  • A concurrent update_channel that sets a TTL resets ttl_deadline itself, making the skipped bump redundant by construction (race is benign in both directions).

Measured impact (local integration benchmark, real relay binary + PG17 + Redis)

Scenario A (zero push leases), net Postgres commits per accepted message, same DB container/channel, quiescence-gated windows, idle-rate subtracted:

qps baseline (main @ 8908bd6) T1a delta
5 11.13 10.22 −0.91
10 11.09 10.07 −1.02
30 11.00 10.02 −0.98
50 10.96 9.99 −0.97
100 11.01 9.99 −1.02

Exactly the predicted ~1 commit/message, flat across load.

Testing

  • Live functional probes on the built binary: permanent channel posts no longer touch ttl_deadline; ephemeral-channel expiration still works, including a permanent→ephemeral flip during traffic.
  • cargo test -p buzz-relay green except the known mesh_demo failure that is also red on untouched main.

Part of the write-amp series; sibling PR: T2a audit batching. Larger follow-up (T1b: push-queue skip at zero leases) targets ~5 of the remaining ~10 commits/msg.

Every stored channel-scoped event currently executes
bump_ttl_deadline, whose predicate (ttl_seconds IS NOT NULL) matches
zero rows on permanent channels — a wasted write-shaped round trip
and implicit transaction on the hot ingest path (~1 of the ~5 write
txns/message measured in the write-amplification baseline).

The ingest path already fetches the channel row once per request
(E1 §4.8). Use it: skip the bump only when the row positively shows
ttl_seconds IS NULL. A missing row (kind:9007 pre-create, transient
get_channel error) still bumps, so ephemeral channels can never
archive early due to a failed prefetch. A concurrent update_channel
that sets a TTL resets ttl_deadline itself, making the skipped bump
redundant by construction.

Ships with bench/t1a_evidence.sh (runnable-from-tip gate): asserts
zero TTL UPDATE statements via pg_stat_statements on a permanent
channel, deadline still advances on an ephemeral channel, and the
TTL-set-during-ingest race leaves a future deadline. wamp_bench is
the shared load generator used by the evidence script and the
baseline benchmark.

Part of the relay write-amplification plan (rev 3), lane T1a.
Base: 8908bd6.

Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
@tlongwell-block

Copy link
Copy Markdown
Collaborator Author

Blocking race in the TTL skip. The prefetched channel_row is not fresh at the point of the skip; it is read before event persistence/side effects. Concrete interleaving:

  1. ingest reads ttl_seconds = NULL and sets known_permanent = true;
  2. concurrent kind:9002/update transaction sets ttl_seconds = 600 and ttl_deadline = t1 + 600;
  3. this ingest stores its event later at t2 and skips bump_ttl_deadline;
  4. deadline remains t1 + 600 rather than t2 + 600.

That violates the existing invariant immediately above the changed code: every successfully stored channel-scoped event keeps the channel alive. update_channel setting a future deadline does not make the later bump redundant; it only ensures the deadline is non-null/future. The evidence script checks only ttl=600 and deadline > now, so it cannot detect this lost extension.

Please close the stale-positive race and add a barrier-forced test asserting the deadline reflects the event ordered after the TTL transition (not merely that it is future). The optimization must remain fail-safe toward bumping when permanence may have changed.

Review source: exact PR head 2515f25c9c1dddbf56bf986cf650008f411a35c7; cargo test -p buzz-relay reproduced 687 pass / the disclosed single mesh_demo failure.

Move ephemeral-channel TTL refresh into a deferred event trigger so a concurrent permanent-to-ephemeral transition cannot be missed by a stale relay prefetch. The trigger locks by channel identity before evaluating TTL state and preserves the existing best-effort failure contract.

Remove the obsolete post-insert application transaction and add Postgres coverage for permanent, ephemeral, duplicate, and forced activation-race behavior.

Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
@tlongwell-block
tlongwell-block merged commit 2e936d4 into main Jul 19, 2026
31 checks passed
@tlongwell-block
tlongwell-block deleted the eva/wamp-t1a-ttl-skip branch July 19, 2026 18:55
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.

1 participant