Skip to content

Commit e2c0eba

Browse files
authored
reference: update doc for follower read (#21962)
1 parent 04c20b1 commit e2c0eba

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

follower-read.md

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,36 @@ aliases: ['/docs/dev/follower-read/','/docs/dev/reference/performance/follower-r
66

77
# Follower Read
88

9-
When a read hotspot appears in a Region, the Region leader can become a read bottleneck for the entire system. In this situation, enabling the Follower Read feature can significantly reduce the load of the leader, and improve the throughput of the whole system by balancing the load among multiple followers. This document introduces the use and implementation mechanism of Follower Read.
9+
In TiDB, to ensure high availability and data safety, TiKV stores multiple replicas for each Region, one of which is the leader and the others are followers. By default, all read and write requests are processed by the leader. The Follower Read feature enables TiDB to read data from follower replicas of a Region while maintaining strong consistency, thereby reducing the read workload on the leader and improving the overall read throughput of the cluster.
1010

11-
## Overview
11+
<CustomContent platform="tidb">
12+
13+
When performing Follower Read, TiDB selects an appropriate replica based on the topology information. Specifically, TiDB uses the `zone` label to identify local replicas: if the `zone` label of a TiDB node is the same as that of the target TiKV node, TiDB considers the replica as a local replica. For more information, see [Schedule Replicas by Topology Labels](/schedule-replicas-by-topology-labels.md).
14+
15+
</CustomContent>
16+
17+
<CustomContent platform="tidb-cloud">
18+
19+
When performing Follower Read, TiDB selects an appropriate replica based on the topology information. Specifically, TiDB uses the `zone` label to identify local replicas: if the `zone` label of a TiDB node is the same as that of the target TiKV node, TiDB considers the replica as a local replica. The `zone` label is set automatically in TiDB Cloud.
20+
21+
</CustomContent>
22+
23+
By enabling followers to handle read requests, Follower Read achieves the following goals:
24+
25+
- Distribute read hotspots and reduce the leader workload.
26+
- Prioritize local replica reads in multi-AZ or multi-datacenter deployments to minimize cross-AZ traffic.
1227

13-
The Follower Read feature refers to using any follower replica of a Region to serve a read request under the premise of strongly consistent reads. This feature improves the throughput of the TiDB cluster and reduces the load of the leader. It contains a series of load balancing mechanisms that offload TiKV read loads from the leader replica to the follower replica in a Region. TiKV's Follower Read implementation provides users with strongly consistent reads.
28+
## Usage scenarios
29+
30+
Follower Read is suitable for the following scenarios:
31+
32+
- Applications with heavy read requests or significant read hotspots.
33+
- Multi-AZ deployments where you want to prioritize reading from local replicas to reduce cross-AZ bandwidth usage.
34+
- Read-write separation architectures that you want to further improve overall read performance.
1435

1536
> **Note:**
1637
>
17-
> To achieve strongly consistent reads, the follower node currently needs to request the current execution progress from the leader node (that is `ReadIndex`), which causes an additional network request overhead. Therefore, the main benefits of Follower Read are to isolate read requests from write requests in the cluster and to increase overall read throughput.
38+
> To ensure strong consistency of the read results, Follower Read communicates with the leader before reading to confirm the latest commit progress (by executing the Raft `ReadIndex` operation). This introduces an additional network interaction. Therefore, Follower Read is most effective where a large number of read requests exist or read-write isolation is required. However, for low-latency single queries, the performance improvement might not be significant.
1839
1940
## Usage
2041

@@ -30,7 +51,24 @@ Scope: SESSION | GLOBAL
3051

3152
Default: leader
3253

33-
This variable is used to set the expected data read mode.
54+
This variable defines the expected data read mode. Starting from v8.5.4 and v9.0.0, this variable only takes effect on read-only SQL statements.
55+
56+
In scenarios where you need to reduce cross-AZ traffic by reading from local replicas, the following configurations are recommended:
57+
58+
- `leader`: the default value, providing the best performance.
59+
- `closest-adaptive`: minimizes cross-AZ traffic while keeping performance loss to a minimum.
60+
- `closest-replicas`: maximizes cross-AZ traffic savings but might cause some performance degradation.
61+
62+
If you are using other configurations, refer to the following table to modify them to the recommended configurations:
63+
64+
| Current configuration | Recommended configuration |
65+
| ------------- | ------------- |
66+
| `follower` | `closest-replicas` |
67+
| `leader-and-follower` | `closest-replicas` |
68+
| `prefer-leader` | `closest-adaptive` |
69+
| `learner` | `closest-replicas` |
70+
71+
If you want to use a more precise read replica selection policy, refer to the full list of available configurations as follows:
3472

3573
- When you set the value of `tidb_replica_read` to `leader` or an empty string, TiDB maintains its default behavior and sends all read operations to the leader replica to perform.
3674
- When you set the value of `tidb_replica_read` to `follower`, TiDB selects a follower replica of the Region to perform read operations. If the Region has learner replicas, TiDB also considers them for reads with the same priority. If no available follower or learner replicas exist for the current Region, TiDB reads from the leader replica.
@@ -57,18 +95,56 @@ This variable is used to set the expected data read mode.
5795
5896
</CustomContent>
5997

98+
## Basic monitoring
99+
100+
> **Note**
101+
>
102+
> This section is only applicable to TiDB Self-Managed.
103+
104+
<CustomContent platform="tidb">
105+
106+
You can check the [**TiDB** > **KV Request** > **Read Req Traffic** panel (New in v8.5.4 and v9.0.0)](/grafana-tidb-dashboard.md#kv-request) to determine whether to enable Follower Read and observe the traffic reduction effect after enabling it.
107+
108+
</CustomContent>
109+
110+
<CustomContent platform="tidb-cloud">
111+
112+
You can check the [**TiDB** > **KV Request** > **Read Req Traffic** panel (New in v8.5.4 and v9.0.0)](https://docs.pingcap.com/tidb/stable/grafana-tidb-dashboard#kv-request) to determine whether to enable Follower Read and observe the traffic reduction effect after enabling it.
113+
114+
</CustomContent>
115+
60116
## Implementation mechanism
61117

62-
Before the Follower Read feature was introduced, TiDB applied the strong leader principle and submitted all read and write requests to the leader node of a Region to handle. Although TiKV can distribute Regions evenly on multiple physical nodes, for each Region, only the leader can provide external services. The other followers can do nothing to handle read requests but receive the data replicated from the leader at all times and prepare for voting to elect a leader in case of a failover.
118+
Before the Follower Read feature was introduced, TiDB applied the strong leader principle and submitted all read and write requests to the leader node of a Region to handle. Although TiKV can distribute Regions evenly on multiple physical nodes, for each Region, only the leader can provide external services. The other followers cannot handle read requests, and they only receive the data replicated from the leader at all times and prepare for voting to elect a leader in case of a failover.
63119

64-
To allow data reading in the follower node without violating linearizability or affecting Snapshot Isolation in TiDB, the follower node needs to use `ReadIndex` of the Raft protocol to ensure that the read request can read the latest data that has been committed on the leader. At the TiDB level, the Follower Read feature simply needs to send the read request of a Region to a follower replica based on the load balancing policy.
120+
Follower Read includes a set of load balancing mechanisms that offload TiKV read requests from the leader replica to a follower replica in a Region. To allow data reading from the follower node without violating linearizability or affecting Snapshot Isolation in TiDB, the follower node needs to use `ReadIndex` of the Raft protocol to ensure that the read request can read the latest data that has been committed on the leader node. At the TiDB level, the Follower Read feature simply needs to send the read request of a Region to a follower replica based on the load balancing policy.
65121

66122
### Strongly consistent reads
67123

68124
When the follower node processes a read request, it first uses `ReadIndex` of the Raft protocol to interact with the leader of the Region, to obtain the latest commit index of the current Raft group. After the latest commit index of the leader is applied locally to the follower, the processing of a read request starts.
69125

126+
![read-index-flow](/media/follower-read/read-index.png)
127+
70128
### Follower replica selection strategy
71129

72-
Because the Follower Read feature does not affect TiDB's Snapshot Isolation transaction isolation level, TiDB adopts the round-robin strategy to select the follower replica. Currently, for the coprocessor requests, the granularity of the Follower Read load balancing policy is at the connection level. For a TiDB client connected to a specific Region, the selected follower is fixed, and is switched only when it fails or the scheduling policy is adjusted.
130+
The Follower Read feature does not affect TiDB's Snapshot Isolation transaction isolation level. TiDB selects a replica based on the `tidb_replica_read` configuration for the first read attempt. From the second retry onward, TiDB prioritizes ensuring successful reads. Therefore, when the selected follower node becomes inaccessible or has other errors, TiDB switches to the leader for service.
131+
132+
#### `leader`
133+
134+
- Always selects the leader replica for reads, regardless of its location.
135+
136+
#### `closest-replicas`
137+
138+
- When the replica in the same AZ as TiDB is the leader node, TiDB does not perform Follower Read from it.
139+
- When the replica in the same AZ as TiDB is a follower node, TiDB performs Follower Read from it.
140+
141+
#### `closest-adaptive`
142+
143+
- If the estimated result is not large enough, TiDB uses the `leader` policy and does not perform Follower Read.
144+
- If the estimated result is large enough, TiDB uses the `closest-replicas` policy.
145+
146+
### Follower Read performance overhead
147+
148+
To ensure strong data consistency, Follower Read performs a `ReadIndex` operation regardless of how much data is read, which inevitably consumes additional TiKV CPU resources. Therefore, in small-query scenarios (such as point queries), the performance loss of Follower Read is relatively more obvious. Moreover, because the traffic reduced by local reads for small queries is limited, Follower Read is more recommended for large queries or batch reading scenarios.
73149

74-
However, for the non-coprocessor requests, such as a point query, the granularity of the Follower Read load balancing policy is at the transaction level. For a TiDB transaction on a specific Region, the selected follower is fixed, and is switched only when it fails or the scheduling policy is adjusted. If a transaction contains both point queries and coprocessor requests, the two types of requests are scheduled for reading separately according to the preceding scheduling policy. In this case, even if a coprocessor request and a point query are for the same Region, TiDB processes them as independent events.
150+
When `tidb_replica_read` is set to `closest-adaptive`, TiDB does not perform Follower Read for small queries. As a result, under various workloads, the additional CPU overhead on TiKV is typically no more than 10% compared with the `leader` policy.

grafana-tidb-dashboard.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@ The following metrics relate to requests sent to TiKV. Retry requests are counte
124124
- **local**: the number of requests per second that attempt a stale read in the local zone
125125
- Stale Read Req Traffic:
126126
- **cross-zone-in**: the incoming traffic of responses to requests that attempt a stale read in a remote zone
127-
- **cross-zone-out**: the outgoing traffic of requests that attempt a stale read in a remote zone
127+
- **cross-zone-out**: the outgoing traffic of responses to requests that attempt a stale read in a remote zone
128128
- **local-in**: the incoming traffic of responses to requests that attempt a stale read in the local zone
129129
- **local-out**: the outgoing traffic of requests that attempt a stale read in the local zone
130+
- Read Req Traffic
131+
- **leader-local**: traffic generated by Leader Read processing read requests in the local zone
132+
- **leader-cross-zone**: traffic generated by Leader Read processing read requests in a remote zone
133+
- **follower-local**: traffic generated by Follower Read processing read requests in the local zone
134+
- **follower-cross-zone**: traffic generated by Follower Read processing read requests in a remote zone
130135

131136
### PD Client
132137

media/follower-read/read-index.png

49.3 KB
Loading

system-variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5433,7 +5433,7 @@ SHOW WARNINGS;
54335433
- Type: Enumeration
54345434
- Default value: `leader`
54355435
- Possible values: `leader`, `follower`, `leader-and-follower`, `prefer-leader`, `closest-replicas`, `closest-adaptive`, and `learner`. The `learner` value is introduced in v6.6.0.
5436-
- This variable is used to control where TiDB reads data.
5436+
- This variable is used to control where TiDB reads data. Starting from v8.5.4 and v9.0.0, this variable only takes effect on read-only SQL statements.
54375437
- For more details about usage and implementation, see [Follower read](/follower-read.md).
54385438
54395439
### tidb_restricted_read_only <span class="version-mark">New in v5.2.0</span>

0 commit comments

Comments
 (0)