Skip to content

perf(do): strip redundant reads on the metric-history hot path - #184

Merged
prisis merged 2 commits into
alphafrom
perf/metric-history-coalesce
Jul 24, 2026
Merged

prisis merged 2 commits into
alphafrom
perf/metric-history-coalesce

Conversation

@prisis

@prisis prisis commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

What

recordMetricHistory runs on every ctx.metrics.* call across every package. A trace + bench (from the notify observability work) showed it costs ~19 µs/call, dominated by a synchronous SQLite write — but each call also re-ran two avoidable statements: CREATE TABLE IF NOT EXISTS and a bucketExists SELECT.

This memoizes both per SQL handle, with no change to durability semantics — every call still upserts synchronously, only the reads around it are removed.

  • ensureMetricHistoryTable no-ops after the first call per handle (WeakSet).
  • A per-handle known-bucket set lets an in-minute repeat skip the existence read (and, transitively, the distinct-series cap scan + retention trim) and go straight to the upsert. It stores membership only, never values, so a stored aggregate can never be made wrong; a hit means the row exists because we wrote it. Bounded (cleared past a cap) and a WeakMap, so a fresh isolate (post-hibernation) starts cold and re-consults the DB.

Impact

Bench (metric-history-record.bench.ts, repeated same-(series, minute) measurement — the hot path a metrics-heavy handler or a per-recipient fan-out hits):

ns/call hz
baseline ~17.4 µs 57,485
this change ~12.2 µs 82,249

~1.4× (≈30% off the hot path), entirely from removing the two redundant statements.

Not in scope (deliberately)

The remaining ~12 µs is the durable upsert itself. Cutting that needs deferred coalescing — folding N same-series counts in memory and flushing once per dispatch — which changes durability timing (a mid-dispatch crash would lose the buffered window) and touches the RPC/alarm/subscription flush lifecycle. That's a semantics decision for its own change, not this one.

Verification

  • 179 tests pass across metrics/telemetry (metric-history, metric-buffer, function-metrics, telemetry, shard-do.admin), incl. a new test asserting 50 repeats of one series fold to count: 50 (the cache-hit path stays exact).
  • lint:types + eslint + prettier clean.

Complements (independent of) the notify delivery-observability PR #183, which reduces how often notify hits this path; this makes the path itself cheaper for every caller.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance Improvements

    • Improved the efficiency of durable metrics recording, especially for repeated measurements within the same time interval.
    • Reduced unnecessary database work while preserving metric history, series limits, and retention behavior.
    • Added safeguards to keep performance optimizations bounded during extended operation.
  • Tests

    • Added benchmark coverage to track metrics recording performance under repeated workloads.

recordMetricHistory runs per ctx.metrics.* call. Its durable upsert is the
irreducible cost, but each call also re-ran CREATE TABLE IF NOT EXISTS and a
bucket-existence SELECT. Memoize both per SQL handle:

- ensureMetricHistoryTable no-ops after the first call per handle (WeakSet).
- A per-handle known-bucket set lets an in-minute repeat skip the existence
  read (and the cap-scan/trim) and go straight to the upsert. Membership only,
  never values, so a stored aggregate can never be made wrong; the set is
  bounded (cleared past a cap) and a fresh isolate re-reads from the DB.

No durability change — every call still upserts synchronously; only the reads
around it are removed. Bench (repeated same-series measurement): ~17.4us →
~12.2us (~1.4x). Cutting the upsert itself would need deferred coalescing,
which is a durability-semantics change left for a separate change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NjFm7WWd7aqnKduj2thnNU
@netlify

netlify Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit 096ba21
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a639456f4199700085c9e7e
😎 Deploy Preview https://deploy-preview-184--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

💤 Files selected but had no reviewable changes (1)
  • packages/do/src/metric-history.ts
⛔ Files ignored due to path filters (1)
  • packages/do/__tests__/metric-history.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d0323909-ebd3-4e14-86a0-3d25bab3ff3f

