# Routing Function
__*PD Route*__

![left](./student-guide/diagram/RoutingCapabilities_aa_002_20220509.png)

------------------------------------------------------------
Since PD stores all TiKV cluster information, it naturally provides a routing function to TiDB Server. supposition 

TiDB Server writes a value to the key.

1. The TiKV Client first obtains which region the key belongs to from PD, and PD return the meta information related to this region.

2. TiKV Client has a Region Cache that can cache metadata, so there is no need to get it from PD every time. Then send the command directly to the leader peer of the region.

3. It is possible that the leader of the region has drifted to another peer, TiKV will return a NotLeader error with the address of the new leader. The TiKV Client will update in the Region Cache and send the request to the new leader again.

4. It is also possible that the version of the region has changed, such as split. At this time, the key may have already been placed on the new region, and the TiKV client will receive an error from StaleCommand, so it will get it from PD again and enter state 1.