# Reading Data: Readindex Read

![`smart`](ReadIndexRead01_aa_002_2022051.png)

---------------------------------------------------------------------
Regarding reading, the first thing TiKV needs to ensure is linear consistency. For example, if we changed (key = 1,value = Tom) to (key = 1,value = Jack) and submitted it at 10:00:00 AM, then it means that we cannot read (key = 1,value = Tom) after 10:00:00 AM.

Here, we first discuss the case of reading from the leader, let's take a look at a simplified process:

Assuming we want to read key = 1,
1. The client asks PD about the Region where key = 1 is located.
2. PD informs the Leader of the Region location (TiKV node).
3. TiDB server sends read commands to the TiKV node where the leader is located.
4. After receiving the read command, the leader node reads the RocksDB KV and then returns the data to the client.

The only problem we have to solve now is to make sure that the node remains the leader from the time we confirm the leader (step 2) to the time we read (step 4), without any leader role switching in between.

The so-called ReadIndex Read is the solution to this problem. Let's take a look at the specific process:

1. When TiKV receives a read request, it records the current log (Raft Log) sequence number, also known as the commit index, into a variable called ReadIndex.
2. Afterwards, this node initiates a heartbeat to other nodes. If most nodes return the corresponding heartbeat response, then the leader can determine that it is still the leader. Otherwise, we consider that the node is no longer the leader, and TiDB server needs to be re-read.
3. If it is confirmed that the current node is the leader, then wait for the Raft Log to be applied until the index of the Raft Log, i.e. the apply index, exceeds the ReadIndex, so that a consistent read can be provided safely.
4. Read the RocksDB of the leader node and return the result to the TiDB server.

Here, we introduced ReadIndex Read, and later we will explain the follower read feature, which is also implemented with it. Next, we will introduce an optimization method for ReadIndex Read, called Lease Read or Local Read.

