Skip to content

Commit a34a5ce

Browse files
authored
[doc][DOC-525][2024.2] Set follower read in transaction (yugabyte#24950)
* set follower read in transaction * format * add config parameters * review comment * copy to older versions
1 parent 3a8b69b commit a34a5ce

File tree

7 files changed

+140
-35
lines changed

7 files changed

+140
-35
lines changed

docs/content/preview/reference/configuration/yb-tserver.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,24 @@ Default: 1024
18121812
18131813
Default: true
18141814
1815+
##### yb_read_from_followers
1816+
1817+
Controls whether or not reading from followers is enabled. For more information, refer to [Follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/).
1818+
1819+
Default: false
1820+
1821+
##### yb_follower_read_staleness_ms
1822+
1823+
Sets the maximum allowable staleness. Although the default is recommended, you can set the staleness to a shorter value. The tradeoff is the shorter the staleness, the more likely some reads may be redirected to the leader if the follower isn't sufficiently caught up. You shouldn't set `yb_follower_read_staleness_ms` to less than 2x the `raft_heartbeat_interval_ms` (which by default is 500 ms).
1824+
1825+
Default: 30000 (30 seconds)
1826+
1827+
##### default_transaction_read_only
1828+
1829+
Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up [follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/#read-only-transaction).
1830+
1831+
Default: false
1832+
18151833
##### default_transaction_isolation
18161834
18171835
Specifies the default isolation level of each new transaction. Every transaction has an isolation level of `read uncommitted`, `read committed`, `repeatable read`, or `serializable`.

docs/content/stable/reference/configuration/yb-tserver.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,24 @@ Default: 1024
17901790
17911791
Default: true
17921792
1793+
##### yb_read_from_followers
1794+
1795+
Controls whether or not reading from followers is enabled. For more information, refer to [Follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/).
1796+
1797+
Default: false
1798+
1799+
##### yb_follower_read_staleness_ms
1800+
1801+
Sets the maximum allowable staleness. Although the default is recommended, you can set the staleness to a shorter value. The tradeoff is the shorter the staleness, the more likely some reads may be redirected to the leader if the follower isn't sufficiently caught up. You shouldn't set `yb_follower_read_staleness_ms` to less than 2x the `raft_heartbeat_interval_ms` (which by default is 500 ms).
1802+
1803+
Default: 30000 (30 seconds)
1804+
1805+
##### default_transaction_read_only
1806+
1807+
Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up [follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/#read-only-transaction).
1808+
1809+
Default: false
1810+
17931811
##### default_transaction_isolation
17941812
17951813
Specifies the default isolation level of each new transaction. Every transaction has an isolation level of `read uncommitted`, `read committed`, `repeatable read`, or `serializable`.

docs/content/v2.18/reference/configuration/yb-tserver.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,24 @@ Default: `1GB`
12261226

12271227
Default: 1
12281228

1229+
##### yb_read_from_followers
1230+
1231+
Controls whether or not reading from followers is enabled. For more information, refer to [Follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/).
1232+
1233+
Default: false
1234+
1235+
##### yb_follower_read_staleness_ms
1236+
1237+
Sets the maximum allowable staleness. Although the default is recommended, you can set the staleness to a shorter value. The tradeoff is the shorter the staleness, the more likely some reads may be redirected to the leader if the follower isn't sufficiently caught up. You shouldn't set `yb_follower_read_staleness_ms` to less than 2x the `raft_heartbeat_interval_ms` (which by default is 500 ms).
1238+
1239+
Default: 30000 (30 seconds)
1240+
1241+
##### default_transaction_read_only
1242+
1243+
Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up [follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/#read-only-transaction).
1244+
1245+
Default: false
1246+
12291247
## Admin UI
12301248

12311249
The Admin UI for the YB-TServer is available at `http://localhost:9000`.

docs/content/v2.20/reference/configuration/yb-tserver.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,24 @@ See also the [--ysql_yb_fetch_row_limit](#ysql-yb-fetch-row-limit) flag. If the
14041404

14051405
Default: 1024
14061406

1407+
##### yb_read_from_followers
1408+
1409+
Controls whether or not reading from followers is enabled. For more information, refer to [Follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/).
1410+
1411+
Default: false
1412+
1413+
##### yb_follower_read_staleness_ms
1414+
1415+
Sets the maximum allowable staleness. Although the default is recommended, you can set the staleness to a shorter value. The tradeoff is the shorter the staleness, the more likely some reads may be redirected to the leader if the follower isn't sufficiently caught up. You shouldn't set `yb_follower_read_staleness_ms` to less than 2x the `raft_heartbeat_interval_ms` (which by default is 500 ms).
1416+
1417+
Default: 30000 (30 seconds)
1418+
1419+
##### default_transaction_read_only
1420+
1421+
Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up [follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/#read-only-transaction).
1422+
1423+
Default: false
1424+
14071425
##### default_transaction_isolation
14081426

14091427
Specifies the default isolation level of each new transaction. Every transaction has an isolation level of `read uncommitted`, `read committed`, `repeatable read`, or `serializable`.

docs/content/v2024.2/develop/learn/transactions/transactions-global-apps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ In such scenarios, you can enable [follower reads](../../../../explore/going-bey
3333
To enable follower reads, set the transaction to be [READ ONLY](../../../../api/ysql/the-sql-language/statements/txn_set/#read-only-mode) and turn on the YSQL parameter `yb_read_from_followers`. For example:
3434

3535
```plpgsql
36-
SET yb_read_from_followers = true;
3736
BEGIN TRANSACTION READ ONLY;
37+
SET LOCAL yb_read_from_followers = true;
3838
...
3939
COMMIT;
4040
```

docs/content/v2024.2/explore/going-beyond-sql/follower-reads-ysql.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ YugabyteDB requires reading from the leader to read the latest data. However, fo
1717

1818
## Use cases
1919

20-
1. **Time-critical** : Suppose the end-user starts a donation page to raise money for a personal cause and the target amount must be met by the end of the day. In a time-critical scenario, the end-user benefits from accessing the most recent donation made on the page and can keep track of the progress. Such real-time applications require the latest information as soon as it is available. But in the case of an application where fetching slightly stale data is acceptable, follower reads can be helpful.
20+
1. **Time-critical**. Suppose the end-user starts a donation page to raise money for a personal cause and the target amount must be met by the end of the day. In a time-critical scenario, the end-user benefits from accessing the most recent donation made on the page and can keep track of the progress. Such real-time applications require the latest information as soon as it is available. But in the case of an application where fetching slightly stale data is acceptable, follower reads can be helpful.
2121

22-
1. **Latency-tolerant (staleness)** : Suppose a social media post gets a million likes and more continuously. For a post with massive likes such as this one, slightly stale reads are acceptable, and immediate updates are not necessary because the absolute number may not really matter to the end-user reading the post. Such applications do not need to always make requests directly to the leader. Instead, a slightly older value from the closest replica can achieve improved performance with lower latency.
22+
1. **Latency-tolerant (staleness)**. Suppose a social media post gets a million likes and more continuously. For a post with massive likes such as this one, slightly stale reads are acceptable, and immediate updates are not necessary because the absolute number may not really matter to the end-user reading the post. Such applications do not need to always make requests directly to the leader. Instead, a slightly older value from the closest replica can achieve improved performance with lower latency.
2323

2424
Follower reads are applicable for applications that can tolerate staleness. Replicas may not be completely up-to-date with all updates, so this design may respond with stale data. You can specify how much staleness the application can tolerate. When enabled, read-only operations may be handled by the closest replica, instead of having to go to the leader.
2525

@@ -56,8 +56,8 @@ The following table provides information on the expected behavior when a read ha
5656

5757
You can mark a transaction as read-only by applying the following guidelines:
5858

59-
- `SET TRANSACTION READ ONLY` applies only to the current transaction block.
60-
- `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY` applies the read-only setting to all statements and transaction blocks that follow.
59+
- SET TRANSACTION READ ONLY applies only to the current transaction block.
60+
- SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY applies the read-only setting to all statements and transaction blocks that follow.
6161
- `SET default_transaction_read_only = TRUE` applies the read-only setting to all statements and transaction blocks that follow.
6262

6363
Note: The use of `pg_hint_plan` to mark a statement as read-only is not recommended. It may work in some cases, but relies on side effects and has known issues (see [GH17024](https://github.com/yugabyte/yugabyte-db/issues/17024) and [GH17135](https://github.com/yugabyte/yugabyte-db/issues/17135)).
@@ -68,16 +68,31 @@ Note: The use of `pg_hint_plan` to mark a statement as read-only is not recommen
6868

6969
- If the follower is not yet caught up to `<current_time> - <staleness>`, the read is redirected to a different replica transparently from the end-user. The end-user may see a slight increase in latency depending on the location of the replica which satisfies the read.
7070

71-
7271
## Examples
7372

7473
This example uses follower reads because the transaction is marked read-only:
7574

7675
```sql
77-
set yb_read_from_followers = true;
78-
start transaction read only;
79-
SELECT * from t WHERE k='k1';
80-
commit;
76+
SET yb_read_from_followers = true;
77+
START TRANSACTION READ ONLY;
78+
SELECT * FROM t WHERE k='k1';
79+
COMMIT;
80+
SET yb_read_from_followers = false;
81+
```
82+
83+
```output
84+
k | v
85+
----+----
86+
k1 | v1
87+
```
88+
89+
Use SET LOCAL inside a transaction to have follower reads turned on only for the transaction. Set `yb_read_from_followers` before any statement is executed in the block:
90+
91+
```sql
92+
START TRANSACTION READ ONLY;
93+
SET LOCAL yb_read_from_followers = true;
94+
SELECT * FROM t WHERE k='k1';
95+
COMMIT;
8196
```
8297

8398
```output
@@ -89,9 +104,9 @@ commit;
89104
This example uses follower reads because the session is marked read-only:
90105

91106
```sql
92-
set session characteristics as transaction read only;
93-
set yb_read_from_followers = true;
94-
SELECT * from t WHERE k='k1';
107+
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
108+
SET yb_read_from_followers = true;
109+
SELECT * FROM t WHERE k='k1';
95110
```
96111

97112
```output
@@ -101,16 +116,16 @@ SELECT * from t WHERE k='k1';
101116
(1 row)
102117
```
103118

104-
The following is a `JOIN` example that uses follower reads:
119+
The following is a JOIN example that uses follower reads:
105120

106121
```sql
107-
create table table1(k int primary key, v int);
108-
create table table2(k int primary key, v int);
109-
insert into table1 values (1, 2), (2, 4), (3, 6), (4,8);
110-
insert into table2 values (1, 3), (2, 6), (3, 9), (4,12);
111-
set yb_read_from_followers = true;
112-
set session characteristics as transaction read only;
113-
select * from table1, table2 where table1.k = 3 and table2.v = table3.v;
122+
CREATE TABLE table1(k int primary key, v int);
123+
CREATE TABLE table2(k int primary key, v int);
124+
INSERT INTO table1 values (1, 2), (2, 4), (3, 6), (4,8);
125+
INSERT INTO table2 values (1, 3), (2, 6), (3, 9), (4,12);
126+
SET yb_read_from_followers = true;
127+
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
128+
SELECT * FROM table1, table2 WHERE table1.k = 3 AND table2.v = table3.v;
114129
```
115130

116131
```output
@@ -123,10 +138,10 @@ select * from table1, table2 where table1.k = 3 and table2.v = table3.v;
123138
The following examples demonstrate staleness after enabling the `yb_follower_read_staleness_ms` property:
124139

125140
```sql
126-
set session characteristics as transaction read write;
127-
insert into t values ('k1', 'v1')
141+
SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;
142+
INSERT INTO t values ('k1', 'v1')
128143
/* sleep 10s */
129-
select * from t where k = 'k1';
144+
SELECT * FROM t WHERE k = 'k1';
130145
```
131146

132147
```output
@@ -141,10 +156,10 @@ UPDATE t SET v = 'v1+1' where k = 'k1';
141156
/* sleep 10s */
142157
UPDATE t SET v = 'v1+2' where k = 'k1';
143158
/* sleep 10s */
144-
select * from t where k = 'k1';
159+
SELECT * FROM t WHERE k = 'k1';
145160
```
146161

147-
This selects the latest version of the row because the transaction setting for the session is `read write`.
162+
This selects the latest version of the row because the transaction setting for the session is READ WRITE.
148163

149164
```output
150165
k | v
@@ -154,9 +169,9 @@ This selects the latest version of the row because the transaction setting for t
154169
```
155170

156171
```sql
157-
set session characteristics as transaction read only;
158-
set yb_read_from_followers = true;
159-
select * from t where k = 'k1';
172+
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
173+
SET yb_read_from_followers = true;
174+
SELECT * FROM t WHERE k = 'k1';
160175
```
161176

162177
```output
@@ -167,8 +182,8 @@ select * from t where k = 'k1';
167182
```
168183

169184
```sql
170-
set yb_follower_read_staleness_ms = 5000;
171-
select * from t where k = 'k1'; /* up to 5s old value */
185+
SET yb_follower_read_staleness_ms = 5000;
186+
SELECT * FROM t WHERE k = 'k1'; /* up to 5s old value */
172187
```
173188

174189
```output
@@ -179,8 +194,8 @@ select * from t where k = 'k1'; /* up to 5s old value */
179194
```
180195

181196
```sql
182-
set yb_follower_read_staleness_ms = 15000;
183-
select * from t where k = 'k1'; /* up to 15s old value */
197+
SET yb_follower_read_staleness_ms = 15000;
198+
SELECT * FROM t WHERE k = 'k1'; /* up to 15s old value */
184199
```
185200

186201
```output
@@ -191,8 +206,8 @@ select * from t where k = 'k1'; /* up to 15s old value */
191206
```
192207

193208
```sql
194-
set yb_follower_read_staleness_ms = 25000;
195-
select * from t where k = 'k1'; /* up to 25s old value */
209+
SET yb_follower_read_staleness_ms = 25000;
210+
SELECT * FROM t WHERE k = 'k1'; /* up to 25s old value */
196211
```
197212

198213
```output

docs/content/v2024.2/reference/configuration/yb-tserver.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,24 @@ Default: 1024
18121812
18131813
Default: true
18141814
1815+
##### yb_read_from_followers
1816+
1817+
Controls whether or not reading from followers is enabled. For more information, refer to [Follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/).
1818+
1819+
Default: false
1820+
1821+
##### yb_follower_read_staleness_ms
1822+
1823+
Sets the maximum allowable staleness. Although the default is recommended, you can set the staleness to a shorter value. The tradeoff is the shorter the staleness, the more likely some reads may be redirected to the leader if the follower isn't sufficiently caught up. You shouldn't set `yb_follower_read_staleness_ms` to less than 2x the `raft_heartbeat_interval_ms` (which by default is 500 ms).
1824+
1825+
Default: 30000 (30 seconds)
1826+
1827+
##### default_transaction_read_only
1828+
1829+
Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up [follower reads](../../../explore/going-beyond-sql/follower-reads-ysql/#read-only-transaction).
1830+
1831+
Default: false
1832+
18151833
##### default_transaction_isolation
18161834
18171835
Specifies the default isolation level of each new transaction. Every transaction has an isolation level of `read uncommitted`, `read committed`, `repeatable read`, or `serializable`.

0 commit comments

Comments
 (0)