# `Routing`

![`smart`](RoutingCapabilities_aa_003_20220509.png)

---------------------------------------------------------------------------
Since the PD holds all of the TiKV cluster information, it can provide the routing features to the TiDB server. 

Let's say the TiDB server wants to write a value.

1. TiKV Client first fetches the key from PD which Region it belongs to, and PD returns meta information related to this Region.

2. TiKV Client has a Region Cache that caches metadata so it doesn't need to be fetched from PD every time. The command is then sent directly to the Region's Leader.

It's possible that the Leader of Region has changed to another TiKV instance, TiKV will return a NotLeader error with the address of the new Leader, and that's where the Region Cache will be updated, and the request will be sent again to the new Leader.

It is possible that the version of Region has changed; for example, it has split. At this point, the key may have been moved into the new Region, and the TiKV Client will receive the StaleCommand error, so the information will be fetched again from Placement Driver.
