Skip to content

feat(metrics): Export replica state, replication lag, and role gauges - #5024

Open
colinbarry wants to merge 11 commits into
masterfrom
feat/replication-health-gauges
Open

colinbarry wants to merge 11 commits into
masterfrom
feat/replication-health-gauges

Conversation

@colinbarry

@colinbarry colinbarry commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Dashboards had no way to see whether a replica is healthy, how far it lags, or which role an instance holds, without running Cypher queries such as SHOW REPLICAS.

Data instances now export these gauges:

Metric Labels Value
memgraph_replica_state mg_instance, database, mode, state 1 for the replica's current state, 0 for the others. States use the SHOW REPLICAS names: ready, replicating, recovery, invalid, diverged
memgraph_replica_transactions_behind mg_instance, database, mode Committed transactions the replica is behind the main
memgraph_replication_role role 1 for the instance's current role (main or replica)
memgraph_accepts_writes 1 if replication lets the instance accept writes: it is a main, and in HA its writes are enabled. Licence and storage limits are not reflected
memgraph_registered_replicas Replicas registered on the main

The values are read from the instance's own replication state at scrape time, so they work without HA. A scrape never waits on a lock: if the replication state or a replica's state is busy, it keeps the previous values. mode is the replica's replication mode (sync, async, or strict_sync). An ASYNC replica catches up in periodic recovery rounds and normally shows invalid between them, so dashboards need the mode to tell that apart from a SYNC replica falling behind. The lag calculation is now shared with SHOW REPLICATION LAG, and the state and mode names with SHOW REPLICAS.

SHOW REPLICATION ROLE gains a second column, writeable, with the same value as memgraph_accepts_writes: false on a replica, and on a main whether it accepts writes (always true outside HA).

@colinbarry

colinbarry commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Tracking

Standard development

  • Update unit/E2E tests: e2e: replication_metrics.py (replica state, lag, role, accepts-writes, registered replicas), show.py for the new SHOW REPLICATION ROLE column; HA, strict-sync, and Jepsen tests updated for the column
  • Compare the benchmarking results between the master branch this branch: not needed: every value is read at scrape time, nothing runs on the query path (replica state is read with a try-lock)

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: no flags change; the dashboards that use these metrics follow in a separate helm-charts PR
  • 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 Prometheus gauges for replication health: each replica's state and the transactions it is behind, per database and with its replication mode, plus the instance's replication role, whether it accepts writes (memgraph_accepts_writes), and the number of registered replicas. Use them to watch replicas and failover from dashboards and alerts without running SHOW REPLICAS. SHOW REPLICATION ROLE now also returns a writeable column, so update any client that reads its rows by position. #5024
  • [ Documentation PR link feat(metrics): Export replica state, replication lag, and role gauges documentation#1932 ]
    • Is back linked to this development PR

@colinbarry colinbarry self-assigned this Oct 6, 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 Oct 6, 2026
@colinbarry
colinbarry force-pushed the feat/replication-health-gauges branch from 1a204e5 to 1387087 Compare October 6, 2026 08:59
@github-actions

github-actions Bot commented Oct 6, 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 added this to the mg-v3.15.0 milestone Oct 7, 2026
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
D Reliability Rating on New Code (required ≥ A)
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Dashboards had no way to see whether a replica is healthy or how far it
lags, or which role an instance holds, without running Cypher queries.
Data instances now export memgraph_replica_state and
memgraph_replica_txns_behind for each replica and database, plus
memgraph_replication_role, memgraph_main_writeable, and
memgraph_registered_replicas. The values are read from the instance's own
replication state at scrape time, so they work without HA and never
block a scrape.
The killed replica stayed out of the cleanup fixture's reach, so its data
directory survived and a rerun started it with the replica role already
set, failing setup.
The replica gauges used a replica label, while the HA instance gauges and
the replication throughput histograms use mg_instance for the same
names. Use mg_instance, so dashboards can join replica and cluster views.
@colinbarry
colinbarry force-pushed the feat/replication-health-gauges branch from 12eeca1 to 0a94622 Compare November 10, 2026 12:56
An ASYNC replica catches up in periodic recovery rounds, so it is normally
reported behind between rounds. Without the mode, dashboards could not tell
that from a SYNC replica that has fallen behind. memgraph_replica_state and
memgraph_replica_txns_behind now carry a mode label with the values SHOW
REPLICAS uses.
The replica gauges' mode label had tests for sync and async replicas only.
A STRICT_SYNC replica, registered alongside an ASYNC one as clusters allow,
is now checked to report mode="strict_sync".
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Release note advisory

Release note: Added Prometheus gauges for replication health: each replica's state and the transactions it is behind, per database and with its replication mode, plus the instance's replication role, whether it accepts writes (`memgraph_accepts_writes`), and the number of registered replicas. Use them to watch replicas and failover from dashboards and alerts without running `SHOW REPLICAS`. `SHOW REPLICATION ROLE` now also returns a `writeable` column, so update any client that reads its rows by position. [#5024](https://github.com/memgraph/memgraph/pull/5024)

The content is informative and the PR link matches, but it contains multiple sentences and is longer than the required single short sentence; please condense into one sentence followed immediately by the PR link in the specified format.

The scrape read each replica's state with a blocking spin lock, which a commit holds across its replication RPC, so a scrape could wait out an RPC timeout while holding the replication state read lock. The scrape now tries that lock and drops the whole snapshot when it is busy, leaving the gauges at their previous values. A new replica's gauges are also built before they enter the map, so a failed Add can't leave a null entry behind.
SHOW REPLICAS spelled out each replication mode name by hand, duplicating the names the metrics use. It now takes them from ReplicationModeName.
…TION ROLE

memgraph_main_writeable had no Cypher view, and its name hid that it reflects only replication, not licence or storage limits. The gauge is now memgraph_accepts_writes, and SHOW REPLICATION ROLE gains a writeable column with the same value: false on a replica, and on a main whether it accepts writes. Tests that read SHOW REPLICATION ROLE rows now expect the second column.
@sonarqubecloud

Copy link
Copy Markdown

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