Skip to content

feat(metrics): Time transaction start, commit lock wait, and commit - #5405

Open
colinbarry wants to merge 7 commits into
masterfrom
feat/transaction-lock-wait-metrics
Open

colinbarry wants to merge 7 commits into
masterfrom
feat/transaction-lock-wait-metrics

Conversation

@colinbarry

@colinbarry colinbarry commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

When throughput dropped while CPU stayed idle, nothing showed whether transactions were waiting on locks or doing work.

Each database now exports three histograms:

Metric Labels Value
memgraph_transaction_begin_seconds database, uuid Time to start a transaction: mostly waiting for the storage access lock (held exclusively by index creation and similar operations) and the engine lock (held by commits)
memgraph_commit_lock_wait_seconds database, uuid Time a write commit waits for the commit locks: the engine lock, plus the commit mutex under commit-lock-narrowing
memgraph_commit_seconds database, uuid Time spent in each write commit attempt, including the WAL write and SYNC replicas

Read-only transactions are not counted in the commit histograms. SHOW METRICS INFO reports them as TransactionBeginLatency, CommitLockWaitLatency, and CommitLatency, each as p50, p90, and p99 rows.

When throughput dropped with idle CPU, nothing showed whether transactions were waiting
on locks or doing work. Each database now exports three histograms:
memgraph_transaction_begin_seconds (starting a transaction, mostly the storage access
and engine lock waits), memgraph_commit_lock_wait_seconds (a write commit waiting for the
commit locks), and memgraph_commit_seconds (the whole write commit, including the WAL
write and SYNC replicas).
@colinbarry colinbarry added this to the mg-v3.15.0 milestone Nov 16, 2026
@colinbarry colinbarry added Docs needed Docs needed CI -build=release -test=core Run release build and core tests on push CI -build=release -test=e2e Run release build and e2e tests on push labels Nov 16, 2026
@colinbarry colinbarry self-assigned this Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR has potential conflicts with the following other open pull requests which modify the same files:

@colinbarry

colinbarry commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Tracking

Standard development

  • Update unit/E2E tests: show_metrics_openmetrics_values.py checks the three families exist and are observed; show_metrics.py checks their SHOW METRICS INFO rows
  • Compare the benchmarking results between the master branch this branch: done 2026-11-16 (A = merge base, B = this branch; RETURN 1, CREATE, indexed SET; 1/4/8 workers; 5 runs): no measurable cost, every difference inside run-to-run noise

CI Testing Labels

  • Select the appropriate CI test labels (CI -build=build-name -test=test-suite): coverage core, release core, release e2e

Kubernetes operator / Helm charts

Does this change affect how Memgraph is deployed or configured on Kubernetes? It likely does if it adds, removes or renames a flag; changes run-time or startup settings; touches the license, AWS settings, coordinators, replication, TLS, monitoring or another enterprise feature.

  • No, the operator and Helm charts are unaffected: new metrics only, no flags or settings change
  • Yes, and I opened the matching PRs:
    • [ Operator PR link memgraph/kubernetes-operator#XXXX ]
    • [ Helm charts PR link memgraph/helm-charts#XXXX ]

Documentation checklist

  • Add the documentation label
  • Add the bug / feature label
  • Add the milestone for which this feature is intended
    • If not known, set for a later milestone
  • Write a release note, including added/changed clauses
    • Added per-database histograms for the time to start a transaction, the time a write commit waits for the commit locks, and the time to commit: memgraph_transaction_begin_seconds, memgraph_commit_lock_wait_seconds, and memgraph_commit_seconds. Use them to tell whether transactions are waiting on locks or doing work when throughput drops while CPU is idle. #5405
  • [ Documentation PR link feat(metrics): Time transaction start, commit lock wait, and commit documentation#1928 ]
    • Is back linked to this development PR

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Release note advisory

Release note: Added per-database histograms for the time to start a transaction, the time a write commit waits for the commit locks, and the time to commit: `memgraph_transaction_begin_seconds`, `memgraph_commit_lock_wait_seconds`, and `memgraph_commit_seconds`. Use them to tell whether transactions are waiting on locks or doing work when throughput drops while CPU is idle. [#5405](https://github.com/memgraph/memgraph/pull/5405)

The note contains two sentences and extra explanatory text; the required format calls for one short sentence describing the change followed immediately by the PR link. Consider shortening to a single sentence (e.g., describing the added histograms) and then the link.

Unit tests build InMemoryStorage without metric handles, and the begin and
commit timers asserted that the handles exist. Pass the raw histogram, which
ScopedHistogramTimer already allows to be null.
Background work such as TTL, the async indexer, and replication also opens
storage accessors, and unique accessors wait for running transactions by
design, so timing in storage mixed internal waits into the begin histogram.
Time the accessor setup in the interpreter instead. The commit histogram's
help now says it covers each commit attempt, failed ones included.
@colinbarry colinbarry added the CI -build=coverage -test=core Run coverage build and core tests on push label Nov 18, 2026
…TRICS INFO

Every metric must be readable through Cypher. Add the begin, commit lock wait,
and commit histograms to SHOW METRICS INFO as p50, p90, and p99 rows, as for
the other latency histograms. The deprecated JSON endpoint is unchanged.
Replicas observe memgraph_commit_seconds and memgraph_commit_lock_wait_seconds
when they apply a replicated commit. The Help text now says so.
@sonarqubecloud

Copy link
Copy Markdown

… released

Observing commit_lock_wait_seconds right after taking the engine lock
held the lock while the histogram's mutex was taken, so a scrape copying
that histogram could stall every commit on the database. Measure the wait
under the lock, and record it on scope exit, after the engine lock and
the commit mutex are released.

@internal-review-service-bot internal-review-service-bot 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.

Automated review by the Memgraph review service

Verdict: APPROVE WITH NITS
Findings: 0 blocker, 1 non-blocking, 0 nit (1 inline) · 2 pre-existing · 2 nits (collapsed)

Pre-existing, not introduced by this PR (2)
  • src/dbms/inmemory/replication_handlers.cpp:608-613

    [Non-blocking] Strict-sync replica 2nd-phase finalization is not measured by either commit histogram

  • src/storage/v2/disk/storage.cpp:1816

    [Non-blocking] On-disk (ON_DISK_TRANSACTIONAL) databases never populate the commit histograms, producing permanently-zero CommitLatency/CommitLockWaitLatency percentiles

Nits (2)
  • design.md:20

    [Nit] Release note and PR summary say "Time to commit" without "attempt" qualifier

    vs correct Help text at prometheus_metrics.cpp:797–801.

  • ?:1

    [Nit] Empty CI commit 52bb882b9

    zero file changes.

2 findings that state they need no change are omitted; they are in the full report.

Comment thread src/storage/v2/inmemory/storage.cpp Outdated
auto commit_serializer = mem_storage->LockCommitMutexIfNarrowing();

auto engine_guard = std::unique_lock{storage_->engine_lock_};
mem_storage->metric_handles_.commit_lock_wait_seconds.Observe(commit_lock_timer.Elapsed().count());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Non-blocking] commit_lock_wait_seconds.Observe() runs while holding commit_mutex_ + engine_lock_, adding prometheus-internal mutex contention to the commit critical section

@colinbarry
colinbarry marked this pull request as ready for review November 23, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI -build=coverage -test=core Run coverage build and core tests on push CI -build=release -test=core Run release build and core tests on push CI -build=release -test=e2e Run release build and e2e tests on push Docs needed Docs needed feature feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant