What problems do readIndex read, lease read, and follower read solve?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: readIndex read、lease read、follower read是解决什么问题的?

| username: alfred

[TiDB Usage Environment] Online, Testing, Research
[TiDB Version]
[Encountered Problem]
[Reproduction Path] What operations were performed to cause the problem
[Problem Phenomenon and Impact]

[Attachments]

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: forever | Original post link

ReadIndex Read and Local Read

Inspect: Determine the type of request and how to handle it. Let’s focus on read requests, which can be handled in two ways:

RequestPolicy::ReadLocal, which is local read, indicates that the Peer is the leader and within the lease, allowing direct data reading.

RequestPolicy::ReadIndex, which is read index, indicates that the Peer is the leader but not within the lease, or the request explicitly requires handling with read index.

self.read_local: Handles the request using local read, directly reading from RocksDB.

self.read_index: Handles the request using read index, querying the majority of nodes to ensure it is the legitimate leader, and then reading from RocksDB after reaching or exceeding the linearizability point (read index).

The inspect method is not complicated; let’s look at it line by line:

req.get_header().get_read_quorum(): The request explicitly requires handling with read index, so it returns ReadIndex.

self.has_applied_to_current_term(): If the leader has not yet applied to its own term, it uses ReadIndex for handling. The reason can be found in TiKV feature introduction - Lease Read.

self.raft_group.raft.in_lease(): If the leader is not within the raft lease, it indicates potential issues such as network instability or unsuccessful heartbeats. It uses ReadIndex for handling.

self.leader_lease.inspect(None): Uses the CPU clock to determine if the leader is within the lease. If it is, it handles using ReadLocal.

In summary, if it is not certain that RocksDB can be read safely, use read index; otherwise, boldly use local read.

| username: alfred | Original post link

This is to address the question “Is the current region Leader role definitely the leader?” in order to ensure data reading accuracy in a distributed environment. Can you confirm that readindex read is the default in TiDB? Can it be modified?

| username: ddhe9527 | Original post link

To ensure linear consistency of read requests

| username: forever | Original post link

What do you mean by modification?

| username: forever | Original post link

readIndex read: This is a read request that obtains the Raft log index at the current moment, ensuring linear consistency.

lease read (local read): If the Peer is the leader and within the lease, it can directly read the data.

readIndex: If the Peer is the leader but not within the lease, or if the request explicitly requires using the read index, the request is processed using the read index method. This involves querying the majority of nodes to ensure it is the legitimate leader, and then reading from RocksDB once it reaches or surpasses the point of linear consistency (read index).

follower read: As the name suggests, this involves using the follower replica of the Region to handle data read tasks under the premise of strong consistency.

| username: alfred | Original post link

When giving the lecture, it was mentioned that readIndex read is used by default. What does “default” refer to in this context?

| username: alfred | Original post link

Can the default readIndex read here be adjusted to lease read?

| username: Lucien-卢西恩 | Original post link

Hello, the default cannot be modified. The execution logic is controlled by the TiKV code. I wonder if you have read these two source code articles to better understand the scenarios and principles of ReadIndex Read and Lease Read. Feel free to discuss, thank you.

| username: alfred | Original post link

I’ll read it, thanks.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.