# Read Data


![left](./student-guide/diagram/03-ReadIndex-Read-01-01.png)

------------------------------------------------------------
When it comes to reading, the first thing TiKV needs to ensure is linear consistency, so called linear consistency. For example, at 10:00:00 a.m., we change key = 1, value = Tom to key = 1, value = Jack and submit it, that is, after 10:00:00 a.m., we can't read the key. = 1, value = Tom.

Here, let's first discuss the situation of reading from leader. Let's first take a look at a simplified process:

Suppose we want to read key = 1,
- Client asks PD about the region where key = 1 is located
- The PD informs the Leader of the region location (TiKV node)
- TiDB Server sends the read command to the TiKV node where the leader resides
- After the leader node accepts the read command, read the KV RocksDB and return the data to the client

The only problem we need to solve at this point is that when we confirm the leader (step 2) until we read (step 4), we can ensure that this node is still the leader, without switching the leader role during this process.

The so-called ReadIndex Read solves this problem. Let's take a look at the specific process:
- When TiKV receives a read request, the current log (raft log) sequence number, also called commit index, is recorded in a variable readIndex.
- After that, this node initiates a heartbeat to other nodes. If most nodes return the corresponding heartbeat response (heartbeat response), then the leader can be sure that he is still the leader. On the other hand, we think that the node at this point is no longer the leader and needs to be read again by the TiDB Server.
- If it is confirmed that the current node is the leader, wait for the log (raft log) to be applied until the sequence number of the log (raft log), that is, apply index, exceeds readIndex, so that consistency reads can be safely provided.
- Read the RocksDB of the leader node and return the result to the TiDB Server.

Here, we have introduced ReadIndex Read, and the follower read function that we will explain later is also implement with it. Let's first introduce an optimization method for readIndex Read, which is called Lease Read, or Local Read.