Skip to content

perf(0132): write only newly-discovered assets per reconcile - #155

Merged
karczuRF merged 2 commits into
developfrom
perf/0132_ledger-processor-full-asset-registry-reemit
Jul 29, 2026
Merged

perf(0132): write only newly-discovered assets per reconcile#155
karczuRF merged 2 commits into
developfrom
perf/0132_ledger-processor-full-asset-registry-reemit

Conversation

@karczuRF

Copy link
Copy Markdown
Collaborator

Summary

  • The live ledger-processor re-emitted the entire 200k-row prices.assets registry to ClickHouse on every reconcile run — a 9,413× write amplification (1.9B rows/day, 98.6% of all DB writes) billed as **$337/mo** of AWS→Hetzner Lambda egress. Reported by the BE team, confirmed in prod system.part_log. ReplacingMergeTree dedups server-side, so this was pure cost, not correctness — which is why every functional test passed.
  • Capture the surrogate-id watermark (next_id) before a run and write only the assets interned at/after it via write_new_assets; a run that discovers no new assets writes nothing (empty guard the full write_assets lacked). Rows and their deterministic sac_address are identical to before.
  • Full write_assets retained for the one-shot backfill/discovery/oracle callers; prices.assets/asset_metadata single-writer split (0067) preserved.
  • BE's late-SAC concern doesn't apply here: sac_address_of is a deterministic derivation from the asset identity, complete when interned — no side table needed.
  • Tests: 2 registry unit tests + 2 sink-boundary tests (no new → 0 rows, one new → 1 row); workspace compiles, clippy clean.
  • Spawns backlog task 0133 (egress / write-volume alarm) so a future amplification surfaces on a dashboard, not a bill.

Post-deploy check: system.part_log daily assets write volume should drop from ~1.9B to ~thousands/day.

karczuRF added 2 commits July 29, 2026 11:38
The live ledger-processor re-emitted the entire ~200k-row asset registry
to ClickHouse on every reconcile run — a 9,413x write amplification
(1.9B rows/day, 98.6% of all DB writes) billed as ~$337/mo of AWS to
Hetzner Lambda egress. Reported by the BE team, confirmed in prod
system.part_log. The ReplacingMergeTree dedups server-side, so the bug is
pure cost, not correctness — which is why every functional test passed.

Capture the surrogate-id watermark (next_id) before a run and write only
the assets interned at/after it via write_new_assets; a run that discovers
no new assets writes nothing (empty guard the full write_assets lacked).
Rows and their deterministic sac_address are identical to before. The full
write_assets is kept for the one-shot backfill/discovery/oracle callers.

BE's late-SAC concern does not apply here: sac_address_of is a
deterministic derivation from the asset identity, complete when interned.

Spawns 0133 (egress/write-volume alarm) so a future amplification surfaces
on a dashboard, not a bill.
…ciler

Code review found a data-loss regression in the first cut. The Reconciler
is a warm Arc singleton reused across Lambda invocations, so recapturing
the surrogate-id watermark from the in-memory registry each run was unsafe:
a run that interned new assets and then failed a later write left next_id
advanced but the assets unpersisted, so the redelivered doorbell's next run
recaptured a watermark past them and skipped them forever — orphaning the
candles that reference their ids. The old full re-emit self-healed this.

Track a durable persisted_asset_watermark in ProcessingState that advances
only after a successful asset write; a failed run leaves it unmoved so the
retry re-writes those assets. Write assets before candles so the dimension
row is referentially ahead of the facts. Add an O(1) steady-state guard in
write_new_assets (since >= watermark) to avoid scanning the ~200k registry
on every idle reconcile. Fix a stale write_asset_metadata doc comment.

Regression test assets_from_a_failed_run_are_written_on_the_next_run fails
against the pre-fix code and passes now.
@karczuRF
karczuRF merged commit 096e8e4 into develop Jul 29, 2026
3 checks passed
@karczuRF
karczuRF deleted the perf/0132_ledger-processor-full-asset-registry-reemit branch July 29, 2026 10:07
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