📥 Commits

Reviewing files that changed from the base of the PR and between a2960d6 and 096ba21.

⛔ Files ignored due to path filters (1)
  • packages/do/__tests__/metric-history.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (1)
  • packages/do/src/metric-history.ts

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The metric history write path now memoizes table initialization and (series, bucket) existence per SQLite handle, bounds the cache, and adds a Vitest benchmark for repeated same-bucket recordings.

Changes

Metric history caching

Layer / File(s) Summary
Per-handle table and bucket caching
packages/do/src/metric-history.ts
Table setup and bucket existence checks use per-handle memoization, with bounded cache updates after writes.
Repeated bucket benchmark
packages/do/__bench__/metric-history-record.bench.ts
A warmed SQLite harness benchmarks repeated recordings for a fixed series and minute bucket.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • anolilab/lunora#158: Introduced the durable metric history implementation that this caching optimization modifies.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, conventional-commit style, and accurately summarizes the main performance change.
Description check ✅ Passed The description explains why, what, impact, and verification clearly, with only minor template-heading gaps.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/metric-history-coalesce

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/do/src/metric-history.ts`:
- Line 119: Capture the stable SQL handle once per request before
instrumentation can re-proxy this.sql, then pass that captured handle to
recordMetricHistory(). Update the request flow in shard-do.ts around
currentStmtSamples so the same handle is reused for metric-history recording and
both caches remain keyed consistently.
- Around line 274-282: Update the bucket-recording flow around the
cache.add(cacheKey) call so cacheKey is added only after confirming the bucket
still exists following insertion and retention. Do not mark retention-deleted
buckets as known; their next write must perform the existence and retention
checks instead of being skipped by the cache.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 13b1c1da-366a-4054-a387-0abcf2bdbbb5

📥 Commits

Reviewing files that changed from the base of the PR and between a07c091 and a2960d6.

⛔ Files ignored due to path filters (1)
  • packages/do/__tests__/metric-history.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
📒 Files selected for processing (2)
  • packages/do/__bench__/metric-history-record.bench.ts
  • packages/do/src/metric-history.ts

Comment thread packages/do/src/metric-history.ts Resolved
Comment thread packages/do/src/metric-history.ts Resolved
@codspeed

codspeed Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 36.61%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 100 untouched benchmarks
🆕 1 new benchmark
⏩ 60 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ broadcastWhisper to 1024 members 1,281.2 µs 937.8 µs +36.61%
🆕 repeated same-(series, bucket) measurement N/A 423.9 µs N/A

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/metric-history-coalesce (096ba21) with alpha (7acf42b)2

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on alpha (a07c091) during the generation of this report, so 7acf42b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

A late, out-of-window sample can be inserted then immediately removed by the
retention trim in the same call; caching it as known let its next write skip
both the existence check and the trim, resurrecting the expired row until a cold
bucket re-triggered trimming (CodeRabbit review, PR #184).

Cache only a bucket confirmed to already exist (bucketExists), so a
freshly-created bucket is re-checked on its next write and cached only once
durable. Adds a regression test: an old bucket trimmed by a far-ahead bucket
stays gone when re-recorded. Bench unchanged (~1.4x); steady-state repeats are
still the cached hot path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NjFm7WWd7aqnKduj2thnNU
@prisis
prisis merged commit 8fbbdf2 into alpha Jul 24, 2026
44 checks passed
@prisis
prisis deleted the perf/metric-history-coalesce branch July 24, 2026 16:49
prisis added a commit that referenced this pull request Jul 24, 2026
Sync the cloud observability + security-hardening branch with alpha (OTLP
resource attributes #182, notify observability #183, do perf #184, workerd
custom-spans test #180, ai-gateway correlation #181, and release bumps).
Clean auto-merge — no conflicts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — 096ba216 Deployed Jul 24, 2026 by prisis via Benchmarks #914
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant