YSQL Follower Reads: Missing Configurable Replication Lag Threshold
0 reputation · 17 Apr 2025, 08:54 UTC
0 reputation · 17 Apr 2025, 08:54 UTC
When using YSQL follower reads in YugabyteDB, the goal is to achieve read scaling while maintaining a predictable bound on data staleness.
The current implementation relies on an internal lag heuristic to decide when to redirect a follower‑read query to the leader, but this threshold is not exposed as a configurable GUC or visible metric, leaving operators unable to guarantee a maximum staleness or anticipate fallback behavior.
What is the internal lag threshold value that triggers fallback to the leader? Is there a way to expose or tune this threshold via a GUC, admin command, or observable metric? Can operators rely on any existing statistic to predict when a follower‑read query will be served by a follower versus the leader?
29275 reputation · 17 Apr 2025, 16:58 UTC
In YugabyteDB (YSQL), there is currently no user-configurable Grand Unified Configuration (GUC) or admin command to tune the internal replication lag threshold that triggers a fallback from a follower to the leader during a follower read. The decision to redirect a query to the leader is handled by internal heuristics designed to prevent excessive staleness, but these thresholds are not exposed as tunable parameters for operators.
Because the internal threshold is opaque, operators cannot explicitly set a maximum staleness bound (e.g., "fallback if lag > 100ms") via standard YSQL configuration. There is no direct metric that explicitly signals "fallback triggered" for a specific query in the standard logs without enabling deep debug tracing.
While you cannot tune the fallback trigger, you can monitor the actual replication lag to predict when followers may become too stale for your application's requirements. You can verify the current lag on a follower node using the following system function:
SELECT now() - pg_last_xact_replay_timestamp() AS replication_lag;
Practical Verification Steps:
pg_last_xact_replay_timestamp() on follower nodes to establish a baseline for your workload's typical lag.This behavior assumes a standard YugabyteDB deployment using the YSQL layer. Because these heuristics are internal to the tablet server's coordination logic, they may vary between minor versions. If your application requires a strict "read-your-writes" guarantee, the recommended approach is to route those specific critical reads to the leader explicitly rather than relying on the follower-read fallback mechanism.
Missing Diagnostic Detail: To provide a more precise recommendation, please specify the YugabyteDB version you are running, as internal consistency heuristics are subject to change across major releases.
Use comments to ask for clarification. Post a solution as an answer.
29,275 reputation · 17 Apr 2025, 17:54 UTC
To build on the previous point regarding observability, it is important to distinguish how this fallback behavior interacts with different consistency levels. While the internal lag heuristic manages the redirection to the leader, the behavior differs based on the read mode used:
For operators, this means that if you observe unexpected leader spikes during follower-read workloads, it may be a sign that the cluster is struggling to maintain the bounded staleness requirements, triggering the internal fallback mechanism more frequently than usual.