Does YugabyteDB guarantee read-your-writes consistency for cross-tablet queries in multi-region DocDB deployments?
0 reputation · 22 Dec 2023, 00:26 UTC
0 reputation · 22 Dec 2023, 00:26 UTC
In a multi-region YugabyteDB cluster using DocDB storage, determining whether a client session that writes a row and then immediately issues a read spanning multiple tablets is guaranteed to observe its own write requires understanding how hybrid logical timestamps order visibility across tablet leaders when follower reads are enabled, and whether the default YSQL isolation level changes the outcome.
YSQL defaults to READ COMMITTED, though snapshot isolation can be requested. It is unclear whether that default affects cross-tablet visibility in the same way as single-tablet operations. Additionally, automatic tablet splitting can rebalance data while queries are in flight, raising the question of whether this introduces a window where a follower replica lags behind the leader beyond the HLT window.
Assuming a recent stable YugabyteDB version with default YSQL settings and follower reads permitted, does read-your-writes consistency hold across tablet boundaries when the read is served by a non-leader replica? How does the READ COMMITTED default interact with DocDB's timestamp ordering for multi-tablet transactions? Can tablet splitting temporarily violate read-your-writes semantics for ongoing cross-tablet queries?
Assumes a recent stable YugabyteDB 2.x cluster with DocDB, default YSQL READ COMMITTED, and follower reads permitted. No. Enabling follower reads does not by itself guarantee read-your-writes consistency for a cross-tablet read. A same-session read observes its own write only when the read hybrid time is at or after the write’s commit hybrid time and the serving follower replica has applied through that time within the configured follower-read staleness bound. Otherwise the cross-tablet read may wait for replication, fall back to the tablet leader, or return a snapshot that predates the write.
The practical risk in multi-region deployments is cross-region replication delay between the write leader and a local follower. If that lag exceeds the follower-read staleness bound, the local follower cannot serve the read fresh enough, so read-your-writes is violated unless the read waits or goes to the leader. This is a likely explanation, not a versioned guarantee; behavior can change between YugabyteDB releases.
BEGIN;
INSERT INTO orders (id, region) VALUES (1, 'us-east');
EXPLAIN SELECT o.id, i.item FROM orders o JOIN items i ON o.id = i.order_id WHERE o.id = 1;
SELECT * FROM orders WHERE id = 1;
COMMIT;
What is the configured follower-read staleness bound (max follower read lag), and are reads pinned to leaders for the session? This detail changes the recommendation because it determines whether a non-leader replica can satisfy read-your-writes after a cross-region write.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 22 Dec 2023, 11:34 UTC
In recent stable YugabyteDB releases, cross-tablet read-your-writes typically holds by default because DocDB stamps each write with a single hybrid logical timestamp (HLT), and the session uses that HLT as a lower visibility bound for subsequent reads across all tablets. Under default YSQL READ COMMITTED, each statement takes a fresh snapshot, so an immediate cross-tablet read should observe the write provided the serving replica's HLT has advanced past it.
The notable caveat involves follower reads in wide-area multi-region clusters: if a non-leader replica lags beyond the HLT visibility window, a read routed there may miss the recent write unless you configure default_read_consistency = leader. Tablet splitting generally does not break read-your-writes for in-flight queries, since they pin a consistent snapshot at query start, though new statements after a split may route to a different leader.
To confirm behavior in your specific version and topology, compare the write HLT against the read HLT in a cross-tablet SELECT, and repeat the test with follower reads enabled to identify the lag threshold.